Explanation:
1. Creating a List
nums = [10, 20, 30]
This line creates a list named nums.
The list contains three numbers: 10, 20, and 30.
A list is used to store multiple values in a single variable.
2. Initializing the Sum Variable
sum = 0
This line creates a variable called sum.
It is initialized with 0 because we will add numbers to it later.
This variable will store the total of all numbers in the list.
3. Starting the Loop
for i in nums:
This is a for loop.
It goes through each element in the nums list one by one.
On each loop:
i takes the value of the next number in the list
(first 10, then 20, then 30).
4. Adding Each Number to Sum
sum += i
This line adds the current value of i to sum.
It is the same as:
sum = sum + i
Step by step:
First loop: sum = 0 + 10 = 10
Second loop: sum = 10 + 20 = 30
Third loop: sum = 30 + 30 = 60
5. Printing the Final Result
print(sum)
This line prints the final value of sum.
After the loop finishes, sum contains 60.
Output will be:
60

0 Comments:
Post a Comment