-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforsoevendigstr.py
More file actions
53 lines (45 loc) · 1.3 KB
/
forsoevendigstr.py
File metadata and controls
53 lines (45 loc) · 1.3 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# find sum of even digits present in string
s=input('enter a string:') # s='hai2025'
sum=0
for ele in s:
if ele.isdigit():
d=int(ele)
if d%2==0: # we can use if ele in '2468':
sum+=int(ele)
print(sum)
'''
Here are the iteration steps for the given code with example input string s = 'hai2025':
Step 1: Initialize sum = 0.
Step 2: Begin iterating through each character in the string s.
Step 3: First character 'h':
ele = 'h' is not a digit (ele.isdigit() → False).
Skip addition, sum remains 0.
Step 4: Second character 'a':
ele = 'a' is not a digit.
Skip addition, sum remains 0.
Step 5: Third character 'i':
ele = 'i' is not a digit.
Skip addition, sum remains 0.
Step 6: Fourth character '2':
ele = '2' is a digit.
Convert to int: d = 2.
Check if even (2 % 2 == 0) → True.
Add to sum: sum = 0 + 2 = 2.
Step 7: Fifth character '0':
ele = '0' is a digit.
Convert to int: d = 0.
Check if even (0 % 2 == 0) → True.
Add to sum: sum = 2 + 0 = 2.
Step 8: Sixth character '2':
ele = '2' is a digit.
Convert to int: d = 2.
Check if even (2 % 2 == 0) → True.
Add to sum: sum = 2 + 2 = 4.
Step 9: Seventh character '5':
ele = '5' is a digit.
Convert to int: d = 5.
Check if even (5 % 2 == 0) → False.
Skip addition, sum remains 4.
Step 10: Loop ends.
Step 11: Print the final sum = 4.
'''