Master Python with These 20 Must-Know Functions

1. `print()` – Output to the Console

The 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!")  

2. `len()` – Length of an Object

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"))  

3. `type()` – Check the Data Type

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))  

4. `int()` – Convert to Integer

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)  

5. `str()` – Convert to String

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)  

6. `float()` – Convert to Float

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)  

7. `sum()` – Sum of Iterable

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))  

8. `min()` – Minimum Value

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]))  

9. `max()` – Maximum Value

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]))  

10. `sorted()` – Sorting an Iterable

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]))  

11. `range()` – Generate a Sequence of Numbers

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)  
    

12. `input()` – Taking User Input

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}!")  
    

13. `abs()` – Absolute Value

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))  
    

14. `round()` – Round a Number

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))  
    

15. `zip()` – Combine Multiple Iterables

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)  
    

16. `all()` – Check if All Elements are True

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]))  
    

17. `any()` – Check if Any Element is True

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]))  
    

18. `filter()` – Filter Elements Based on Condition

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))  
    

19. `map()` – Apply a Function to Each Item in an Iterable

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))  
    

20. `reduce()` – Apply a Function to Accumulate Elements

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)