-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforsofintinlist.py
More file actions
33 lines (32 loc) · 1.06 KB
/
forsofintinlist.py
File metadata and controls
33 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# sum of integers in list
l=eval(input()) # l = [3,4,6,5,7]
sum=0
for num in l:
if isinstance(num,int): # we can also use if type(num)==int:
sum+=num
print(sum)
'''
Step 1: Initialize variables
The input list l = is taken.
Initialize sum = 0 to store the running total.
Step 2: Begin the for loop iteration over each element in the list.
Step 3: First iteration (num = 3)
Check if num is an integer using isinstance(num, int) → True.
Add num (3) to sum → sum = 0 + 3 = 3.
Step 4: Second iteration (num = 4)
Check if num is an integer → True.
Add num (4) to sum → sum = 3 + 4 = 7.
Step 5: Third iteration (num = 6)
Check if num is an integer → True.
Add num (6) to sum → sum = 7 + 6 = 13.
Step 6: Fourth iteration (num = 5)
Check if num is an integer → True.
Add num (5) to sum → sum = 13 + 5 = 18.
Step 7: Fifth iteration (num = 7)
Check if num is an integer → True.
Add num (7) to sum → sum = 18 + 7 = 25.
Step 8: End of loop
The loop ends after processing all elements in the list.
Step 9: Print the result
Print the final value of sum → 25.
'''