๐ Python Dictionary
Every keyword, operator, and symbol โ with plain-English explanations.
abs BUILT-IN FUNCTIONSReturn the absolute value of the argument.
abs(-10) = 10
all BUILT-IN FUNCTIONSReturns True if bool(x) is True for all elements x of the iterable.
all([True, True, True])
any BUILT-IN FUNCTIONSReturns True if bool(x) is True for any x in the iterable.
any([False, False, True])
bin BUILT-IN FUNCTIONSReturns the binary representation of an integer.
bin(10)
bool BUILT-IN FUNCTIONSReturns the Boolean value of the specified object.
bool(0)
bytes BUILT-IN FUNCTIONSReturns a new bytes object from the given object.
bytes([0x61, 0x62, 0x63])
bytearray BUILT-IN FUNCTIONSReturns a new bytearray object, a mutable sequence of integers in the range 0 <= x < 256.
bytearray([0x61, 0x62, 0x63])
callable BUILT-IN FUNCTIONSReturns whether the object is callable, that is, whether obj can be called as a function.
callable(print)
chr BUILT-IN FUNCTIONSReturns a string representing a character whose Unicode code point is the integer i.
chr(65)
compile BUILT-IN FUNCTIONSCompiles the source into a code or AST object.
compile('print("Hello")', '<string>', 'exec')len BUILT-IN FUNCTIONSReturns the number of items in an object (string, list, dict, etc.)
len([1,2,3]) # 3
len("hello") # 5complex BUILT-IN FUNCTIONSReturns a complex number with the value real + imag*1j or converts a string or number to a complex number.
complex('1+2j')print BUILT-IN FUNCTIONSPrints objects to stdout, separated by sep, ending with end
print("hello", "world") # hello world
print("x =", 42, end="\n")delattr BUILT-IN FUNCTIONSDeletes the named attribute from the specified object.
delattr(sys.modules[__name__], 'example')
range BUILT-IN FUNCTIONSReturns a sequence of numbers from start to stop (exclusive) with given step
range(5) # 0,1,2,3,4 range(1, 10, 2) # 1,3,5,7,9
dict BUILT-IN FUNCTIONSReturns a new dictionary with the specified values.
{'a': 1, 'b': 2}list BUILT-IN FUNCTIONSCreates a list from an iterable, or an empty list
list(range(3)) # [0, 1, 2]
list("abc") # ['a', 'b', 'c']dir BUILT-IN FUNCTIONSReturns a list of the attributes and keys of the specified object.
dir(str)
divmod BUILT-IN FUNCTIONSReturns the tuple (div, mod) such that a = b*div + mod and div is the quotient of a//b.
divmod(10, 3)
set BUILT-IN FUNCTIONSCreates an unordered collection of unique elements
set([1,2,2,3]) # {1, 2, 3}
set("hello") # {'h', 'e', 'l', 'o'}enumerate BUILT-IN FUNCTIONSReturns an enumerate object that produces a sequence of tuples containing indices and values of the iterable.
enumerate(['a', 'b', 'c'])
tuple BUILT-IN FUNCTIONSCreates an immutable ordered sequence from an iterable
tuple([1,2,3]) # (1, 2, 3)
tuple("abc") # ('a', 'b', 'c')eval BUILT-IN FUNCTIONSEvaluates the specified expression and returns the result.
eval('1 + 2')str BUILT-IN FUNCTIONSCreates or converts a value to a string
str(42) # "42" str([1,2,3]) # "[1, 2, 3]"
exec BUILT-IN FUNCTIONSExecutes the specified statement(s) or code object.
exec('print("Hello")')int BUILT-IN FUNCTIONSCreates or converts a value to an integer
int("42") # 42
int(3.9) # 3filter BUILT-IN FUNCTIONSConstructs an iterator from elements of an iterable for which a function returns true.
filter(lambda x: x > 0, [-1, 0, 1, 2])
float BUILT-IN FUNCTIONSConverts a string or number to a floating point number.
float('123.456')type BUILT-IN FUNCTIONSReturns the type of an object, or creates a new type
type(42) # <class 'int'>
type("hello") # <class 'str'>isinstance BUILT-IN FUNCTIONSReturns True if the object is an instance of the given class or tuple of classes
isinstance(42, int) # True
isinstance("hi", (int,str)) # Truefrozenset BUILT-IN FUNCTIONSReturns an immutable frozenset object initialized with elements from the specified iterable.
frozenset([1, 2, 3])
zip BUILT-IN FUNCTIONSAggregates elements from multiple iterables into tuples
zip([1,2,3], ["a","b","c"]) # (1,"a"), (2,"b"), (3,"c")
map BUILT-IN FUNCTIONSApplies a function to every item of an iterable and returns an iterator
list(map(str, [1,2,3])) # ["1","2","3"]
sorted BUILT-IN FUNCTIONSReturns a new sorted list from an iterable
sorted([3,1,2]) # [1, 2, 3] sorted([3,1,2], reverse=True) # [3, 2, 1]
reversed BUILT-IN FUNCTIONSReturns a reversed iterator over a sequence
list(reversed([1,2,3])) # [3, 2, 1]
sum BUILT-IN FUNCTIONSSums the items of an iterable, starting from an optional initial value
sum([1,2,3]) # 6 sum([1,2,3], 10) # 16
min BUILT-IN FUNCTIONSReturns the smallest item in an iterable or among arguments
min([3,1,2]) # 1 min(3, 1, 2) # 1
max BUILT-IN FUNCTIONSReturns the largest item in an iterable or among arguments
max([3,1,2]) # 3 max(3, 1, 2) # 3
open BUILT-IN FUNCTIONSOpens a file and returns a file object
with open("file.txt", "r") as f:
content = f.read()input BUILT-IN FUNCTIONSReads a line from stdin; optional prompt string is displayed
name = input("Enter name: ")
age = int(input("Age: "))format BUILT-IN FUNCTIONSFormats a value according to a format spec; also available as str.format()
format(3.14159, ".2f") # "3.14"
"{:.2f}".format(3.14159) # "3.14"round BUILT-IN FUNCTIONSRounds a number to a given number of digits
round(3.14159, 2) # 3.14 round(2.5) # 2
id BUILT-IN FUNCTIONSReturns the identity (unique integer) of an object
id(42) # e.g. 140234567
id("hi") # unique inthash BUILT-IN FUNCTIONSReturns the hash value of an object
hash("hello") # integer
hash((1,2,3)) # integerhelp BUILT-IN FUNCTIONSDisplays help for an object, function, or module
help(str) # str class docs help(print) # print function docs
vars BUILT-IN FUNCTIONSReturns the __dict__ of an object, or local variables if no argument
class Foo:
x = 1
vars(Foo()) # {'x': 1}hasattr BUILT-IN FUNCTIONSReturns True if the object has the named attribute
hasattr([], "append") # True hasattr(42, "upper") # False
getattr BUILT-IN FUNCTIONSGets the value of a named attribute of an object
getattr([], "append") # <method> getattr(obj, "x", None) # default if missing
setattr BUILT-IN FUNCTIONSSets the value of a named attribute of an object
setattr(obj, "x", 42) # obj.x = 42
iter BUILT-IN FUNCTIONSReturns an iterator object from an iterable
it = iter([1,2,3]) next(it) # 1
next BUILT-IN FUNCTIONSReturns the next item from an iterator
it = iter([1,2,3]) next(it) # 1 next(it, 0) # default when exhausted
super BUILT-IN FUNCTIONSReturns a proxy object that delegates method calls to a parent class
class Child(Parent):
def __init__(self):
super().__init__()property BUILT-IN FUNCTIONSCreates a managed attribute with getter, setter, and deleter
class C:
@property
def x(self): return self._xstaticmethod BUILT-IN FUNCTIONSMarks a method as a static method that does not receive self
class C:
@staticmethod
def greet(): return "hello"classmethod BUILT-IN FUNCTIONSMarks a method as a class method that receives the class as first argument
class C:
@classmethod
def create(cls): return cls()