PY
py
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
"""Lets say you need to work with the individual numbers in an integer. I'll explain how I go about it. There should be better ways though...😁😁😁 Don't mind my variable naming. I suck at it.
"""
'''I want to add the three numbers in 123'''
a = int(input()) #enter number here
#convert it to a string
b = str(a)
#loop through b, converts it back to integer then add them with a list comprehension
c = sum([int(i) for i in b])
print(c)
#or print straight up with:
#print(sum([int(i) for i in b]))
#or use normal😁 loop
sum = 0
for i in b:
sum = sum + int(i)
print(sum)
'''you can do all sort of things with the string version of the integer. '''
print('3' in b)
print('4' in b)
print(b.endswith('3'))
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run