Built-In Functions in Python
Built-In Functions in Python
Built in Functions
1. abs()
The abs() is one of the most popular Python built-in functions, which returns the
absolute value of a number.
>>> abs(-7)
Output
7
2. bin()
The bin() method converts and returns the binary equivalent string of a given
integer.
Output
bool() parameters
It's not mandatory to pass a value to bool(). If you do not pass a value, bool() returns
False.
• None
• False
• Zero of any numeric type. For example, 0, 0.0, 0j
• Empty sequence. For example, (), [], ''.
• Empty mapping. For example, {}
test = []
print(test,'is',bool(test))
test = [0]
print(test,'is',bool(test))
test = 0.0 Output
print(test,'is',bool(test))
test = None [] is False
print(test,'is',bool(test)) [0] is True
test = True 0.0 is False
print(test,'is',bool(test)) None is False
test = 'Easy string' True is True
print(test,'is',bool(test)) Easy string is True
4. chr()
The chr() method returns a character (a string) from an integer (represents
unicode code point of the character).
Example :
print(chr(97))
print(chr(65))
Output
a
A
5. ord()
The ord() function returns an integer representing the Unicode character.
Example:
print(ord('5')) # 53
print(ord('A')) # 65
print(ord('$')) # 36
Output
53
65
36
6. complex()
The complex() method returns a complex number when real and imaginary parts are
provided, or it converts a string to a complex number.
Example :
z = complex(2, -3)
print(z)
z = complex(1)
print(z)
z = complex() Output
print(z)
z = complex('5-9j') (2-3j)
print(z) (1+0j)
0j
(5-9j)
7. dict()
The dict() constructor creates a dictionary in Python.
Example :
numbers = dict(x=5, y=0)
print('numbers =', numbers)
print(type(numbers))
Example: Output
print('divmod(8, 3) = ', divmod(8, 3))
print('divmod(3, 8) = ', divmod(3, 8)) divmod(8, 3) = (2, 2)
print('divmod(5, 5) = ', divmod(5, 5)) divmod(3, 8) = (0, 3)
divmod(5, 5) = (1, 0)
# divmod() with Floats divmod(8.0, 3) = (2.0, 2.0)
print('divmod(8.0, 3) = ', divmod(8.0, 3)) divmod(3, 8.0) = (0.0, 3.0)
print('divmod(3, 8.0) = ', divmod(3, 8.0)) divmod(7.5, 2.5) = (3.0, 0.0)
print('divmod(7.5, 2.5) = ', divmod(7.5, 2.5))
9. eval()
The eval() method parses the expression passed to this method and runs python
expression (code) within the program.
Example :
x=1
print(eval('x + 1'))
Output
2
10. exec()
The exec() method executes the dynamically created program, which is either a string
or a code object.
Example :
program = 'a = 5\nb=10\nprint("Sum =", a+b)'
exec(program)
Output
Sum = 15
11. float()
The float() method returns a floating point number from a number or a string.
Example :
# for integers
print(float(10))
# for floats Output
print(float(11.22))
# for string floats 10.0
print(float("-13.33")) 11.22
# for string floats with whitespaces -13.33
print(float(" -24.45")) -24.45
# string float error ValueError: could not convert string to float:
print(float("abc")) 'abc'
12. format()
The built-in format() method returns a formatted representation of the given value
controlled by the format specifier.
Example:
>>> help(list)
>>> help(dict)
>>> help(print)
14. hex()
The hex() function converts an integer number to the corresponding hexadecimal
string.
Example :
number = 435
print(number, 'in hex =', hex(number))
number = 0
print(number, 'in hex =', hex(number)) Output
inputString = input()
print('The inputted string is:', inputString)
Output
Python is interesting.
The inputted string is: Python is interesting
Example 2: Get input from user with a prompt
# get input from user
Output
testString = ''
print('Length of', testString, 'is', len(testString))
testString = 'Python'
print('Length of', testString, 'is', len(testString))
Output
Length of is 0
Length of Python is 6
Example 3: How len() works with dictionaries and sets
testSet = {1, 2, 3}
print(testSet, 'length is', len(testSet))
# Empty Set
testSet = set()
print(testSet, 'length is', len(testSet))
testDict = {1: 'one', 2: 'two'}
print(testDict, 'length is', len(testDict))
testDict = {}
print(testDict, 'length is', len(testDict))
Output
{1, 2, 3} length is 3
set() length is 0
{1: 'one', 2: 'two'} length is 2
{} length is 0
17. list()
The list() constructor returns a list in Python.
# vowel dictionary
vowel_dictionary = {'a': 1, 'e': 2, 'i': 3, 'o':4, 'u':5}
print(list(vowel_dictionary))
Output
Note: In the case of dictionaries, the keys of the dictionary will be the items of the list. Also, the order
of the elements will be random.
18. max()
The Python max() function returns the largest item in an iterable. It can also be used to
find the largest item between two or more parameters.
Output
Output
Output
Output
Example :
# decimal to octal
print('oct(10) is:', oct(10))
# binary to octal
print('oct(0b101) is:', oct(0b101))
# hexadecimal to octal
print('oct(0XA) is:', oct(0XA))
Output
oct(10) is: 0o12
oct(0b101) is: 0o5
oct(0XA) is: 0o12
21. pow()
The pow() function returns the power of a number.
Example :
# positive x, positive y (x**y)
print(pow(2, 2)) # 4
# negative x, positive y Output
print(pow(-2, 2)) # 4 4
# positive x, negative y 4
print(pow(2, -2)) # 0.25 0.25
# negative x, negative y 0.25
print(pow(-2, -2)) # 0.25
Example 2: pow() with three arguments (x**y) % z
x=7
y=2
z=5
print(pow(x, y, z)) # 4
Output
4
Here, 7 powered by 2 equals 49. Then, 49 modulus 5 equals 4.
22. print()
The print() function prints the given object to the standard output device (screen) or
to the text stream file.
Example 1: print("Python is fun.")
a=5
# Two objects are passed
print("a =", a)
b=a
# Three objects are passed
print('a =', a, '= b')
Output
Python is fun.
a=5
a=5=b
Example 2: print() with separator and end parameters
a=5
print("a =", a, sep='00000', end='\n\n\n')
print("a =", a, sep='0', end='')
Output
a =000005
a =05
23. range()
The range() type returns an immutable sequence of numbers between the given
start integer to the stop integer.
range() Parameters
range() takes mainly three arguments having the same use in both definitions:
# using range(stop)
print(list(range(10)))
Output
[]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 2: Create a list of even number between the given numbers using
range()
start = 2
stop = 14
step = 2
Output
Output
# vowels list
py_list = ['e', 'a', 'u', 'o', 'i']
print(sorted(py_list))
Output
# set
py_set = {'e', 'a', 'u', 'o', 'i'}
print(sorted(py_set, reverse=True))
Output
Output
4.5
29. type()
The type() function either returns the type of the object or returns a new type object
based on the arguments passed.
<class 'list'>
<class 'dict'>
Example 2:
a = ('apple', 'banana', 'cherry')
b = "Hello World"
c = 33
x = type(a)
y = type(b)
z = type(c) Output:
<class 'tuple'>
print(x) <class 'str'>
print(y) <class 'int'>
print(z)
30. tuple()
The tuple() builtin can be used to create tuples in Python.
A tuple is an immutable sequence type. One of the ways of creating tuple is by
using the tuple() construct.
Example: Create tuples using tuple()
t1 = tuple()
print('t1 =', t1)