Python provides a rich set of built-in functions that you can use to perform common tasks. In this tutorial, we will explore some of the most commonly used built-in functions, along with examples and explanations.
len()
The len()
function returns the length (the number of items) of an object. The object can be a sequence (such as a string, list, or tuple) or a collection (such as a dictionary).
example_list = [1, 2, 3, 4, 5]
print(len(example_list)) # Output: 5
example_string = "Hello, World!"
print(len(example_string)) # Output: 13
What will be the output of len()
for an empty list and an empty string?
The output of len()
for an empty list []
and an empty string ""
will be 0
.
type()
The type()
function returns the type of an object.
example_int = 10
print(type(example_int)) # Output:
example_list = [1, 2, 3]
print(type(example_list)) # Output:
What will be the output of type()
for a string "Hello"?
The output of type()
for a string "Hello"
will be <class 'str'>
.
max()
and min()
The max()
function returns the largest item in an iterable or the largest of two or more arguments. The min()
function returns the smallest item in an iterable or the smallest of two or more arguments.
numbers = [1, 2, 3, 4, 5]
print(max(numbers)) # Output: 5
print(min(numbers)) # Output: 1
print(max(10, 20, 30)) # Output: 30
print(min(10, 20, 30)) # Output: 10
What will be the output of max()
and min()
for the list [-1, -2, -3, -4]
?
The output of max()
for the list [-1, -2, -3, -4]
will be -1
and the output of min()
will be -4
.
sum()
The sum()
function returns the sum of all items in an iterable.
numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # Output: 15
numbers_with_start = [1, 2, 3]
print(sum(numbers_with_start, 10)) # Output: 16 (10 + 1 + 2 + 3)
What will be the output of sum()
for the list [1.5, 2.5, 3.5]
?
The output of sum()
for the list [1.5, 2.5, 3.5]
will be 7.5
.
sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
unsorted_list = [3, 1, 4, 1, 5, 9]
print(sorted(unsorted_list)) # Output: [1, 1, 3, 4, 5, 9]
unsorted_string = "python"
print(sorted(unsorted_string)) # Output: ['h', 'n', 'o', 'p', 't', 'y']
What will be the output of sorted()
for the list [3, 2, 1]
?
The output of sorted()
for the list [3, 2, 1]
will be [1, 2, 3]
.
abs()
The abs()
function returns the absolute value of a number.
negative_number = -10
print(abs(negative_number)) # Output: 10
negative_float = -7.5
print(abs(negative_float)) # Output: 7.5
What will be the output of abs()
for 0
?
The output of abs()
for 0
will be 0
.
round()
The round()
function rounds a floating-point number to the nearest integer. You can also specify the number of decimal places to round to.
floating_number = 3.14159
print(round(floating_number)) # Output: 3
print(round(floating_number, 2)) # Output: 3.14
What will be the output of round(2.675, 2)
?
The output of round(2.675, 2)
will be 2.67
because of floating-point arithmetic issues.
pow()
The pow()
function returns the value of x to the power of y (x^y). It can also take a third argument, the modulo z, which will return (x^y) % z.
base = 2
exponent = 3
print(pow(base, exponent)) # Output: 8
print(pow(base, exponent, 5)) # Output: 3 (2^3 % 5)
What will be the output of pow(3, 2, 4)
?
The output of pow(3, 2, 4)
will be 1
because 3^2 % 4 = 9 % 4 = 1
.
all()
and any()
The all()
function returns True
if all elements of the iterable are true (or if the iterable is empty). The any()
function returns True
if any element of the iterable is true. If the iterable is empty, any()
returns False
.
bool_list = [True, True, False]
print(all(bool_list)) # Output: False
print(any(bool_list)) # Output: True
What will be the output of all([])
and any([])
?
The output of all([])
will be True
(an empty iterable is considered to have all true elements), and the output of any([])
will be False
(an empty iterable has no true elements).
enumerate()
The enumerate()
function adds a counter to an iterable and returns it as an enumerate object.
example_list = ['a', 'b', 'c']
for index, value in enumerate(example_list):
print(index, value)
# Output:
# 0 a
# 1 b
# 2 c
What will be the output of list(enumerate('hello'))
?
The output of list(enumerate('hello'))
will be [(0, 'h'), (1, 'e'), (2, 'l'), (3, 'l'), (4, 'o')]
.
Python's built-in functions provide a powerful and easy way to perform common tasks. Understanding how to use these functions will help you write more efficient and readable code.