Python Built-In Functions with Syntax and Examples

In this article will talk about Python Built-In Functions, We’ll see how basic statements may be combine into functions to do useful tasks.

As an issue grows more complicated, so does the program, and a programmer can’t keep track of all the data and control instructions in the program. They can, however, deal with issues whose answers can be represent as little Python programs, with the solutions found as functions and then combined to build the final program.

The sub-programs can be broken down into smaller issues if necessary. It can be continue to any level that is acceptable. The progressive refining technique, often known as the modular approach, is a problem-solving strategy.

Python Built-in Functions

Predefined functions that are already accessible in Python are known as built-in functions. It has already been made available to programmers in Python.

print() Function (Python Built-in Functions)

>>> print("Hello World!")

When you run the above program in Python IDLE, you will get this result: Hello World!

The expression print(“Hello World!”) invokes the print() function, which takes the input “Hello World!”.  The print function prints the value of the string that contains the sequence of characters in “Hello World!” without any apostrophe marks.

You may also use the print() function to add comma-separated integers.

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

Note: When numerous values are separated by commas in a call to the print() function, they are shown on a single line with single spaces between them.

>>> x='World!'
>>> print('Hello', x, '3+3=' 3+3)
Hello World! 3+3= 6

The characters with a backslash \ have a unique meaning. The character sequence \n acts as a line feed character, transferring print control to the first character of the line. Escape sequences are character sequences such as \n and \t.

>>> print('Hello', x, '\n3+3=', 3+3)
Hello World!
3+3= 6
>>> print('Hello', x, '\t3+3=', 3+3)
Hello World!    3+3= 6

If you want to include \n in the print function but don’t want your Python statement to terminate on a new line, use a double slash to precede it. The alternative option is to use R or r before the escape symbol-containing string.

>>> 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 (Python Built-in Functions)

We may accept a user’s input string without assessing its value using the input() function. This function continues to read user input until it meets a new line.

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

The name is a variable that corresponds to the string value ‘Ranjeet’ that the user has provided. Calling a function is the process of executing it. The sentence ‘Enter your name:’ is refer to as an argument. So, with the parameter ‘Enter your name:’, we can claim that the function input is call. In addition, the user’s string is assign to the variable.

eval() Function (Python Built-in Functions)

The eval() function is use to determine the value of a string.

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

round() Function

The round() function is use to round integers. You may also select the number of decimal places. The function will round up to the number of decimal places you provide.

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

type() Function (Python Built-in Functions)

type() is a Python function that returns the type of a value. In Python, values or objects are categorized as integers, floating numbers, or strings. For example, 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))
<class 'int'> <class 'float'> <class 'str'> <class 'type'>

min() and max() Function (Python Built-in Functions)

The min() and max() functions are used to discover the least and maximum values from a set of numbers, respectively. The string value is also used by this function.

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

One thing to keep in mind is that numeric and string values cannot be compared.

pow() Function (Python Built-in Functions)

Pow(a,b) is a function that computes a raise to the power of b.

>>> pow(5,3)

Math Module Functions (Python Built-in Functions)

  • ceil(x): The lowest integer bigger than or equal to x is returned.
  • floor(x): It returns the largest integer less than or equal to x
  • fabs(x): Returns the absolute value of x
  • exp(x): It 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.
  • log10(x): It returns the log(x) to the base 10.
  • pow(x,y): Returns x raised to the power y.
  • sqrt(x): It returns the square root of x
  • cos(x): Returns the cosine of x radians
  • sin(x): It returns the sine of x radians
  • tan(x): Returns the tangent of x radians
  • acos(x): It return the inverse cosine of x in radians
  • asin(x): Returns the inverse sine of x in radians
  • atan(x): IIt 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): It returns a value in radian equivalent of the input value x in degrees

ceil() Function

>>> import math
>>> math.ceil(6.7)

floor() Function

>>> import math
>>> math.floor(6.7)

fabs() Function

>>> import math
>>> math.fabs(-44)

exp() Function

>>> import math
>>> math.exp(3)

log() Function

>>> import math
>>> math.log(10, 1000)

log10() Function

>>> import math
>>> math.log10(10000)

pow() Function

>>> import math
>>> math.pow(4,4)

sqrt() Function

>>> import math
>>> math.sqrt(81)

cos() Function

>>> import math
>>> math.cos(math.pi/2)

sin() Function

>>> import math
>>> math.sin(math.pi/2)

tan() Function

>>> import math
>>> math.tan(math.pi/2)

acos() Function

>>> import math
>>> math.acos(math.pi/2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

asin() Function

>>> import math
>>> math.asin(math.pi/2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

atan() Function

>>> import math
>>> math.atan(math.pi/2)

degrees() Function

>>> import math
>>> math.degrees(3)

radians() Function

>>> import math
>>> math.radians(180)

Generation of Random Numbers

In Python, the random module is used to create random numbers. It has a randint() method that selects an integer at random from the provided range. For example:

>>> import random
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))
>>> print(random.randint(1,100))

As you can see, the Python statement displays different numbers inside the defined range each time it is executed.

help() Function

Let’s say you want to utilize the tan function, but you don’t know how to use it or have forgotten. The function help() will then assist you in getting out of the issue. 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).

If you want to learn more about python then visit the official documentation of python.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]

If you want to learn more about the python programming language then click the link to see our last article Python Strings with Examples

Leave a Comment