So, there are two small questions here:
a)
my_list = ['apple', 'banana', 'grapes', 'pear']
for c, value in enumerate(my_list, 1):
print(c, value)
Step as follows:
enumerate(my_list, 1) will get a list with index, here the output is a enumereate object, if use list(enumerate(my_list, 1) to have a look, it is [(1, 'apple'), (2, 'banana'), (3, 'grapes'), (4, 'pear')].
- So, with every
for, the first iterate get c=1, value='apple', the second get c=2, value='banana' ...
- Then the final output is:
1 apple
2 banana
3 grapes
4 pear
b)
[print(int(x)==sum(int(d)**p for p,d in enumerate(x,1)))for x in[input()]]
Step as follows:
- First, it's a list comprehension, I suppose you have known that.
- The
input first expect a user input, let's input 100 for example, then the input will treat it as a str, so [input()] returns ['100']
- Then with
for x in [input()] the x is '100'
- Next according
list comprehension, it will handle int(x)==sum(int(d)**p for p,d in enumerate(x,1))
(int(d)**p for p,d in enumerate(x,1)) will first iterate '100', get something like [(1, '1'), (2, '0'), (3, '0')] if use list to see it, just similar as example 1. Then calculate int(d)**p for every iterate and finally use sum to get the result, similar to int('1')**1 + int('0')**2 + int('0')**3, the result is 1.
- So
print(int('100')==1 certainly output False
- And the return value of
print function call is always None, so list comprehension will make the new list is [None].
- So the final outout is (NOTE: 100 is the echo of your input):
>>> [print(int(x)==sum(int(d)**p for p,d in enumerate(x,1)))for x in[input()]]
100
False
[None]