Python Function |
Description |
Example |
abs() |
Returns the absolute value of a number. |
abs(-10) -> 10 |
all() |
Returns True when all elements in an iterable are True. |
all([True, True]) -> True |
any() |
Checks if any element in an iterable is True. |
any([False, True]) -> True |
ascii() |
Returns a printable representation of an object. |
ascii(‘ä’) -> ‘ä’ |
bin() |
Converts an integer to a binary string. |
bin(5) -> ‘0b101’ |
bool() |
Converts a value to Boolean. |
bool(1) -> True |
bytearray() |
Returns an array of the given byte size. |
bytearray(5) -> bytearray(b’\x00\x00\x00\x00\x00′) |
bytes() |
Returns an immutable bytes object. |
bytes(5) -> b’\x00\x00\x00\x00\x00′ |
callable() |
Check if the object is callable. |
callable(len) -> True |
compile() |
Compiles source into a code object. |
compile(‘5 + 5’, ‘<string>’, ‘eval’) |
complex() |
Creates a complex number. |
complex(1, 2) -> (1+2j) |
delattr() |
Deletes an attribute from an object. |
delattr(obj, ‘attr’) |
dict() |
Creates a Python dictionary. |
dict(a=1, b=2) -> {‘a’: 1, ‘b’: 2} |
dir() |
Returns attributes of an object. |
dir([]) -> [‘append’, ‘clear’, …] |
divmod() |
Returns a tuple of quotient and remainder. |
divmod(5, 2) -> (2, 1) |
enumerate() |
Returns an enumerate object. |
enumerate([‘a’, ‘b’]) -> (0, ‘a’),(1, ‘b’) |
eval() |
Executes a given string as Python code. |
eval(‘5 + 5’) -> 10 |
exec() |
Executes dynamically created Python code. |
exec(‘a = 5’) |
filter() |
Constructs an iterator from true elements of an iterable. |
filter(lambda x: x > 0, [-1, 2]) -> [2] |
float() |
Converts a value to a floating-point number. |
float(‘2.5’) -> 2.5 |
format() |
Formats a value into a string. |
{:.2f}’.format(3.14159) -> ‘3.14’ |
frozenset() |
Returns an immutable frozenset object. |
frozenset([1, 2]) -> frozenset({1, 2}) |
getattr() |
Gets the value of a named attribute. |
getattr(obj, ‘attr’) |
globals() |
Returns the global symbol table as a dictionary. |
globals() -> {‘__name__’: ‘__main__’, …} |
hasattr() |
Checks if an object has a named attribute. |
hasattr(obj, ‘attr’) -> True |
hash() |
Returns the hash value of an object. |
hash(‘test’) |
help() |
Invokes the interactive help system. |
help(abs) |
hex() |
Converts an integer to hexadecimal. |
hex(255) -> ‘0xff’ |
id() |
Returns the identity of an object. |
id(obj) |
input() |
Reads a line of input from the user. |
input(‘Enter: ‘) |
int() |
Converts a value to an integer. |
int(’10’) -> 10 |
isinstance() |
Checks if an object is an instance of a class. |
isinstance(5, int) -> True |
issubclass() |
Checks if a class is a subclass of another. |
issubclass(bool, int) -> True |
iter() |
Returns an iterator for an object. |
iter([1, 2, 3]) |
len() |
Returns the length of an object. |
len([1, 2, 3]) -> 3 |
list() |
Creates a list. |
list((1, 2)) -> [1, 2] |
locals() |
Returns the local symbol table as a dictionary. |
locals() -> {…} |
map() |
Applies a function to an iterable. |
map(str, [1, 2]) -> [‘1’, ‘2’] |
max() |
Returns the largest item in an iterable. |
max([1, 2, 3]) -> 3 |
memoryview() |
Returns a memory view object. |
memoryview(b’abc’) |
min() |
Returns the smallest item in an iterable. |
min([1, 2, 3]) -> 1 |
next() |
Returns the next item from an iterator. |
next(iter([1, 2])) -> 1 |
object() |
Creates a featureless object. |
object() |
oct() |
Converts an integer to octal. |
oct(8) -> ‘0o10’ |
open() |
Opens a file and returns a file object. |
open(‘file.txt’) |
ord() |
Returns the Unicode code point for a character. |
ord(‘A’) -> 65 |
pow() |
Returns the power of a number. |
pow(2, 3) -> 8 |
print() |
Prints a value to the console. |
print(‘Hello’) |
property() |
Returns a property object. |
@property
def name(self): return self._name |
range() |
Creates a sequence of integers. |
range(5) -> 0, 1, 2, 3, 4 |
repr() |
Returns a string representation of an object. |
repr(‘test’) -> ”test” |
reversed() |
Returns a reversed iterator. |
reversed([1, 2]) -> [2, 1] |
round() |
Rounds a number to the nearest integer. |
round(2.5) -> 3 |
set() |
Creates a set. |
set([1, 2, 3]) |
setattr() |
Sets the value of an object’s attribute. |
setattr(obj, ‘attr’, 5) |
slice() |
Creates a slice object. |
slice(0, 10) |
sorted() |
Returns a sorted list from an iterable. |
sorted([3, 1]) -> [1, 3] |
staticmethod() |
Creates a static method. |
@staticmethod
def method(): pass |
str() |
Converts an object to a string. |
str(10) -> ’10’ |
sum() |
Adds items in an iterable. |
sum([1, 2]) -> 3 |
super() |
Returns a proxy for the parent class. |
super().method() |
tuple() |
Creates a tuple. |
tuple([1, 2]) -> (1, 2) |
type() |
Returns the type of an object. |
type(5) -> <class ‘int’> |
vars() |
Returns the __dict__ attribute of an object. |
vars(obj) |
zip() |
Returns an iterator of tuples. |
zip([1, 2], [‘a’, ‘b’]) -> [(1, ‘a’), (2, ‘b’)] |
__import__() |
Dynamically imports a module. |
__import__(‘os’) |