FUNCTIONS IN PYTHON

BUILT-IN FUNCTIONS IN PYTHON

Built-in function are predefined functions that are already available in python. Recall that we have already used the build-in functions input and print. Next, we will discuss some more examples of built-in functions.

input Function

The function input enables us to accept an input string form the user without evaluating its value. The function input continues to read input text from the user until it encounters a newline, for example:
>>> name = input('Enter a name:')
Enter a name: Zeeshan
>>>name
'Zeeshan'
The variable name now refers to the string value 'Zeeshan' entered by the user. Making use of a function is called calling the function, or invoking the function. In the above statement, the string 'Enter a name: ' specified within the parentheses is called an argument 'Enter a number: '. Further, we say that the function returns the string entered by the user ('Zeeshan')which is subsequently assigned to the variable name.

eval Function

The function eval is used to evaluate the value of a string, for example:
>>> eval('15')
15
>>> eval('15+10')
25

Composition

The value returned by a function may be used as an argument for another function in a nested manner.The is called composition. For example, if we wish to take a numeric value or an expression as an input from the user, we take the input string from the user using the function input, and apply eval functon to evaluate its value, for example:
>>> n1= eval(input('Enter a number: '))
Enter a number: 234
>>> n1
234
>>> n2 = eval(input('Enter an arithmetic expression: '))
Enter an arithmetic expression: 12.0 + 13.0 * 2
>>> n2
38.0
Note that the input 234 and 12.0 + 13.0 *2 were correctly evaluated as int and float value respectively.

print Function

Recall from the previous chapter that the following statement enables us to produce the output in Python. On execution of the statement:
print('hello')
the system will output hello:
>>> print('hello')
hello

Statement print('hello') call the function print with the argument 'hello'. Note that the output of print function does not include any apostrophe marks as it displays the value of the string that comprises the sequence of charactors: hello. Apostrophe marks are used just to tell Python where a string begins and do not form part of the string. Any number of comma-separated expressions may be used while invoking a print function, for example:
>>> print(2, 567, 234)
2 567 234
>>> name = 'Zeeshan'
>>> print('hello', name, '2 + 2=', 2 + 2)
hello Zeeshan 2 + 2 = 4

Note that when several values are including in a call to the print function separated by commas, they are displayed on the same line, separated by single spaces between them. It is important to point out that after printing the values of the expressions included as arguments while invoking the print function, the print control moves to the beginning of the next line. Thus, the output of a sequence of print function calls appears on separate lines. In the next example, the output of a single call to the print function is displayed on two lines:
>>> print('hello', name, '\n2 + 2 =', 2 + 2)
hello Zeeshan
2 + 2 = 4

The backslash character \ has a special meaning. The character sequnce \n serves as a line feed character and transfers the print control to the beginning of the next line. The character sequences like \n and \t are called escape sequences. Next, we give an example of the escape sequence \t which is interpreted as a tab character:
>>> print('hello' , name, '\t2 + 2 =', 2 + 2)
hello Zeeshan 2 + 2 = 4

.

Type Function

Values or object in Python are classified into types or classes, for example, 12 is an integer, 12.5 is a floating point number, and 'hello' is a string. Python function type tells us the type pf a value, for example:
>>> print(type(12), type(12.5), type('hello'), type(int))

Round Function

The round function rounds a number up to specific number of decimal places. for example:
>>> print(round(89.625 , 2), round(89.635), round(89.635 , 0))
89.62 90.0
>>> print(round(34.12, 1), round(-34.63))
34.1 -35

When calling the function round, the first argument is used to specify the value to be rounded, and the second argument is used to specify the number of decimal digits desired after rounding. In a call to function round, if the second argument is missing, the system rounds the value of first argument to an integer, for example, round(89.635) yields 90.

2 Responses to "FUNCTIONS IN PYTHON"

ads