python - What does this line do? (using num and enumerate) -
i'm python beginner, , have been given code write out each line does. here code:
z = [int(input("enter nth term sequence ")) _ in range(12)] total = sum([num * 8 if % 2 == 0 else num * 4 i, num in enumerate(z)])
so, assume z list, user inputs numbers of nth term sequence, there 12 digits can enter. code finds sum of numbers multiplied 8 if divided 2 gives remainder of 0. don't ([num * 8 if % 2 == 0 else num * 4 i, num in enumerate(z)])
means. i've tried , have attempted search things up, it's confusing. please if possible
the code:
foo = [num * 8 if % 2 == 0 else num * 4 i, num in enumerate(z)]
is functionally equivalent this:
foo = [] i, num in enumerate(z): # num element in z, index of num if % 2 == 0: foo.append(num*8) else: foo.append(num*4)
then user calls sum
on result.
answer question?
edit: explain enumerate
you should used reading documentation on functions, example enumerate
. after you've read it, if still don't understand should search stackoverflow. example: a post asking enumerate answer
calling:
for num in z: = z.index(num) # stuff
is functionally same saying:
for i, num in enumerate(z): # stuff
Comments
Post a Comment