
It’s tempting to turn to Google and ChatGPT every time you need a Python function. However, toggling back and forth is time-consuming and energy-draining. According to Professors Meyer, Evans, and Rubinstein, each “task switch” results in a 40% loss in productivity. After experiencing a lot of brain drain from toggling, I decided to memorize these ten Python functions, and my programming skills have increased.
In this article, you’ll learn ten functions that have improved my coding skills. I’ve included examples for each to help you navigate some of the nuances. The examples are based on randomly generated sales data.
1. len() function
- purpose : Get the length of an iterable (e.g., list, string, or DataFrame rows/columns).
- Example :
python
data = [10, 20, 30, 40]
print(len(data)) # Output: 4
2. sum() function
- Purpose : Calculate the sum of an iterable (e.g., list or array).
- Example :
python
numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # Output: 15
3.Map() function
- Purpose : Apply a function to each repeating item.
- Example :
python
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
print(squared) # Output: [1, 4, 9, 16]
4. filter() function
- Purpose : Filter elements in a return based on a condition.
- Example :
python
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # Output: [2, 4, 6]
5.zip() function
- Purpose: Combine multiple iterables element-wise.
- Example :
python
names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 95]
combined = list(zip(names, scores))
print(combined) # Output: [('Alice', 85), ('Bob', 90), ('Charlie', 95)]
6.sorted() function
- Purpose : To return a sorted list from an iterable.
- Example :
python
data = [7, 1, 5, 3]
print(sorted(data)) # Output: [1, 3, 5, 7]
7.round() function
- Purpose: Round the number to the nearest integer or to the specified precision.
- Example :
python
pi = 3.14159
print(round(pi, 2)) # Output: 3.14
8.enumerate() function
- Purpose : Get the index and value when making a return.
- Example :
python
items = ['a', 'b', 'c']
for idx, value in enumerate(items):
print(idx, value)
# Output:
# 0 a
# 1 b
# 2 c
9.any()
/ all()
function
- Purpose : Check if any or all repeatable elements meet a condition.
- Example :
python
values = [True, False, True]
print(any(values)) # Output: True
print(all(values)) # Output: False
10.set() function
- Purpose : To remove duplicates from an iterable and create a set.
- Example :
python
data = [1, 2, 2, 3, 4, 4, 5]
unique_data = set(data)
print(unique_data) # Output: {1, 2, 3, 4, 5}
These functions are foundational and highly versatile, saving time and improving code efficiency for data scientists.
Great breakdown! Roulette strategies, like AI tools, rely on smart decision-making. Check out the AI Reviews Assistant for curated insights that simplify tech choices-just like understanding odds simplifies the game.
Navigating the AI landscape just got smarter with platforms like Best AI Tools. It’s a goldmine for finding curated solutions that actually work, saving time and boosting productivity.