Code Explanation:
1. Importing Pandas
import pandas as pd
We load the pandas library and give it the alias pd.
This allows us to use pandas functions easily, like pd.Series.
2. Creating a Series
s = pd.Series([10, 20, 30, 40, 50])
pd.Series() creates a one-dimensional labeled array.
Here, we create a Series with values: [10, 20, 30, 40, 50].
The default index will be [0, 1, 2, 3, 4].
So, s looks like:
0 10
1 20
2 30
3 40
4 50
dtype: int64
3. Boolean Masking with Condition
s[s % 20 == 0]
s % 20 == 0 checks which values are divisible by 20.
Result of condition:
[False, True, False, True, False]
Applying this mask gives only the values where condition is True → 20 and 40.
So,
s[s % 20 == 0] → [20, 40]
4. Updating Selected Elements
s[s % 20 == 0] *= 2
This multiplies the selected elements (20 and 40) by 2.
So:
20 → 40
40 → 80
Now the Series becomes:
0 10
1 40
2 30
3 80
4 50
dtype: int64
5. Summing the Series
print(s.sum())
s.sum() adds all values in the Series.
Calculation: 10 + 40 + 30 + 80 + 50 = 210.
So the output is:
210
Final Answer: 210
Download Book - 500 Days Python Coding Challenges with Explanation
.png)

0 Comments:
Post a Comment