Built-in Functions in Python

Built-in Functions in Python

Built-in functions are predefined functions that are already available in Python. Let's have a look at some of the commonly used built-in functions in Python.

input Function

The function input enables us to accept an input string from the user without evaluating its value. The function input continues to read input text from the user until it encounters a newline, for example:

invoking input function to take user input

11.jpg

The variable name now refers to the string value 'Alok' 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. So, we say that the function input has been called with the argument 'Enter a number: . Further, we say that the function returns the string entered by the user ('Alok') which is subsequently assigned to the variable name.

eval Function

The function eval is used to evaluate the value of a string, for example:

evaluating a string 12.jpg

Composition

The value returned by a function may be used as an argument for another function in a nested manner. This 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 function to evaluate its value, for example:

nested calls: output of inner function serves as input argument to outer function

13.jpg

print Function

14.jpg Statement print('hello') calls 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 characters: hello. Apostrophe marks are used just to tell Python where a string begins and ends and do not form part of the string. Any number of comma-separated expressions may be used while invoking a print function, for example:

15.jpg Use of Python escape sequences :

16.jpg

If we do not want Python to interpret an escape sequence, it should be preceded by another backslash. Another way of achieving the same thing is to use R or r before the string containing escape symbol, for example:

17.jpg Python also supports various other escape sequences such as \a (ASCII bell), \b (ASCII backspace), \f (ASCII form feed), and \r (ASCII carriage return). However, some of these are not supported by Python IDLE.

type Function

Values or objects 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 of a value, for example:

18.jpg

round Function

The round function rounds a number up to specific number of decimal places, for example:

rounding to nearest value

19.jpg 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.

min and max Functions

The functions max and min are used to find maximum and minimum value respectively out of several values. These functions can also operate on string values. Next, we illustrate the use of these functions.

20.jpg

Note that the integer and floating point values are compatible for comparison. However, numeric values cannot be compared with string values.

operands must be compatible for comparison

pow Function

The function pow(a, b)computes a. Thus, given the side of a cube, if we want to find its volume, we may just write pow(side, 3).

Random Number Generation

Imagine a two-player (say, player A and player B) game in which we want to decide who will play the first turn. Python provides a function random that generates a random number in the range [0,1). Python module random contains this function and needs to be imported for using it. Let us agree that player A will play the first turn if the random number generated falls in the range [0,0.5). Otherwise, player B will play the first turn. The following sequence of statements achieves this:

21.jpg generating a random number in the range (0, 1)

The if-else statement use here will be discussed in detail in the next chapter. In the case of multiple players, using the above approach for deciding who will play the first turn will make the program complex. The random module provides another function randint that randomly chooses an integer in the specified range, for example, randint (1,n) will randomly generate a number in the range 1 to n (both 1 and n included).

Functions from math module

There are various operations such as floor, ceil, log, square root, cos, and sin that may be required in different applications. The math module provides these functions, among several others.

Functions for mathematical computations :

Functions from math module :

  1. ceil (x) - Returns the smallest integer greater than or equal to x.
  2. floor (x) - Returns the largest integer less than or equal to x.
  3. fabs (x) - Returns the absolute value of x.
  4. exp (x) - Returns the value of expression e**x.
  5. log (x, b) - Returns the log(x) to the base b. In the case of absence of the second argument, the logarithmic value of x to the base e is returned.
  6. log10 (x) - Returns the log(x) to the base 10. This is equivalent to specifying math.log (x, 10).
  7. pow (x, y) - Returns x raised to the power y, i.e., x**y. Returns the square root of x.
  8. sqrt (x) - Returns the square root of x.
  9. cos (x) - Returns the cosine of x radians.
  10. sin (x) - Returns the sine of x radians.
  11. tan (x) - Returns the tangent of x radians.
  12. acos (x) - Returns the inverse cosine of x in radians.
  13. asin (x) - Returns the inverse sine of x in radians.
  14. atan (x) - Returns the inverse tangent of x in radians.
  15. degrees (x) - Returns a value in degree equivalent of input value x (in radians).
  16. radians (x) - Returns a value in radian equivalent of input value x (in degrees).

In order to make these functions available for use in a script, we need to import the math module. The import statement serves this purpose:

import math

The math module also defines a constant math.pi having value 3.141592653589793.

22.jpg 23.jpg

Complete list of Built-in Functions

If we want to see the complete list of built-in functions, we can use the built-in function dir as dir(builtins..). To know the purpose of a function and how it is used, we may make use of the function help, for example, to display help on cos function in the math module, we may give the following instructions:

getting help on a function

24.jpg

Hope you liked this article. Let me know your thoughts in the comments below. Criticism is always welcome! Stay tuned for more such informative articles.

Thanks for reading :)

Have a nice day!