Python comes with a variety of built-in functions that can be used to perform common tasks. Here are some examples:
print(): This function is used to print output to the console.print('Hello, world!') |
len(): This function is used to get the length of a list, tuple, or string.|
|
range(): This function is used to create a sequence of numbers.|
|
sum(): This function is used to get the sum of a list of numbers.|
|
max(): This function is used to get the maximum value in a list of numbers.|
|
min(): This function is used to get the minimum value in a list of numbers.|
|
sorted(): This function is used to sort a list of numbers or strings.
my_list = [5, 4, 3, 2, 1]print(sorted(my_list))my_list = ['banana', 'apple', 'orange']print(sorted(my_list)) |
zip(): This function is used to combine two or more lists into a list of tuples.
names = ['John', 'Jane', 'Mike']ages = [30, 25, 35]zipped = zip(names, ages)print(list(zipped)) |
enumerate(): This function is used to iterate over a list and get the index of each element.
my_list = ['apple', 'banana', 'orange']for i, fruit in enumerate(my_list): print(i, fruit) |
map(): This function is used to apply a function to each element of a list.
my_list = [1, 2, 3, 4, 5]def square(x): return x ** 2squared = map(square, my_list)print(list(squared)) |