- Python Built-in Functions
- print() Function (Python Built-in Functions)
- input() Function (Python Built-in Functions)
- eval() Function (Python Built-in Functions)
- round() Function
- type() Function (Python Built-in Functions)
- min() and max() Function (Python Built-in Functions)
- pow() Function (Python Built-in Functions)
- Math Module Functions (Python Built-in Functions)
- Generation of Random Numbers
- help() Function
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
Ranjeet
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')
7
>>> eval('7+7')
14
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)
733
>>> min(3,3663,8727,82)
3
>>> max('hello', 'how', 'are', 'you')
'you'
>>> min('hello', 'how', 'are', 'you', 'Sir')
'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)
125
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 tox
fabs(x)
: Returns the absolute value ofx
exp(x)
: It returns the value of expressione**x
log(a,b)
: Returns the log(a
) to the baseb
. In the case of the absence of the second argument, the logarithmic value ofa
to the baseb
is returned.log10(x)
: It returns the log(x
) to the base10
.pow(x,y)
: Returnsx
raised to the powery
.sqrt(x)
: It returns the square root ofx
cos(x)
: Returns the cosine ofx
radianssin(x)
: It returns the sine ofx
radianstan(x)
: Returns the tangent ofx
radiansacos(x)
: It return the inverse cosine ofx
in radiansasin(x)
: Returns the inverse sine ofx
in radiansatan(x)
: IIt returns the inverse tangent ofx
in radiansdegrees(x)
: Returns the value in degree equivalent of the input valuex
in radiansradians(x)
: It returns a value in radian equivalent of the input valuex
in degrees
ceil() Function
>>> import math
>>> math.ceil(6.7)
7
floor() Function
>>> import math
>>> math.floor(6.7)
6
fabs() Function
>>> import math
>>> math.fabs(-44)
44.0
exp() Function
>>> import math
>>> math.exp(3)
20.085536923187668
log() Function
>>> import math
>>> math.log(10, 1000)
0.33333333333333337
log10() Function
>>> import math
>>> math.log10(10000)
4.0
pow() Function
>>> import math
>>> math.pow(4,4)
256.0
sqrt() Function
>>> import math
>>> math.sqrt(81)
9.0
cos() Function
>>> import math
>>> math.cos(math.pi/2)
6.123233995736766e-17
sin() Function
>>> import math
>>> math.sin(math.pi/2)
1.0
tan() Function
>>> import math
>>> math.tan(math.pi/2)
1.633123935319537e+16
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)
1.0038848218538872
degrees() Function
>>> import math
>>> math.degrees(3)
171.88733853924697
radians() Function
>>> import math
>>> math.radians(180)
3.141592653589793
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))
54
>>> print(random.randint(1,100))
47
>>> print(random.randint(1,100))
97
>>> print(random.randint(1,100))
37
>>> print(random.randint(1,100))
5
>>> print(random.randint(1,100))
26
>>> print(random.randint(1,100))
76
>>> print(random.randint(1,100))
47
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