So I was given a list and I must print the type of each item in the list. I can clearly see that there are strings and integers but I need it to print out in Python. We just learned for loops so I feel like that is what they are looking for but I cannot get it to print out.
ls = [type(item) for item in list_of_items]
print(ls)
Essentially, the type
function takes an object and returns the type of it. Try the below code:
for item in [1,2,3, 'string', None]:
print type(item)
Output:
<type 'int'>
<type 'int'>
<type 'int'>
<type 'str'>
<type 'NoneType'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With