Python comes with a rich set of built-in functions that provide functionality for various common tasks. Here are some of the most commonly used built-in functions in Python:
1. `print()`: Prints the specified object to the screen or standard output.
print(“Hello, world!”) # Output: Hello, world!
2. `len()`: Returns the length (the number of items) of an object like a string, tuple, list, dictionary, etc.
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Output: 5
3. `type()`: Returns the type of the specified object.
x = 5
print(type(x)) # Output: <class ‘int’>
4. `input()`: Reads a line from input, converts it into a string, and returns it.
name = input(“Enter your name: “)
print(“Hello,”, name)
5. `int()`: Converts a number or string to an integer.
num_str = “10”
num_int = int(num_str)
print(num_int) # Output: 10
Â
6. `float()`: Converts a number or string to a floating-point number.
num_str = “10.5”
num_float = float(num_str)
print(num_float) # Output: 10.5
7. `str()`: Converts an object into a string.
num = 10
num_str = str(num)
print(num_str) # Output: ’10’
Â
8. `list()`: Creates a list from an iterable object.
my_tuple = (1, 2, 3, 4, 5)
my_list = list(my_tuple)
print(my_list) # Output: [1, 2, 3, 4, 5]
Â
9. `tuple()`: Creates a tuple from an iterable object.
my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(my_list)
print(my_tuple) # Output: (1, 2, 3, 4, 5)
10. `dict()`: Creates a dictionary.
my_dict = dict(name=”John”, age=30)
print(my_dict) # Output: {‘name’: ‘John’, ‘age’: 30}
11. `range()`: Returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), stopping before a specified number.
# Example 1: Using range to generate a sequence of numbers
for num in range(5):
print(num) # Output: 0, 1, 2, 3, 4
# Example 2: Specifying start, stop, and step parameters
for num in range(1, 10, 2):
print(num) # Output: 1, 3, 5, 7, 9
Â
12. `sorted()`: Returns a sorted list of the specified iterable object.
# Example: Sorting a list of numbers
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [1, 1, 2, 3, 4, 5, 6, 9]
Â
13. `sum()`: Returns the sum of all elements in the specified iterable.
# Example: Calculating the sum of elements in a list
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total) # Output: 15
14. `max()`: Returns the largest item in an iterable or the largest of two or more arguments.
# Example: Finding the largest item in a list
numbers = [7, 2, 9, 1, 6]
largest = max(numbers)
print(largest) # Output: 9
15. `min()`: Returns the smallest item in an iterable or the smallest of two or more arguments.
# Example: Finding the smallest item in a list
numbers = [7, 2, 9, 1, 6]
smallest = min(numbers)
print(smallest) # Output: 1
16. `abs()`: Returns the absolute value of a number.
# Example: Finding the absolute value of a number
num = -10
absolute_value = abs(num)
print(absolute_value) # Output: 10
17. `input()`: Reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
# Example: Getting user input
name = input(“Enter your name: “)
print(“Hello, ” + name + “!”) # Output depends on user input
These are just a few examples of the built-in functions available in Python. There are many more built-in functions provided by Python, each serving a specific purpose to help you write more efficient and readable code.