Python built-in functions are functions whose functionality is pre-defined and that are always available.
Built-in Functions
Let's see some of the most common built-in functions.
abs()
The abs() function returns the absolute value of a number.
print(abs(1)) # 1
print(abs(-1)) # 1
print(abs(1.23)) # 1.23
print(abs(-1.23)) # 1.23
print(abs(0b10)) # 2
print(abs(0o20)) # 16Output:
1
1
1.23
1.23
2
16ascii()
The ascii() function returns a string with a printable representation of an object.
print(ascii('A')) # 'A'
print(ascii('a')) # 'a'
print(ascii('è')) # '\xe8'
print(ascii(['A', 'è'])) # ['A', '\xe8']Output:
'A'
'a'
'\xe8'
['A', '\xe8']int()
The int() function returns an integer object constructed from a number or string.
from_integer = int(123)
print(type(from_integer)) # <class 'int'>
print(from_integer) # 123
from_float = int(-3.14)
print(type(from_float)) # <class 'int'>
print(from_float) # -3
from_string = int("123")
print(type(from_string)) # <class 'int'>
print(from_string) # 123Output
<class 'int'>
123
<class 'int'>
-3
<class 'int'>
123len()
The len() function returns the length (i.e. the number of items) of an object.
print(len('Tutorial Reference')) # 18
print(len(['a string', 10, 1.23])) # 3
print(len([])) # 0Output
18
3
0max()
The max() function returns the largest item in an iterable.
# max() with Lists
print(max([1,10, 100, 20, -5])) # 100
# max() with Tuples
print(max((1,10, 100, 20, -5))) # 100Output
100
100min()
The min() function returns the smallest item in an iterable.
# min() with Lists
print(max([1,10, 100, 20, -5])) # -5
# min() with Tuples
print(max((1,10, 100, 20, -5))) # -5Output
-5
-5print()
The print() function print objects to the text stream file, separated by sep separator and followed by end string.
print('Hello world!') # Hello world!
name = 'Tom'
print('Hello,', name) # Hello, Tom
print('red', 'green', 'blue', sep='@') # red@green@blue
print('red', end=':)') # red:)
print('red', 'red', end=':)') # red red:)
colors = ['red', 'gree', 'blue', 'yellow', 'violet']
for color in colors:
print(color, end='-')
# red-gree-blue-yellow-violet-Output
Hello world!
Hello, Tom
red@green@blue
red:)
red red:)
red-gree-blue-yellow-violet-