r/pythonhelp • • Jun 15 '19

I suck at python so pls help

ok so i just started out on python 2 weeks ago and my homework is

c = [(11,22,33), (44,55,66), (77,88,99),(102,103,492), (222,333,444)] print out the sum of c print out the sum of the second, third, and fourth items in c.

2 Upvotes

6 comments sorted by

View all comments

1

u/[deleted] Jun 15 '19

So you can parse through lists in a decent way, and I suggest you use the built-in method sum to make it easier

c = [(11,22,33), (44,55,66), (77,88,99),(102,103,492), (222,333,444)]

#Parse through the whole list
for item in c:
    #Prints the sum of each items
    print(sum(item))

Output:
66
165
264
697
999

2

u/[deleted] Jun 16 '19

thanks bro

1

u/[deleted] Jun 16 '19

Happy to help!

2

u/[deleted] Jun 16 '19 edited Jun 16 '19

hmm how could i print out only the 2 third and fouth item?

1

u/[deleted] Jun 16 '19
c = [(11,22,33), (44,55,66), (77,88,99),(102,103,492), (222,333,444)]

#Parse through the whole list
for item in c[1:4]:
    #Prints the sum of each items
    print(sum(item))

Do you see that [1:4] right next to the c? The 1 says to start at the 2nd element in the list, and the 4 says to stop at the 5th, but not include the 5th.

2

u/[deleted] Jun 16 '19

Thank you!