Python Built-in Functions
Python has numerous built-in functions that are always available for use.
These functions perform a wide range of tasks, such as type conversion,
mathematical operations, and input/output handling.
1. Input and Output
- print(): Prints output to the console.
- input(): Reads input from the user.
2. Type Conversion
- int(): Converts to an integer.
- float(): Converts to a float.
- str(): Converts to a string.
- bool(): Converts to a boolean.
- list(): Converts to a list.
- tuple(): Converts to a tuple.
- set(): Converts to a set.
- dict(): Converts to a dictionary.
3. Mathematical Operations
- abs(): Returns the absolute value.
- pow(): Returns the result of a number raised to a power.
- round(): Rounds a number to a specified number of digits.
- min(): Returns the smallest of the input values.
- max(): Returns the largest of the input values.
- sum(): Returns the sum of a list or iterable.
4. Iterables and Sequences
- len(): Returns the length of an iterable.
- sorted(): Returns a sorted list of an iterable.
- reversed(): Returns a reversed iterator.
- enumerate(): Returns an enumerated object.
- zip(): Combines iterables element-wise into tuples.
5. String Operations
- format(): Formats a string.
- chr(): Converts an integer to a character.
- ord(): Converts a character to its Unicode integer.
6. Functional Programming
- map(): Applies a function to all items in an iterable.
- filter(): Filters elements of an iterable based on a function.
- reduce(): Performs cumulative computation (requires functools).
- lambda: Used to create small anonymous functions.
7. File Handling
- open(): Opens a file for reading or writing.
8. Object and Type Inspection
- 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 or type.
- dir(): Returns a list of valid attributes for an object.
- help(): Displays help information for an object.
9. Miscellaneous
- range(): Returns a sequence of numbers.
- eval(): Evaluates a string as Python code.
- exec(): Executes Python code dynamically.
- hash(): Returns the hash value of an object.
- any(): Returns True if any element in an iterable is true.
- all(): Returns True if all elements in an iterable are true.
Example Usage:
```
numbers = [1, 2, 3, 4, 5]
# Get the sum of the list
print("Sum:", sum(numbers))
# Check the type of a variable
print("Type:", type(numbers))
# Use map to double the numbers
doubled = list(map(lambda x: x * 2, numbers))
print("Doubled:", doubled)
# Get the length of the list
print("Length:", len(numbers))
```
Python's built-in functions are powerful and efficient. You can find the complete list in the official
Python documentation (https://docs.python.org/3/library/functions.html).