Python provides a rich set of built-in functions that are always available for use without needing to import any additional modules. These functions cover a wide range of functionalities and are essential for everyday programming tasks. Below is a list of some built-in functions in Python:
| Function | Description | Example |
|---|---|---|
| abs() | Returns the absolute value of a number. | abs(-5) → 5 |
| all() | Returns True if all elements of an iterable are true. |
all([True, True, False]) → False |
| any() | Returns True if any element of an iterable is true. |
any([False, False, True]) → True |
| ascii() | Returns a string containing a printable representation of an object, with non-ASCII characters escaped. | ascii('ΓΌ') → '\u00fc' |
| bin() | Converts an integer to a binary string. | bin(10) → '0b1010' |
| bool() | Converts a value to a Boolean (True or False). |
bool(1) → True |
| bytearray() | Returns a new array of bytes. | bytearray([65, 66, 67]) → bytearray(b'ABC') |
| bytes() | Returns a new bytes object. | bytes('hello', 'utf-8') → b'hello' |
| callable() | Checks if the object appears callable (i.e., if it can be called as a function). | callable(len) → True |
| chr() | Returns a string representing a character whose Unicode code point is the integer
i.
|
chr(65) → 'A' |
| classmethod() | Returns a class method for the given function. | @classmethod in class definition |
| compile() | Compiles a source into a code or AST object. | compile('print("Hello, World!")', '<string>', 'exec') |
| complex() | Creates a complex number. | complex(1, 2) → (1+2j) |
| delattr() | Deletes an attribute from an object. | delattr(obj, 'attr') |
| dict() | Creates a dictionary. | dict(a=1, b=2) → {'a': 1, 'b': 2} |
| divmod() | Returns a tuple containing the quotient and remainder when dividing two numbers. | divmod(9, 4) → (2, 1) |
| enumerate() | Adds a counter to an iterable and returns it in the form of an enumerate object. | list(enumerate(['a', 'b'])) → [(0, 'a'), (1, 'b')] |
| eval() | Evaluates a Python expression and returns the result. | eval('3 + 4') → 7 |
| exec() | Executes a dynamically created Python code. | exec('x = 5') and then print(x) → 5 |
| filter() | Constructs an iterator from elements of an iterable for which a function returns true. | list(filter(lambda x: x % 2 == 0, [1, 2, 3])) → [2] |
| float() | Converts a number or string to a floating-point number. | float('3.14') → 3.14 |
| format() | Formats a specified value. | "{:.2f}".format(3.14159) → 3.14 |
| frozenset() | Creates an immutable set. | frozenset([1, 2, 3]) → frozenset({1, 2, 3}) |
| getattr() | Returns the value of a named attribute of an object. | getattr(obj, 'attr') |
| globals() | Returns a dictionary representing the current global symbol table. | globals() |
| hasattr() | Checks if an object has a specified attribute. | hasattr(obj, 'attr') |
| id() | Returns the identity of an object. | id([]) |
| input() | Reads a line of input from the user. | input("Enter your name: ") |
| int() | Converts a number or string to an integer. | int('10') → 10 |
| isinstance() | Checks if an object is an instance of a class or a subclass. | isinstance(5, int) → True |
| issubclass() | Checks if a class is a subclass of another class. | issubclass(bool, int) → True |
| iter() | Returns an iterator object. | iter([1, 2, 3]) |
| len() | Returns the length of an object. | len('hello') → 5 |
| list() | Creates a list. | list('abc') → ['a', 'b', 'c'] |
| locals() | Returns a dictionary representing the current local symbol table. | locals() |
| map() | Applies a function to all items in an iterable and returns an iterator. | list(map(lambda x: x ** 2, [1, 2, 3])) → [1, 4, 9] |
| memoryview() | Returns a memory view object of a given object. | memoryview(bytes('hello', 'utf-8')) |
| object() | Returns a new featureless object. | object() |
| open() | Opens a file and returns a file object. | open('file.txt', 'r') |
| ord() | Returns the Unicode code point for a given character. | ord('A') → 65 |
| pow() | Returns the value of a number raised to a power. | pow(2, 3) → 8 |
| print() | Prints values to the standard output. | print('Hello, World!') |
| reversed() | Returns a reverse iterator. | list(reversed([1, 2, 3])) → [3, 2, 1] |
| range() | Returns an immutable sequence of numbers. | list(range(5)) → [0, 1, 2, 3, 4] |
| round() | Rounds a floating-point number to a specified number of decimal places. | round(3.14159, 2) → 3.14 |
| str() | Converts a value to a string. | str(123) → '123' |
| tuple() | Creates a tuple. | tuple([1, 2, 3]) → (1, 2, 3) |
| type() | Returns the type of an object. | type('hello') → <class 'str'> |
| vars() | Returns the __dict__ attribute of an object. |
vars(obj) |
| zip() | Aggregates elements from two or more iterables (lists, tuples, etc.). | list(zip([1, 2, 3], ['a', 'b', 'c'])) → [(1, 'a'), (2, 'b'), (3, 'c')]
|
Here are some more commonly used Python built-in functions that are useful for daily programming tasks:
Generates a sequence of numbers.
print(list(range(5))) # Output: [0, 1, 2, 3, 4]
print(list(range(2, 10, 2))) # Output: [2, 4, 6, 8]
Returns a reversed iterator of a sequence.
print(list(reversed([1, 2, 3]))) # Output: [3, 2, 1]
Rounds a floating-point number to a specified number of decimal places.
print(round(3.14159, 2)) # Output: 3.14
print(round(3.5)) # Output: 4
Creates a set from an iterable.
print(set([1, 2, 2, 3])) # Output: {1, 2, 3}
Returns a slice object that can be used to slice sequences.
s = 'Python'
print(s[slice(1, 4)]) # Output: 'ytho'
Returns a new sorted list from the elements of any iterable.
print(sorted([3, 1, 2])) # Output: [1, 2, 3]
print(sorted('python')) # Output: ['h', 'n', 'o', 'p', 't', 'y']
Converts an object to a string.
print(str(123)) # Output: '123'
print(str([1, 2, 3])) # Output: '[1, 2, 3]'
Returns the sum of all items in an iterable.
print(sum([1, 2, 3, 4])) # Output: 10
Creates a tuple from an iterable.
print(tuple([1, 2, 3])) # Output: (1, 2, 3)
Aggregates elements from two or more iterables (lists, tuples, etc.) into tuples.
print(list(zip([1, 2, 3], ['a', 'b', 'c']))) # Output: [(1, 'a'), (2, 'b'), (3, 'c')]