Here is the code I wrote:
a = "5,3,1"
print (type(a))
b = []
b = a.split(",")
print (type(b))
print (b)
for i in b:
if (int(i) % 5 == 0):
print (i)
print (type(i))
In the for loop, I was expecting the variable i
to be an integer type. But turns out that it is a string. How does this make sense?
Output
<class 'str'>
<class 'list'>
['5', '3', '1']
5
<class 'str'>
How can we use a string as an iterator to loop through the list items in a list?
Further if I remove the int
in the line if (int(i) % 5 == 0):
, I get this error:
TypeError: not all arguments converted during string formatting
I'm unable to understand how this works