Built-in Functions
Python's built-in functions are available without importing any modules. This reference covers all built-in functions with their parameters, return values, and practical usage examples.
Mathematical Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
abs() | Absolute value | abs(x) | Number | abs(-5) # 5 | Returns absolute value of a number |
divmod() | Division and modulo | divmod(a, b) | Tuple | divmod(10, 3) # (3, 1) | Returns quotient and remainder |
max() | Maximum value | max(iterable) or max(a, b, ...) | Any | max([1, 5, 3]) # 5 | Returns largest item |
min() | Minimum value | min(iterable) or min(a, b, ...) | Any | min([1, 5, 3]) # 1 | Returns smallest item |
pow() | Power operation | pow(base, exp, mod=None) | Number | pow(2, 3) # 8 | Returns base raised to power |
round() | Round number | round(number, ndigits=None) | Number | round(3.14159, 2) # 3.14 | Rounds to specified decimal places |
sum() | Sum of sequence | sum(iterable, start=0) | Number | sum([1, 2, 3]) # 6 | Returns sum of all items |
Type Conversion Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
bool() | Convert to boolean | bool(x) | bool | bool(1) # True | Converts value to boolean |
int() | Convert to integer | int(x, base=10) | int | int("123") # 123 | Converts to integer |
float() | Convert to float | float(x) | float | float("3.14") # 3.14 | Converts to floating point |
str() | Convert to string | str(object) | str | str(123) # "123" | Converts to string representation |
list() | Convert to list | list(iterable) | list | list("abc") # ['a', 'b', 'c'] | Converts iterable to list |
tuple() | Convert to tuple | tuple(iterable) | tuple | tuple([1, 2, 3]) # (1, 2, 3) | Converts iterable to tuple |
set() | Convert to set | set(iterable) | set | set([1, 2, 2, 3]) # {1, 2, 3} | Converts iterable to set |
dict() | Create dictionary | dict(**kwargs) | dict | dict(a=1, b=2) # {'a': 1, 'b': 2} | Creates dictionary |
bytes() | Convert to bytes | bytes(source, encoding) | bytes | bytes("hello", "utf-8") | Converts to bytes object |
Sequence and Iteration Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
len() | Get length | len(s) | int | len([1, 2, 3]) # 3 | Returns length of sequence |
range() | Create range | range(start, stop, step) | range | list(range(5)) # [0, 1, 2, 3, 4] | Creates sequence of numbers |
enumerate() | Add indices | enumerate(iterable, start=0) | enumerate | list(enumerate(['a', 'b'])) # [(0, 'a'), (1, 'b')] | Returns indexed pairs |
zip() | Combine iterables | zip(*iterables) | zip | list(zip([1, 2], ['a', 'b'])) # [(1, 'a'), (2, 'b')] | Combines multiple iterables |
reversed() | Reverse sequence | reversed(seq) | iterator | list(reversed([1, 2, 3])) # [3, 2, 1] | Returns reversed iterator |
sorted() | Sort sequence | sorted(iterable, key=None, reverse=False) | list | sorted([3, 1, 2]) # [1, 2, 3] | Returns sorted list |
filter() | Filter items | filter(function, iterable) | filter | list(filter(lambda x: x > 0, [-1, 0, 1, 2])) # [1, 2] | Filters items by function |
map() | Apply function | map(function, iterable) | map | list(map(str.upper, ['a', 'b'])) # ['A', 'B'] | Applies function to each item |
any() | Test any true | any(iterable) | bool | any([False, True, False]) # True | True if any element is true |
all() | Test all true | all(iterable) | bool | all([True, True, False]) # False | True if all elements are true |
Object and Attribute Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
getattr() | Get attribute | getattr(object, name, default=None) | Any | getattr(obj, 'attr', 'default') | Gets object attribute safely |
setattr() | Set attribute | setattr(object, name, value) | None | setattr(obj, 'attr', 'value') | Sets object attribute |
hasattr() | Check attribute | hasattr(object, name) | bool | hasattr(obj, 'attr') # True/False | Checks if object has attribute |
delattr() | Delete attribute | delattr(object, name) | None | delattr(obj, 'attr') | Deletes object attribute |
callable() | Check callable | callable(object) | bool | callable(len) # True | Checks if object is callable |
isinstance() | Check instance | isinstance(object, classinfo) | bool | isinstance([], list) # True | Checks object type |
issubclass() | Check subclass | issubclass(class, classinfo) | bool | issubclass(bool, int) # True | Checks class inheritance |
type() | Get type | type(object) | type | type([]) # <class 'list'> | Returns object type |
id() | Get object ID | id(object) | int | id([]) # 140234567890 | Returns unique object identifier |
vars() | Get variables | vars(object=None) | dict | vars(obj) # {'attr': 'value'} | Returns object's __dict__ |
Input/Output Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
print() | Print output | print(*values, sep=' ', end='\n', file=sys.stdout) | None | print("Hello", "World") | Prints values to output |
input() | Get user input | input(prompt='') | str | name = input("Name: ") | Gets string input from user |
open() | Open file | open(file, mode='r', encoding=None) | file object | open('file.txt', 'r') | Opens file for reading/writing |
Advanced Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
eval() | Evaluate expression | eval(expression, globals=None, locals=None) | Any | eval("2 + 3") # 5 | Evaluates Python expression |
exec() | Execute code | exec(object, globals=None, locals=None) | None | exec("x = 5") | Executes Python code |
compile() | Compile code | compile(source, filename, mode) | code object | compile("x + 1", "", "eval") | Compiles source into code object |
globals() | Global namespace | globals() | dict | globals()['__name__'] | Returns global symbol table |
locals() | Local namespace | locals() | dict | locals() | Returns local symbol table |
dir() | List attributes | dir(object=None) | list | dir([]) # ['append', 'clear', ...] | Lists object attributes |
help() | Get help | help(object=None) | None | help(len) | Shows help documentation |
hash() | Get hash | hash(object) | int | hash("hello") # 2314058222102390712 | Returns object hash value |
iter() | Create iterator | iter(object, sentinel=None) | iterator | iter([1, 2, 3]) | Creates iterator object |
next() | Get next item | next(iterator, default=None) | Any | next(iter([1, 2, 3])) # 1 | Gets next item from iterator |
repr() | String representation | repr(object) | str | repr([1, 2, 3]) # "[1, 2, 3]" | Returns string representation |
ascii() | ASCII representation | ascii(object) | str | ascii("café") # "'caf\\xe9'" | Returns ASCII string representation |
bin() | Binary representation | bin(x) | str | bin(10) # '0b1010' | Converts integer to binary string |
hex() | Hexadecimal representation | hex(x) | str | hex(255) # '0xff' | Converts integer to hex string |
oct() | Octal representation | oct(x) | str | oct(8) # '0o10' | Converts integer to octal string |
ord() | Character code | ord(c) | int | ord('A') # 65 | Returns Unicode code of character |
chr() | Character from code | chr(i) | str | chr(65) # 'A' | Returns character from Unicode code |
Memory and Object Functions
Function | Purpose | Parameters | Return Type | Example | Description |
---|---|---|---|---|---|
object() | Create object | object() | object | obj = object() | Creates new object instance |
super() | Parent class access | super(type=None, object=None) | super | super().__init__() | Access parent class methods |
property() | Create property | property(fget=None, fset=None, fdel=None, doc=None) | property | prop = property(getter, setter) | Creates property descriptor |
staticmethod() | Static method | staticmethod(function) | staticmethod | @staticmethod | Creates static method |
classmethod() | Class method | classmethod(function) | classmethod | @classmethod | Creates class method |
Best Practices
- Use type conversion functions: Prefer
int()
,str()
, etc. over manual conversion - Leverage sequence functions: Use
enumerate()
,zip()
,sorted()
for cleaner code - Check before accessing: Use
hasattr()
beforegetattr()
for safety - Prefer built-ins: Built-in functions are optimized and typically faster
- Know your iterators: Understand the difference between
map()
,filter()
, and list comprehensions - Use
any()
andall()
: More readable than manual loops for boolean testing
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.