Table of Contents
ToggleThe print() function is used to output information to the console. It’s one of the most basic and widely used functions in Python for debugging or simply displaying values.
print("Hello, World!")
len() returns the number of items in an object, such as a string, list, or dictionary. It’s commonly used to find the length of a sequence or collection.
print(len("Python"))
The type() function is used to get the data type of a variable or object. It returns the class type of the object, which is very useful for debugging.
print(type(123))
The int() function converts a given value to an integer type. It’s useful when you want to ensure that a value is represented as an integer for mathematical operations.
num = int("123") print(num)
The str() function converts any given value to a string type. This is important when you need to concatenate or format values as text.
text = str(123) print(text)
The float() function converts a value into a floating-point number. This is helpful when performing calculations involving decimal numbers.
num = float("3.14") print(num)
The sum() function calculates the sum of all elements in an iterable such as a list or tuple. It’s an easy way to add up numbers in a collection.
numbers = [1, 2, 3, 4] print(sum(numbers))
The min() function returns the smallest item from an iterable or among two or more arguments. It helps identify the minimum value in a set of data.
print(min([3, 2, 1, 4]))
The max() function returns the largest item from an iterable or between two or more arguments. It’s useful for finding the maximum value in a list.
print(max([3, 2, 1, 4]))
The sorted() function returns a sorted list of the specified iterable’s elements in ascending order. This is ideal for organizing data.
print(sorted([3, 1, 4, 2]))
The range() function generates a sequence of numbers, commonly used for looping a specific number of times in for
loops. It can accept start, stop, and step arguments.
for i in range(5): print(i)
The input() function allows the user to provide input from the console. It returns the data as a string, which can be converted into other types using functions like int()
or float()
.
name = input("Enter your name: ") print(f"Hello, {name}!")
The abs() function returns the absolute (non-negative) value of a number. It’s commonly used when you need to eliminate negative signs from numbers in calculations.
print(abs(-5))
The round() function rounds a number to a specified number of decimal places. It’s useful when you need to round off floating-point numbers for display or calculations.
print(round(3.14159, 2))
The zip() function combines multiple iterables (like lists) into a single iterable where elements are paired together. It is useful when you want to iterate over multiple lists in parallel.
names = ["Alice", "Bob", "Charlie"] scores = [85, 90, 95] zipped = zip(names, scores) for name, score in zipped: print(name, score)
The all() function returns True if all elements in an iterable are true (or if the iterable is empty). It’s commonly used to validate conditions across multiple values.
print(all([True, True, False]))
The any() function returns True if any element in the iterable is true. It’s used to check if there is at least one true condition among a group of items.
print(any([False, True, False]))
The filter() function filters elements from an iterable based on a function that evaluates each element. It returns an iterable containing the elements that meet the condition.
numbers = [1, 2, 3, 4, 5] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers))
The map() function applies a given function to each item in an iterable and returns an iterator. It’s useful for applying transformations to each element of a list or other iterable.
numbers = [1, 2, 3, 4] squared_numbers = map(lambda x: x ** 2, numbers) print(list(squared_numbers))
The reduce() function from the functools
module applies a rolling computation to sequential pairs of values in an iterable to reduce it to a single result. It is useful for cumulative operations like summing values.
from functools import reduce numbers = [1, 2, 3, 4] sum_numbers = reduce(lambda x, y: x + y, numbers) print(sum_numbers)