Python Built-ins Reference
v1.0.0Searchable reference for Python built-in functions, type conversions, iteration tools, and I/O.
34 entries found
print(*objects, sep=' ', end='\n', file=sys.stdout)
Prints objects to the text stream.
print('Hello', 'World', sep=', ')len(s)
Returns the number of items in a container.
len([1, 2, 3]) # 3
range(stop) / range(start, stop[, step])
Returns an immutable sequence of numbers.
list(range(0, 10, 2)) # [0, 2, 4, 6, 8]
type(object)
Returns the type of an object.
type(42) # <class 'int'>
isinstance(object, classinfo)
Returns True if object is an instance of classinfo.
isinstance(42, int) # True
repr(object)
Returns a string containing a printable representation.
repr([1, 2]) # '[1, 2]'
id(object)
Returns the identity (memory address) of an object.
id(x)
dir([object])
Returns a list of names in scope or object's attributes.
dir([]) # All list methods
vars([object])
Returns the __dict__ of an object.
vars(obj) # {'x': 1, 'y': 2}help([object])
Invokes the built-in help system.
help(str.split)
int(x=0, base=10)
Converts to integer.
int('42') # 42float(x=0.0)
Converts to float.
float('3.14') # 3.14str(object='')
Converts to string.
str(42) # '42'
bool(x=False)
Converts to boolean.
bool(0) # False; bool(1) # True
list(iterable=())
Creates a list from an iterable.
list('abc') # ['a', 'b', 'c']tuple(iterable=())
Creates a tuple from an iterable.
tuple([1, 2, 3]) # (1, 2, 3)
set(iterable=())
Creates a set from an iterable.
set([1, 2, 2, 3]) # {1, 2, 3}dict(**kwargs) / dict(mapping)
Creates a dictionary.
dict(a=1, b=2) # {'a': 1, 'b': 2}bytes(source, encoding)
Returns an immutable bytes object.
bytes('hello', 'utf-8')chr(i) / ord(c)
Converts between Unicode code points and characters.
chr(65) # 'A'; ord('A') # 65enumerate(iterable, start=0)
Returns (index, value) pairs.
for i, v in enumerate(['a','b']): ...
zip(*iterables, strict=False)
Zips multiple iterables together.
list(zip([1,2], ['a','b'])) # [(1,'a'),(2,'b')]
map(function, *iterables)
Applies function to every item.
list(map(str.upper, ['a','b'])) # ['A','B']
filter(function, iterable)
Filters items where function returns True.
list(filter(lambda x: x > 0, [-1, 0, 1])) # [1]
sorted(iterable, key=None, reverse=False)
Returns a new sorted list.
sorted([3,1,2]) # [1, 2, 3]
reversed(seq)
Returns a reverse iterator.
list(reversed([1, 2, 3])) # [3, 2, 1]
any(iterable) / all(iterable)
Tests if any / all elements are truthy.
any([False, True]) # True; all([True, 1]) # True
sum(iterable) / min(iterable) / max(iterable)
Aggregate functions for iterables.
sum([1,2,3]) # 6; max([1,2,3]) # 3
hasattr(obj, name) / getattr(obj, name[, default]) / setattr(obj, name, value)
Inspect and modify object attributes dynamically.
getattr(obj, 'x', 0) # 0 if missing
callable(object)
Returns True if the object appears callable.
callable(len) # True
super([type[, object-or-type]])
Returns a proxy object for the parent class.
super().__init__()
@property
Defines managed attributes with getter/setter/deleter.
@property def name(self): return self._name
open(file, mode='r', encoding=None)
Opens a file and returns a file object.
with open('f.txt', 'r', encoding='utf-8') as f: ...input(prompt='')
Reads a string from stdin.
name = input('Enter name: ')