In this python tutorial, you will learn about the different Python built-in functions with a few examples. There are various built-in functions that can be used in Python. Here we will check:
- Python absolute value
- Python type function
- Python any function
- Python round function
- Python object function
- Python set function
- Python character function
- Python getattr function
- Python sorted function
- Python reversed function
- Python all function
- Python range function
- Python ord() function
- Python vars() function
- Python eval() function
- Python pow() function
- Python input() function
- Python next() function
- Round to two decimal places in python
- Python globals() function
Python built-in functions
Now, let us see the Python built-in functions with some examples.
- Python naming conventions
- String methods in Python with examples
- Python While Loop Example
- String methods in Python with examples
- Python Array with Examples
- Python exit command (quit(), exit(), sys.exit())
- Python binary search and linear search
Python absolute() value function
In python, to get the absolute value we have built-in function abs(). The absolute value of a number is a value without considering its sign.
Example:
value = abs(-3.7)
print(value)
After writing the above code (python absolute value function), Ones you will print “value” then the output will appear as a “ 3.7 “. Here, the abs() function is used to give absolute value by removing the sign.
You can refer to the below screenshot for python absolute value function.
Python type() function
In python, the type() function is used to return the type of the specified object.
Example:
value = "Hello"
x = type(value)
print(x)
After writing the above code (python type function), Ones you will print “ x ” then the output will appear as a “ <class ‘str’> “. Here, the type() function returns the type of the object so, it is of string type.
You can refer to the below screenshot for python type function
Python any() function
In python, any() function is used to check if any item present in the list is true then it will returns true, otherwise it will return false. This is another Python built-in functions that we mostly use.
Example:
value = [True, False, False]
check = any(value)
print(check)
After writing the above code (python any function), Ones you will print “ check ” then the output will appear as a “ True “. Here, any() function is used to check whether items present have true value in the list or not, if it is false then it will return false.
You can refer to the below screenshot for python any function
Python round() function
In the list of Python built-in functions, another function as round() function. round() function in Python is used to return a floating point number that is rounded of a specified number and the function will return the nearest integer.
Example:
value = round(22.34567)
print(value)
After writing the above code (python round function), Ones you will print “ value ” then the output will appear as a “ 22 “. Here, the round() function is used to return the floating number which will be rounded of the specified number of decimals.
You can refer to the below screenshot for python round function
- NameError: name is not defined in Python
- Python check if the variable is an integer
- ValueError: math domain error
- Check if a number is a prime Python
- Python GUI Programming
Python object() function
In python, object() function returns a empty object and it doesn’t take any parameters. This object is base for all class and it has built-in properties which is default for all class.
Example:
value = object()
print(dir(value))
After writing the above code (python round function), Ones you will print “ dir(value) ” then the output will appear. Here, the object() function is used to return the object which is base for all class and dir() is used for getting all the attributes.
You can refer to the below screenshot for python object function
Python set() function
Python set() function is used to create a set in which elements list are unordered, it will be in random form and it removes the duplicate items.
Example:
value = set(('Alice', 'Mark', 'Andrew'))
print(value)
After writing the above code (python set function), Ones you will print “ value ” then the output will appear as a “ {‘Marks’, ‘Alice’, ‘Andrew’} “. Here, the set() function is used for creating a set in which elements are unordered.
You can refer to the below screenshot for python set function
Python character() function
We can use Python chr() function, to get the characters that represent the specified Unicode in python. This is another popular Python built-in functions.
Example:
value = chr(97)
print(value)
After writing the above code (python character function), Ones you will print “ value ” then the output will appear as an “ a “. Here, the chr() function is used for getting the character of the specified Unicode. So, Unicode ” 97 ” is for ” a “.
You can refer to the below screenshot for python character function
Python getattr() function
Python getattr() function is used to get the value of the specified attribute from the object, and getattr() can take two parameters.
Example:
class Employee:
Name = 'Aahana'
Age =36
Salary = 15000
value = getattr(Employee, 'Salary')
print(value)
After writing the above code (python getattr function), Ones you will print “ value ” then the output will appear as a “ 15000 “. Here, the getattr() function is used for getting the specified value from the object, and ” Salary ” is specified attribute.
You can refer to the below screenshot for python getattr function
Python sorted function
Python sorted() function is used to sort the elements by default in ascending order and the result will be in sorted order. Check an example of Python built-in functions and this time it is the sorted() function.
Example:
value = (15, 12, 10, 11, 14, 13)
s = sorted(value)
print(s)
After writing the above code (python sorted function), Ones you will print “ s ” then the output will appear as a “ [10, 11, 12, 13, 14, 15] “. Here, the sorted() function sorted the numerical items in ascending order.
You can refer to the below screenshot for python sorted function
Also, if you want to sort the list in descending order then we will use sorted() function and reverse parameters, if the reverse is true then it will sort in descending order.
value = (15, 12, 10, 11, 14, 13)
s = sorted(value, reverse=True)
print(s)
After writing the above code (python sorted function), Ones you will print “ s ” then the output will appear as a “ [10, 11, 12, 13, 14, 15] “. Here, the sorted() function with parameters “reverse=True” will sort the numerical items in descending order.
You can refer to the below screenshot for python sorted function
Python reversed function
In python, reversed() function is used to reverse the elements present in the list.
Example:
value = ["tom", "alice", "sid", "zoe"]
store = reversed(value)
for a in store:
print(a)
After writing the above code (python reversed function), Ones you will print “ a ” then the output will appear as a “ zoe, sid, alice, tom “. Here, the reversed() function is used to reverse the sequence of the item.
You can refer to the below screenshot for python reversed function
Python all function
In python, the built-in all() function will return true if all the elements are true otherwise it will return false if any of the elements is false.
Example:
value = [11, 12, 13, 14, 15]
print(all(value))
After writing the above code (python all function), Ones you will print “ all() ” then the output will appear as a “ True “. Here, the all() function is used to check all elements in iterable are true or not.
You can refer to the below screenshot for python all function
Also we will check, if any of the element is false then the all() function will return false.
Example:
value = [0, 12, 13, 14, 15]
print(all(value))
After writing the above code (python all function), Ones you will print “ all() ” then the output will appear as a “ False “. Here, the all() function will check and it will find one false element which is “0” and it will return false.
You can refer to the below screenshot for python all function
Python range function
In python, the built-in range() function is used to return a sequence of number, by default it will start from 0 and it will stop just before the mentioned range.
Example:
value = range(10)
for number in value:
print(number)
After writing the above code (python range function), Ones you will print “ number ” then the output will appear as a “ 0 1 2 3 4 5 6 7 8 9 “. Here, the range() function will return a number from 0 and it will stop before the mentioned range by default.
You can refer to the below screenshot for python range function
Python ord() function
The ord() function returns an integer representing the Unicode character and the ord() function takes a single parameter.
Example:
x = ord("B")
print(x)
After writing the above code (python ord() function), Ones you will print “ x ” then the output will appear as a “ 66 “. Here, the ord() function will return the Unicode code point value for the character ‘B’.
You can refer to the below screenshot for python ord() function
Python vars() function
In python, the vars() function returns the __dict__ attribute of the given object.
Example:
class Teacher:
def __init__(self, x = 15, y = 18):
self.x = x
self.y = y
t1 = Teacher()
print(vars(t1))
After writing the above code (python vars() function), Ones you will print “ vars(t1)” then the output will appear as a “ {‘x’:15, ‘y’:18} “. Here, the vars() function will return the dictionary and it stores the object attributes.
You can refer to the below screenshot for python vars() function
Python eval() function
The eval() is a built-in function that evaluates the expression passed to the method and if the expression is legal then it will execute.
Example:
a = 1
print(eval('a + 10'))
After writing the above code (python eval() function), Ones you will print “ eval(‘a + 10’) ” then the output will appear as an ” 11 “. Here, the eval() returns the result evaluated from the expression.
You can refer to the below screenshot for python eval() function
Python pow() function
The pow() is a built-in function that returns the power of the number. The pow() function takes three parameters, pow(x, y, z) where “x” is the base, “y” is exponent, and “z” is used for modulus.
Example:
a = pow(5, 3)
print(a)
After writing the above code (python pow() function), Ones you will print “ a ” then the output will appear as a ” 125 “. Here, the pow() returns the value of 5 to the power of 3 which gives 125.
You can refer to the below screenshot for python pow() function
Python input() function
The input() is built-in function which allows user input and print it.
Example:
a = input("Enter a string: ")
print("Welcome, " + a)
After writing the above code (python input() function), Ones you will print then the output will appear as a ” Welcome, Student “. Here, the input() function will allow the user to give input, and then it gets printed.
You can refer to the below screenshot for python input() function
Python next() function
The next() function returns the next item in an iterator. A list is iterable and we get its iterator from it by using the iter() function in python.
Example:
f_list = iter(["rose","tulip"])
s = next(f_list)
print(s)
s = next(f_list)
print(s)
After writing the above code (python next() function), Once you will print ” s “ then the output will appear as a ” rose tulip “. Here, the next() function returns the next item from the iterator and it will print the item one by one.
You can refer to the below screenshot for python next() function
Round to two decimal places in python
We will use round(numbers, ndigits) with a float as number and ndigits to round the float to two decimal places in python.
Example:
my_float = 5.13169
float_v = round(my_float, 2)
print(float_v)
After writing the above code (round to two decimal places in python), Ones you will print “ float_v ” then the output will appear as a “ 5.13 “. Here, the round() function is used to limit a float to two decimal places in python.
You can refer to the below screenshot for python round to two decimal places in python
Python globals() function
The globals() function returns the dictionary of the current global symbol table. Symbol tables contains all necessary information of the current program and it includes methods, variable names, etc.
s = globals()
print(s)
After writing the above code (python globals() function), Ones you will print “ s ” then the output will appear. Here, the globals() method doesn’t take any parameters and it returns the dictionary of the current global symbol table.
You can refer to the below screenshot for python globals() function
This is how we can use built-in functions in python.
You can check out the below Python tutorials:
- Get current directory Python
- Syntaxerror return outside function python
- Remove character from string Python
- How to handle indexerror: string index out of range in Python
- Remove Unicode characters in python
- Python convert list to string
- Python String Functions
- How to convert an integer to string in python
- How to concatenate strings in python
In this tutorial, we learned how to use various built-in functions in python and also we saw Python built-in functions examples.
- Python absolute value
- Python type function
- Python any function
- Python round function
- Python object function
- Python set function
- Python character function
- Python getattr function
- Python sorted function
- Python reversed function
- Python all function
- Python range function
- Python ord() function
- Python vars() function
- Python eval() function
- Python pow() function
- Python input() function
- Python next() function
- Round to two decimal places in python
- Python globals() function
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.