Python Built-in Functions
Python built-in functions are functions whose functionality is pre-defined and that are always available.
Built-in Functions
There are several built-in functions in Python.
See built-in functions reference for a complete and detailed list of available 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)) # 16
Output:
1
1
1.23
1.23
2
16
ascii()
The ascii()
function returns a string with a printable representation of an object.
ascii()
escapes the non-ASCII characters in the string
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) # 123
Output
<class 'int'>
123
<class 'int'>
-3
<class 'int'>
123
len()
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([])) # 0
Output
18
3
0
max()
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))) # 100
Output
100
100
min()
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))) # -5
Output
-5
-5
print()
The print()
function print objects to the text stream file, separated by sep
separator and followed by end
string.
sep
, end
, file
, and flush
, if present, must be given as keyword arguments.
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-