Python Built-in Functions Cheatsheet
int()
Converts a value to an integer.
int('123')
→ 123
float()
Converts a value to a floating-point number.
float('3.14')
→ 3.14
str()
Converts a value to a string.
str(123)
→ '123'
bool()
Converts a value to a boolean.
bool(1)
→ True
list()
Converts an iterable to a list.
list('abc')
→ ['a', 'b', 'c']
tuple()
Converts an iterable to a tuple.
tuple([1, 2, 3])
→ (1, 2, 3)
complex()
Converts a value to a complex number.
complex(3, 4)
→ (3+4j)
chr()
Converts an integer to a character.
chr(97)
→ 'a'
ord()
Converts a character to its ASCII code.
ord('a')
→ 97
bin()
Converts an integer to its binary representation.
bin(10)
→ '0b1010'
hex()
Converts an integer to its hexadecimal representation.
hex(255)
→ '0xff'
len()
Returns the number of items in an iterable.
len([1, 2, 3])
→ 3
sum()
Returns the sum of elements in an iterable.
sum([1, 2, 3])
→ 6
sorted()
Returns a sorted list of elements from an iterable.
sorted([3, 1, 2])
→ [1, 2, 3]
any()
Returns True if any element of an iterable is true.
any([False, True, False])
→ True
all()
Returns True if all elements of an iterable are true.
all([True, True, True])
→ True
zip()
Aggregates elements from two or more iterables.
zip([1, 2], ['a', 'b'])
→
[(1, 'a'), (2, 'b')]
reversed()
Returns a reversed iterator of an iterable.
reversed([1, 2, 3])
→ [3, 2, 1]
abs()
Returns the absolute value of a number.
abs(-5)
→ 5
round()
Rounds a number to the nearest integer.
round(3.1415, 2)
→ 3.14
divmod()
Returns a tuple of the quotient and remainder when dividing two numbers.
divmod(9, 4)
→ (2, 1)
pow()
Returns the value of x to the power of y.
pow(2, 3)
→ 8
min()
Returns the smallest element from an iterable or a set of values.
min([1, 2, 3])
→ 1
max()
Returns the largest element from an iterable or a set of values.
max([1, 2, 3])
→ 3
len()
Returns the length of a string.
len('hello')
→ 5
str.lower()
Converts all characters in a string to lowercase.
'HELLO'.lower()
→ 'hello'
str.upper()
Converts all characters in a string to uppercase.
'hello'.upper()
→ 'HELLO'
str.strip()
Removes leading and trailing whitespace from a string.
' hello '.strip()
→ 'hello'
str.split()
Splits a string into a list based on a separator.
'a,b,c'.split(',')
→ ['a', 'b', 'c']
str.join()
Joins elements of a list into a string using a separator.
','.join(['a', 'b', 'c'])
→ 'a,b,c'
str.replace()
Replaces a substring in a string with another substring.
'hello'.replace('e', 'a')
→ 'hallo'
str.find()
Returns the index of the first occurrence of a substring in a string.
'hello'.find('e')
→ 1
print()
Prints the specified message to the console.
print('Hello, world!')
→ Hello, world!
repr()
Returns a string representation of an object.
repr(123)
→ '123'
format()
Formats a string using placeholders.
'Hello, {}'.format('World')
→
'Hello, World'
input()
Reads input from the user.
input('Enter name: ')
→ 'Alice'
map()
Applies a function to all items in an iterable.
map(str, [1, 2, 3])
→ ['1', '2', '3']
filter()
Filters elements from an iterable based on a function.
filter(lambda x: x % 2 == 0, [1, 2, 3, 4])
→
[2, 4]
reduce()
Applies a function cumulatively to the items in an iterable.
from functools import reduce; reduce(lambda x, y: x + y, [1, 2,
3])
→ 6
all()
Returns True if all elements of an iterable are true.
all([True, True, False])
→ False
open()
Opens a file and returns a file object.
open('file.txt', 'r')
→ file object
close()
Closes a file object.
file.close()
→ None
readlines()
Reads all the lines of a file into a list.
file.readlines()
→
['line 1', 'line 2', ...]
write()
Writes a string to a file.
file.write('Hello, world!')
raise
Raises an exception explicitly.
raise ValueError('An error occurred')
assert()
Tests if a condition is true. If false, raises an AssertionError.
assert x > 0
→ None
if True, raises
AssertionError if False.
try/except
Handles exceptions by defining a block of code to execute if an error occurs.
try: 1 / 0 except ZeroDivisionError: print('Error!')
input()
Reads input from the user.
input('Enter your name: ')
→ 'John'
id()
Returns the identity of an object.
id(123)
→ 123456789
callable()
Checks if an object appears callable (e.g., a function or method).
callable(print)
→ True
dir()
Returns the list of attributes and methods of an object.
dir('hello')
→
['__class__', '__delattr__', '__dict__', ...]