Skip to main content

Python Built-ins Reference

v1.0.0

Searchable reference for Python built-in functions, type conversions, iteration tools, and I/O.

34 entries found

printBuilt-ins

print(*objects, sep=' ', end='\n', file=sys.stdout)

Prints objects to the text stream.

print('Hello', 'World', sep=', ')
lenBuilt-ins

len(s)

Returns the number of items in a container.

len([1, 2, 3])  # 3
rangeBuilt-ins

range(stop) / range(start, stop[, step])

Returns an immutable sequence of numbers.

list(range(0, 10, 2))  # [0, 2, 4, 6, 8]
typeBuilt-ins

type(object)

Returns the type of an object.

type(42)  # <class 'int'>
isinstanceBuilt-ins

isinstance(object, classinfo)

Returns True if object is an instance of classinfo.

isinstance(42, int)  # True
reprBuilt-ins

repr(object)

Returns a string containing a printable representation.

repr([1, 2])  # '[1, 2]'
idBuilt-ins

id(object)

Returns the identity (memory address) of an object.

id(x)
dirBuilt-ins

dir([object])

Returns a list of names in scope or object's attributes.

dir([])  # All list methods
varsBuilt-ins

vars([object])

Returns the __dict__ of an object.

vars(obj)  # {'x': 1, 'y': 2}
helpBuilt-ins

help([object])

Invokes the built-in help system.

help(str.split)
intType Conversions

int(x=0, base=10)

Converts to integer.

int('42')  # 42
floatType Conversions

float(x=0.0)

Converts to float.

float('3.14')  # 3.14
strType Conversions

str(object='')

Converts to string.

str(42)  # '42'
boolType Conversions

bool(x=False)

Converts to boolean.

bool(0)  # False; bool(1)  # True
listType Conversions

list(iterable=())

Creates a list from an iterable.

list('abc')  # ['a', 'b', 'c']
tupleType Conversions

tuple(iterable=())

Creates a tuple from an iterable.

tuple([1, 2, 3])  # (1, 2, 3)
setType Conversions

set(iterable=())

Creates a set from an iterable.

set([1, 2, 2, 3])  # {1, 2, 3}
dictType Conversions

dict(**kwargs) / dict(mapping)

Creates a dictionary.

dict(a=1, b=2)  # {'a': 1, 'b': 2}
bytesType Conversions

bytes(source, encoding)

Returns an immutable bytes object.

bytes('hello', 'utf-8')
chr / ordType Conversions

chr(i) / ord(c)

Converts between Unicode code points and characters.

chr(65)  # 'A';  ord('A')  # 65
enumerateIteration

enumerate(iterable, start=0)

Returns (index, value) pairs.

for i, v in enumerate(['a','b']): ...
zipIteration

zip(*iterables, strict=False)

Zips multiple iterables together.

list(zip([1,2], ['a','b']))  # [(1,'a'),(2,'b')]
mapIteration

map(function, *iterables)

Applies function to every item.

list(map(str.upper, ['a','b']))  # ['A','B']
filterIteration

filter(function, iterable)

Filters items where function returns True.

list(filter(lambda x: x > 0, [-1, 0, 1]))  # [1]
sortedIteration

sorted(iterable, key=None, reverse=False)

Returns a new sorted list.

sorted([3,1,2])  # [1, 2, 3]
reversedIteration

reversed(seq)

Returns a reverse iterator.

list(reversed([1, 2, 3]))  # [3, 2, 1]
any / allIteration

any(iterable) / all(iterable)

Tests if any / all elements are truthy.

any([False, True])  # True; all([True, 1])  # True
sum / min / maxIteration

sum(iterable) / min(iterable) / max(iterable)

Aggregate functions for iterables.

sum([1,2,3])  # 6; max([1,2,3])  # 3
hasattr / getattr / setattrObject / Class

hasattr(obj, name) / getattr(obj, name[, default]) / setattr(obj, name, value)

Inspect and modify object attributes dynamically.

getattr(obj, 'x', 0)  # 0 if missing
callableObject / Class

callable(object)

Returns True if the object appears callable.

callable(len)  # True
superObject / Class

super([type[, object-or-type]])

Returns a proxy object for the parent class.

super().__init__()
propertyObject / Class

@property

Defines managed attributes with getter/setter/deleter.

@property
def name(self): return self._name
openI/O

open(file, mode='r', encoding=None)

Opens a file and returns a file object.

with open('f.txt', 'r', encoding='utf-8') as f: ...
inputI/O

input(prompt='')

Reads a string from stdin.

name = input('Enter name: ')