The function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. After function is defined you can call a function by typing the function name (function call). Examples of function calls are given in the following example.
Example 1Create a variable and assign the value of 6 to the variable. Then check the type of the variable and transform it to float type.
Solution: The number 6 will be assigned to variable named "num".
Example 1Create a variable and assign the value of 6 to the variable. Then check the type of the variable and transform it to float type.
Solution: The number 6 will be assigned to variable named "num".
num = 6The type of then number must be checked out so we will type the following:
print("type(num) = ",type(num))Now change the integer num to float num. For this conversion, we will create a new variable and name it num2.
num2 = float(num)Finally, show the variable type of num2 using the type function and print function.
print("type(num2) = ",type(num2))The entire code of Example 1 is shown below.
num = 6When previous code is executed the following outptu is obtained.
print("type(num) = ",type(num))
num2 = float(num)
print("type(num2) = ",type(num2))
type(num) = <class 'int'>In previous example (Example 1) we have used functions named type, float, and print. The expressions in parentheses after the function name is called argument of the function. The argument is a value or variable that we are passing into the function as input to the function. The result for type function is the type of argument. The result for float function is transformed from integer type value to a float type value. The result of print function is the output displayed on the screen.
type(num2) = <class 'float'>