Built-In Functions in Python

A tutorial for Python beginners.

Rohit Kumar Thakur
Python in Plain English
5 min readOct 17, 2021

--

Python Function

As a problem becomes complex, the program also increases in size and complexity, and a programmer can't keep track of the data and control statements in the whole program. But they can deal with problems whose solutions can be written in the form of small Python programs, finding their solutions in the form of functions and integrating them to form the final program.

If necessary, the sub-programs can be divided into a set of smaller problems. It can be continued to any appropriate level. This approach to problem-solving is called the stepwise refinement method or modular approach.

In this article, we will learn how simple statements can be put together in the form of functions to do useful tasks.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

Built-in Functions

Built-in functions are predefined functions that are already available in Python. It has already been made available in Python for use by programmers.

print() Function

>>> print("hello dev")

When you execute the above program in Python IDLE. It shows the output: hello dev

Here, the statement print(“hello dev”) calls the function print() with the argument “hello dev”. The print function does not include any apostrophe marks as it displays the value of the string that comprises the sequence of characters in “hello dev”.

You can add comma-separated numbers too inside the print() function.

>>> print(3, 45, 567, 677)
3 45 567 677

Note that when several values are included in a call to the print() function separated by commas, they are displayed on the line, separated by single spaces between them.

>>> x='dev'
>>> print('hello', x, '3+3=' 3+3)
hello dev 3+3= 6

The backslash characters \ have a special meaning. The character sequence \n serves as a line feed character and transfers the print control to the beginning of the line. The character sequences like \n and \t are called escape sequences.

>>> print('hello', x, '\n3+3=', 3+3)
hello dev
3+3= 6
>>> print('hello', x, '\t3+3=', 3+3)
hello dev 3+3= 6

If in case you want to add \n in the print function and don’t want to end up your Python statement in a new line then you can use a double slash to precede it. The other method is to use R or r before the string containing the escape symbol.

>>> print('This is \\n a new line')
This is \n a new line
>>> print(R'This is \n a new line')
This is \n a new line
>>> print(r'This is \n a new line')
This is \n a new line

input() Function

The input() function allows us to accept the input string from the user without evaluating its value. Until it encounters a new line, this function continues to read input from the user.

>>> name = input('Enter your name: ')
Enter your name: Ninza7
>>> name
Ninza7

Here, the name is the variable that refers to the string value ‘Ninza7’ entered by the user. Making use of a function is called calling the function. The above statement ‘Enter your name: ’ is called an argument. So, we can say that the function input is called with an argument ‘Enter your name: ’. Further, the string entered by the user is assigned to the variable.

eval() Function

The function eval() is used to evaluate the value of a string.

>>> eval('7')
7
>>> eval('7+7')
14

round() Function

The function round() is used to round off the numbers. You can specify the decimal places, too. The function will round off up to specified decimal places.

>>> print(round(89.92345,2)), round(89.725))
89.92 90

type() Function

Python function type() tells us about the type of values. Values or objects in Python are classified into types or classes, e.g., 7 is an integer, 7.5 is a floating number, and ‘hello’ is a string.

>>> print(type(7), type(7.5), type('hello'), type(int))
print(type(7), type(7.5), type('hello'), type(int))

min() and max() Function

The min() and max() function is used to find the minimum and maximum values respectively from the several values. This function also operates on the string value.

>>> max(7,22,733,56)
733
>>> min(3,3663,8727,82)
3
>>> max('hello', 'how', 'are', 'you')
'you'
>>> min('hello', 'how', 'are', 'you','Sir')
'Sir'

Remember one thing that you can’t compare the numeric values with string values.

pow() Function

The function pow(a,b) computes a raise to the power b.

>>> pow(5,3)
125

Math Module Functions

  • ceil(x): Returns the smallest integer greater than or equal to x
  • floor(x): Returns the largest integer less than or equal to x
  • fabs(x): Returns the absolute value of x
  • exp(x): Returns the value of expression e**x
  • log(a,b): Returns the log(a) to the base b. In the case of the absence of the second argument, the logarithmic value of a to the base b is returned.
  • log(10, x): Returns the log(x) to the base 10.
  • pow(x,y): Returns x raised to the power y.
  • sqrt(x): Returns the square root of x
  • cos(x): Returns the cosine of x radians
  • sin(x): Returns the sine of x radians
  • tan(x): Returns the tangent of x radians
  • acos(x): Return the inverse cosine of x in radians
  • asin(x): Returns the inverse sine of x in radians
  • atan(x): Returns the inverse tangent of x in radians
  • degrees(x): Returns the value in degree equivalent of the input value x in radians
  • radians(x): Returns a value in radian equivalent of the input value x in degrees

Here are a few examples:

Math function module example in Python

Generation of Random Numbers

The random module is used to generate random integers in python. It provides a randint() function that randomly chooses an integer in the specified range. For example:

Use of random function in Python

As you can see that, each time you run the Python statement it shows different values within the specified range.

help() Function

Let’s suppose that you want to use the cos function but you don’t know the procedure or you just forgot. Then the function help() will help you come out of the situation. For example:

>>> import math
>>> help(math.tan)
Help on built-in function tan in module math:tan(x, /)Return the tangent of x (measured in radians).

Well, that’s it for this article. In the next article, we will talk more about Python functions.

If this article sounds informative to you then make sure to follow and share it with your geek community.

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).

More content at plainenglish.io

--

--