-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforsooddint.py
More file actions
40 lines (34 loc) · 1011 Bytes
/
forsooddint.py
File metadata and controls
40 lines (34 loc) · 1011 Bytes
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
34
35
36
37
38
39
40
# find sum of odd integers in list
l=eval(input('enter a list:')) #l=[2,3,5,4,6]
sum=0
for ele in l:
if isinstance(ele,int):#we can usetype(ele)==int
if ele%2!=0:
sum+=ele
print(sum)
'''
Step 1: Initialize sum = 0
Step 2: Begin iterating over each element in the list.
Step 3: First iteration (ele = 2)
Check if ele is int → True
Check if ele is odd (2 % 2 != 0) → False
sum remains 0
Step 4: Second iteration (ele = 3)
Check if ele is int → True
Check if ele is odd (3 % 2 != 0) → True
Add ele to sum: sum = 0 + 3 = 3
Step 5: Third iteration (ele = 5)
Check if ele is int → True
Check if ele is odd (5 % 2 != 0) → True
Add ele to sum: sum = 3 + 5 = 8
Step 6: Fourth iteration (ele = 4)
Check if ele is int → True
Check if ele is odd (4 % 2 != 0) → False
sum remains 8
Step 7: Fifth iteration (ele = 6)
Check if ele is int → True
Check if ele is odd (6 % 2 != 0) → False
sum remains 8
Step 8: Loop ends after all elements processed
Step 9: Print sum → 8
'''