Knowee
Questions
Features
Study Tools

0. Basic annotations - addmandatoryWrite a type-annotated function add that takes a float a and a float b as arguments and returns their sum as a float.bob@dylan:~$ cat 0-main.py#!/usr/bin/env python3add = __import__('0-add').addprint(add(1.11, 2.22) == 1.11 + 2.22)print(add.__annotations__)bob@dylan:~$ ./0-main.pyTrue{'a': <class 'float'>, 'b': <class 'float'>, 'return': <class 'float'>}

Question

  1. Basic annotations - addmandatoryWrite a type-annotated function add that takes a float a and a float b as arguments and returns their sum as a float.bob@dylan:~ cat 0-main.py#!/usr/bin/env python3add = __import__('0-add').addprint(add(1.11, 2.22) == 1.11 + 2.22)print(add.__annotations__)bob@dylan:~ ./0-main.pyTrue{'a': <class 'float'>, 'b': <class 'float'>, 'return': <class 'float'>}
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is how you can define the function add with type annotations in Python:

def add(a: float, b: float) -> float:
    return a + b

In this function:

  • a: float and b: float are the input parameters, both are expected to be of type float.
  • -> float indicates that the function will return a float.

This function simply adds the two input parameters a and b and returns the result. The type annotations help to make the code more readable and self-explanatory.

This problem has been solved

Similar Questions

Write a function that adds 2 integers.Prototype: def add_integer(a, b=98):a and b must be integers or floats, otherwise raise a TypeError exception with the message a must be an integer or b must be an integera and b must be first casted to integers if they are floatReturns an integer: the addition of a and bYou are not allowed to import any moduleguillaume@ubuntu:~/0x07$ cat 0-main.py#!/usr/bin/python3add_integer = __import__('0-add_integer').add_integerprint(add_integer(1, 2))print(add_integer(100, -2))print(add_integer(2))print(add_integer(100.3, -2))try: print(add_integer(4, "School"))except Exception as e: print(e)try: print(add_integer(None))except Exception as e: print(e)guillaume@ubuntu:~/0x07$ ./0-main.py39810098b must be an integera must be an integerguillaume@ubuntu:~/0x07$ python3 -m doctest -v ./tests/0-add_integer.txt | tail -29 passed and 0 failed.Test passed.guillaume@ubuntu:~/0x07$ python3 -c 'print(__import__("0-add_integer").__doc__)' | wc -l5guillaume@ubuntu:~/0x07$ python3 -c 'print(__import__("0-add_integer").add_integer.__doc__)' | wc -l3guillaume@ubuntu:~/0x07$ Repo:GitHub repository: alx-higher_level_programmingDirectory: 0x07-python-test_driven_developmentFile: 0-add_integer.py, tests/0-add_integer.txt

Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:Declare variables: two of type int and two of type float.Read lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your variables.Use the and operator to perform the following operations:Print the sum and difference of two int variable on a new line.Print the sum and difference of two float variable rounded to one decimal place on a new line.

You have a class Measure:public class Measure { public float addition(float a, int b){ return a + b; }}When executed, which lines of code will involve the type promotion of one of the input arguments specified?

Define a function cumsum(num). The function returns the sum of all positive integers smaller or equal to num.Examplecumsum(4) -> 1+2+3+4 = 10cumsum(5.5) -> 1+2+3+4+5 = 15Hintint() converts a float type value into an interger.

Write a program using applets to accept a floating value as shown in the following output, theprogram should compute the sum of the two values and display the results on the screen. [3 marks]

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.