Python provides a rich set of built-in functions for developers. These functions help to perform common programming tasks conveniently. In this article, we will take a closer look at these built-in functions and explore how to use each function along with examples.
1. print()
function
The print()
function is one of the most commonly used functions, and it is used to display output on the console. It can take multiple arguments and concatenate them into a single string for output, adding spaces between the strings by default.
print("Hello, World!")
print("Python", "is", "fun")
Result:
Hello, World! Python is fun
2. len()
function
The len()
function returns the length of an object. It is primarily used with sequence data types such as strings, lists, tuples, and dictionaries.
string = "Python"
print(len(string))
numbers = [1, 2, 3, 4, 5]
print(len(numbers))
Result:
6 5
3. type()
function
The type()
function returns the data type of an object. This function is useful for checking if a variable has the expected type.
print(type(3))
print(type(3.0))
print(type("Hello"))
Result:
<class 'int'> <class 'float'> <class 'str'>
4. input()
function
The input()
function is used to receive string input from the user. In Python 3.x, it always receives input as a string.
name = input("Enter your name: ")
print("Hello, " + name + "!")
5. sum()
function
The sum()
function calculates the sum of a sequence of numbers. This function is primarily used to sum lists or tuples.
numbers = [1, 2, 3, 4, 5]
print(sum(numbers))
Result:
15
6. min()
and max()
functions
The min()
function returns the minimum value from a sequence, while the max()
function returns the maximum value. These functions are useful for finding the minimum and maximum values in a numerical sequence.
numbers = [3, 1, 4, 1, 5, 9]
print(min(numbers))
print(max(numbers))
Result:
1 9
7. sorted()
function
The sorted()
function returns a sorted list from the given sequence. This function does not modify the original list and creates a new sorted list. By default, it sorts in ascending order, and using reverse=True
sorts in descending order.
numbers = [3, 1, 4, 1, 5, 9]
print(sorted(numbers))
print(sorted(numbers, reverse=True))
Result:
[1, 1, 3, 4, 5, 9] [9, 5, 4, 3, 1, 1]
8. any()
and all()
functions
The any()
function returns True
if at least one element in the sequence is True
, otherwise it returns False
. The all()
function returns True
if all elements are True
.
bool_list = [True, False, True]
print(any(bool_list))
print(all(bool_list))
Result:
True False
9. zip()
function
The zip()
function combines multiple sequences together in parallel. It takes the elements from each sequence and combines them into tuples, limiting the output to the length of the shortest sequence.
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
zipped = zip(list1, list2)
print(list(zipped))
Result:
[(1, 'a'), (2, 'b'), (3, 'c')]
10. enumerate()
function
The enumerate()
function returns the elements of a sequence as tuples along with their indices. By default, the index starts at 0, but it can be changed to start at any other number.
letters = ['a', 'b', 'c']
for index, letter in enumerate(letters):
print(index, letter)
Result:
0 a 1 b 2 c
11. range()
function
The range()
function generates a sequence of integers. This sequence is primarily used in loops. range()
can take three arguments, representing the start value, the end value, and the step value.
for i in range(5):
print(i)
for i in range(1, 10, 2):
print(i)
Result:
0 1 2 3 4 1 3 5 7 9
12. filter()
function
The filter()
function takes a function and a sequence and filters the elements that satisfy the condition of the function. The result is a filter object, which can be converted to a list using list()
.
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)
print(list(even_numbers))
Result:
[2, 4, 6]
13. map()
function
The map()
function takes a function and a sequence and returns the results of applying the function. It is useful for applying a function to all given elements.
def square(n):
return n * n
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
print(list(squared_numbers))
Result:
[1, 4, 9, 16, 25]
14. reduce()
function
The reduce()
function performs cumulative calculations and returns a single result. The reduce()
function must be imported from the functools
module. Its primary use case is to accumulate values across a sequence.
from functools import reduce
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5]
total = reduce(add, numbers)
print(total)
Result:
15
The examples above showcase several built-in functions in Python, exploring the characteristics and usage of each function. Additionally, Python offers various other built-in functions that help solve specific tasks more easily and quickly.
By understanding how to use these functions effectively, you can write more efficient and readable code. Built-in functions are essential tools to achieve this goal.