Python provides a rich collection of built-in functions that allow you to perform a wide range of tasks without needing to import additional libraries. These functions cover everything from basic data manipulation to complex mathematical calculations, input/output operations, and more. This comprehensive tutorial will cover all Python built-in functions, providing explanations and examples for each.
Python’s built-in functions provide a set of common functionalities that can be used without importing additional modules. These functions are part of the standard library, meaning they are available by default, making Python a powerful and efficient language for programming tasks.
Built-in functions can be categorized based on their primary purposes:
Here is a complete list of Python built-in functions, organized by category.
bool()
: Converts a value to a Boolean (True
/False
).int()
: Converts a value to an integer.float()
: Converts a value to a floating-point number.complex()
: Converts a value to a complex number.str()
: Converts a value to a string.list()
: Converts a value to a list.tuple()
: Converts a value to a tuple.set()
: Converts a value to a set.dict()
: Converts a value to a dictionary.chr()
: Converts an integer to a character.ord()
: Converts a character to an integer.bin()
: Converts an integer to a binary string.hex()
: Converts an integer to a hexadecimal string.oct()
: Converts an integer to an octal string.abs()
: Returns the absolute value of a number.pow()
: Returns the power of a number.round()
: Rounds a number to a given precision.max()
: Returns the largest item in an iterable.min()
: Returns the smallest item in an iterable.sum()
: Returns the sum of all items in an iterable.len()
: Returns the length of an object.range()
: Generates a sequence of numbers.enumerate()
: Adds a counter to an iterable.map()
: Applies a function to each item in an iterable.filter()
: Filters elements based on a condition.zip()
: Aggregates elements from multiple iterables.sorted()
: Returns a sorted list of a given iterable.reversed()
: Returns a reversed iterator of a sequence.any()
: Returns True if any element in an iterable is True.all()
: Returns True if all elements in an iterable are True.print()
: Prints output to the console.input()
: Takes input from the user.open()
: Opens a file and returns a file object.format()
: Formats a specified value.ascii()
: Returns a string containing a printable representation of an object.repr()
: Returns a string representation of an object.type()
: Returns the type of an object.id()
: Returns the unique ID of an object.isinstance()
: Checks if an object is an instance of a class.issubclass()
: Checks if a class is a subclass of another class.callable()
: Checks if an object is callable.dir()
: Returns a list of attributes and methods of an object.help()
: Invokes the built-in help system.eval()
: Evaluates a given expression.exec()
: Executes Python code dynamically.globals()
: Returns a dictionary of the current global symbol table.locals()
: Returns a dictionary of the current local symbol table.bool(x)
: Converts a value to a Boolean.
print(bool(0)) # Output: False
print(bool(1)) # Output: True
int(x)
: Converts a value to an integer.
print(int(3.5)) # Output: 3
print(int("10")) # Output: 10
float(x)
: Converts a value to a float.
print(float("5.5")) # Output: 5.5
complex(x, y)
: Converts to a complex number.
print(complex(3, 4)) # Output: (3+4j)
str(x)
: Converts a value to a string.
print(str(123)) # Output: "123"
list(iterable)
: Converts an iterable to a list.
print(list("hello")) # Output: ['h', 'e', 'l', 'l', 'o']
tuple(iterable)
: Converts an iterable to a tuple.
print(tuple([1, 2, 3])) # Output: (1, 2, 3)
set(iterable)
: Converts an iterable to a set.
print(set("hello")) # Output: {'e', 'h', 'l', 'o'}
dict()
: Creates a dictionary.
print(dict(name="Alice", age=30)) # Output: {'name': 'Alice', 'age': 30}
chr(x)
: Converts an integer to a Unicode character.
print(chr(65)) # Output: 'A'
ord(char)
: Converts a character to an integer.
print(ord('A')) # Output: 65
bin(x)
: Converts an integer to binary.
print(bin(10)) # Output: '0b1010'
hex(x)
: Converts an integer to hexadecimal.
print(hex(255)) # Output: '0xff'
oct(x)
: Converts an integer to octal.
print(oct(8)) # Output: '0o10'
abs(x)
: Returns the absolute value of x.
print(abs(-5)) # Output: 5
pow(x, y)
: Returns x raised to the power of y.
print(pow(2, 3)) # Output: 8
round(x, n)
: Rounds x to n decimal places.
print(round(3.14159, 2)) # Output: 3.14
max(iterable)
: Returns the largest item in an iterable.
print(max([1, 2, 3])) # Output: 3
min(iterable)
: Returns the smallest item in an iterable.
print(min([1, 2, 3])) # Output: 1
sum(iterable)
: Returns the sum of all items in an iterable.
print(sum([1, 2, 3])) # Output: 6
print()
: Prints output to the console.
print("Hello, World!")
input()
: Takes input from the user.
name = input("Enter your name: ")
open(filename, mode)
: Opens a file.
with open("file.txt", "r") as file:
content = file.read()
Python’s built-in functions provide robust tools for various operations, from basic data manipulation to complex calculations. Knowing how and when to use these functions can streamline your code and help you write cleaner, more efficient programs.