# More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)

## 100 thoughts on “More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)”

1. CS Dojo says:

And here are the courses I mentioned at the end of this video:
Introduction to Data Visualization with Python: https://goo.gl/fZ5oVX

2. BlueMushroom says:

Can someone explain the answer for Tutorial 6 why he set
j = len(given_list) -1?
Timestamp around: 6:39

3. Anikan88 says:

4. Nhan Le says:

someone could explain for me how to compute the sum of all the negative numbers in " given_list" ?? I don't understand why must we write that to get the result ?

5. Gabriel Andy Fono says:

h

6. Nanditha krishna says:

I cannot understand this …………….pl help me………………..pll

would you sell your course for bitcoin?

8. Matthew B says:

Just wow, spent most of this week trying to figure out this negative number problem. Please give us some more simple problems! This has been great. Thank you.

9. Norman Seye says:

why would i get different result?
d = list (range(1, 100))
total11 = 0
for i in range(len(d)):
if i % 3== 0:
total11 += i
elif i % 5 == 0:
total11 += i
print (total11)

result becomes 2219

10. MEGA MODZ says:

t5=0
for element in reversed(list):
if element >=0:
break
t5+= element
print(t5)

11. Ankit kumar Akkiredi says:

could you pls do the last problem in the for loop method pls

12. David Kristian says:

given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
i = 0
for element in given_list3:
if element <= 0 :
total6 += given_list3[i]
i += 1
print(total6)

13. Enes Bingöl says:

at 3:11 what caused apple to be printed once,banana to be printed twice and republic to be printed thrice?
also at 7:00 why did he write j-=1 ? what did he do by doing that or what would've happened if he didn't do that?

14. Izzy McCartney says:

I see that you do this full time and that’s awesome. it could really help you out if you start to monetize your videos. this way, you can earn a little extra income.

15. Dba CFO says:

bro you on tut 7 and no tutorial on input() yet my god, check out the courses on edx.org i like the one from microsoft it's free

16. Gonçalo Marques says:

given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0

for element in given_list3:
if element < 0:
total += element

print(total)

-17

It gave -17 aswell, is this correct?

given_list3=[7,5,4,4,3,1,-2,-3,-5,-7]
total6=0
k=0
while k<len(given_list3) and given_list3[k]<0:
total6+=given_list3[k]
k+=1
print(total6)
this code output is zero why?
what is wrong with this

18. Kyler Crank says:

just figured out a shorthand for running the code: shift + enter

19. Wookr Louy says:

Your voice looks like SpongeBob that's awesome bro!

20. travis lam says:

what is len(a) and len(given_list)

21. lukeaboom says:

Is there a way to continue on a sequence in a list? For instance if I have the list [1, 2, 4, 7, 11] is there a way to continue that sequence? thanks

22. jeff lim says:

Hi CS Dojo,
this is another alternative to the solution that you posted.

given_list2=[7, 5, 4, 4, 3, 1, -1,-2, -3, -5, -7]
total2=0
i=len(given_list2)-1
while True:
total2+=given_list2[i]
i-=1

if given_list2[i]>0:
break

print(total2)

23. Tech It Right says:

cs dojo's lessons are simple and easy.
That's why i learned how to code python in a week or two.
before this awesome step in python, i didn't know how to code in it at all.

24. THE PRO NOOB says:

can you explain the
for j in range(i + 1):
like this
j=1,t2=0
j<5
t2+=j
j+=1

1<5
t2(0)+=1,t2=1
j(1)+=1,j=2

2<5
t2(1)+=2,t2=3
j(2)+=1,j=3

3<5
t2(3)+=3,t2=6
j(3)+=1,j=4

4<5
t2(6)+=4,t2=10
j(4)+=1,j=5

25. Tri Dao says:

I do not understand. How does the second solution = -17.
j = len(given_list) – 1, doesnt that mean J = 9. Then the while loop would start at j = 9 which excludes the last number of the list.

3:02 was confusing as fuck… but i understood it after a while lol

27. Rajesh Pardha says:

@CS Dojo ,Brother I think for loop in j, when i = 0 that means j = 1 not 0. It has to be corrected if I'm not wrong.

28. Muzammil Imran says:

i cant understand the idea of nested for loops so well 🙁

29. Genoville says:

Hey CS Dojo, I found this method of finding the sum of the multiples of 3 & 5 on the net. Sum({*range(1, 100, 3)}|{*range(1, 100, 5)}). It'll be great if you could explain it.

30. Benjamin says:

where's the 6 th series?

31. Ks littleworld says:

What about using a for loop to iterate over a bunch of shapefiles you found previously to be able to print out each filename?

32. Kevin Li says:

the "i +1" loop part, I tried that in pycharm, and it doesn't work out like the one in the video( 1 "apple", 2 "banana", 3 "republic"), instead it keeps giving me error shows there is something wrong with the code

33. Katravath Shiva says:

Can you please start video on Django Framework YK?

34. Sanjay Pal says:

For the solution of tutorial 5 we can write
for I in range [1,100]:
if I%3, I%5 ==0
total3+=I
print(total3)

35. Cracking Bunkertank says:

thanks CS Dojo! Your solutions and the way you explain things is so simple and effective! Occam's Razor in action

36. Tanay Karumanchi says:

I'm getting a star in the place where the line number is supposed to be can somebody please help?

37. Jesus Salinas says:

Does anyone know why for the first example in the if-statement if “else” is used over “elif” a different output results?

38. Nick Rinaldi says:

Thanks for the videos! Few questions about the practice problems.

What is the purpose of assigning the variable 'total' and 'total2' to = 0 in both problems? There exists not much explaining as to why making those variables equal to 0 helps the code run. To some real newbie beginners like myself, we want to understand all this code conceptually too. So if anyone could explain that, it'd be much appreciated.

hey yk 🙂 i used the for loop for the tutorial 6 problem and it is much more simpler than yours i think ..take a look .. Thanks ..
given = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

total = 0

for i in given:

if i < 0:

total += i

print(total)

40. Mrinmoy Shovon says:

What is the difference between
the below code?

if (i% 3 or i % 5) ==0:
if i % 3 == 0 or i % 5 == 0:

41. pcproff says:

Loved these problems

42. Boris says:

I did the second exercise using for loops on my own!! I think I'm a genius!

43. Tech Wizard says:

Hi Would the following also be a correct solution:

given_list3 = [7, 5, 4, 3, 2, 1 , -2, -3, -5, -7]

total = 0

increment = -1

while given_list3[increment] < 0:

total += given_list3[increment]

increment -= 1

print(total)

44. Smriti Das says:

given_list = [7, 5, 4, 4, 3 , 1, 2, -3, -2, -3, -5, -7, -1]

total = 0

i = -1

while given_list[i] < 0:

total += given_list[i]

i -= 1

print(total)

For me, this is also working fine. Since I have taken i = -1 the loop will start in reverse order i.e., from -7 in this case.

45. Sol Warda says:

One thing CS Dojo never mentioned in any video is the "runtime" in each cell after it is executed. It would be very nice to see the time displayed at the bottom of each cell automatically after it is executed. I have tried about a dozen recommendations from the Internet, but none of them works! Any reliable suggestion would be greatly appreciated. Thank you.

46. Kunal Gupta says:

apple banana and republic?
wtf?:'''D

47. Nabil Mohareb says:

do you think this code might be shorter than using for loop: print (a[0], a[1]*2 , a[2]*3)

48. AUSTIN DILTS says:

49. Abbishek Verma says:

given_list3 = [7, 5, 4, 3, 3, -2, -3, -5, -7]

total5 = 0

for z in given_list3:

if z <= 0:

total5 += z

print(total5)

cant we do like this answer is same

50. Mishra Man says:

I think I missed it is the past videos but what is the 'len()' function?

51. Christy N says:

can someone please explain the concept of the inner for loop and why a[I] is printed out once, twice and three times accordingly? p.s. you're videos are amazing, thank you so much

52. Hash Le says:

Hi YK,

for the problem in Tutorial 6 can u see where my mistake is?

given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7]
total6 = 0
j = 0
while given_list3[j] < 0 and j < len(given_list3):
total6 += given_list3[j]
j += 1
print(total6)

this gives me 0, is it because the first index causes it to auto-break?

53. Troy Arledge says:

I can break down each line of the code in his example for the for Loop inside of the for Loop and still can't manage to recreate it without coming straight from memory. I understand that he used the index of each string and added one to it to get how many times it would print ie. Banana in index position 1+1 print 2 times and so on.. but I just can't fully relate all of the info in my head lol

54. David Bui says:

FYI, public libraries usually offer free lynda.com access if you guys wanna study the course

gl9 = [7, 5, 4, 4, 3, 1, -2, -3, -5. -7]

total9 = 0

j = len(gl9) – 1 #line 4

wgile gl9[j] < 0:

total2 += gl9[j]

j -= 1 #line 7

print(total9)

didn't we start from the end of the list to avoid the positive numbers?
weren't we supposed to write += instead of -= ?, if so then what was the purpose of line 4?

56. Gerico welde says:

when am writing this code my output is the series of all individual totals but your output is only the final total 2318 what is the reason?

57. Virgil Robinson says:

Really don’t get the (i + 1) part. Can anyone explain?

58. Camilo Carrillo says:

instead of len(list)-1 you can just set index = -1 and it will start at the end of the list.
total = 0
index = -1
while list[index] < 0:
total += list[index]
index -= 1
total

59. Marco Panzanni says:

Great tutorials =D

60. Victor Vega says:

Has anyone gotten " In [*] : " next to the cell (where you type your code)? What does the * mean?

This would come up after running a cell code that I was looking to print my answer. Nothing gets printed and it moves on to the next cell… ?

My solution was closing the kernel and restarting jupyter notebook.

61. VASIREDDY DHEERAJ says:

Ok now I got it , i used javascript syntax by accident … thats why I got the wrong answer , but now I get it

62. VASIREDDY DHEERAJ says:

# did this on my own
total3 = 0
for t in range(1,100):
if t % 3 == 0 or t % 5 == 0:
total3 += t
print(total3)

63. Chieu Ta says:

I don't get the for loops in the for loops part, what's the logic behind it?

64. A a says:

at beginning why it is printed on top each other not beside ?
how to print beside?

65. All day says:

#tutorial6 , also works for non descending orders .
givenl_listrr = [ 8,7,9,-2,-3,-1]

totalo = 0

i = len(givenl_listrr)-1

while givenl_listrr [i] < 0:

totalo += givenl_listrr [i]

i -= 1

print(totalo)
-6 .

66. Victor Vega says:

Why does -1 start you at the end of the list? Wouldn't that be index 9?

Hi YK, I have one problem how one should use while loop when the list is not sorted like list= [-2,20,-43,-54]

Can anyone explain me if I have a list new_list = [23,45,-56,34,56,-67,-87,94] how can I add only negative numbers, and print the total by using "WHILE" loop? Please post ur code because I could easily do it by using for loop but stuck by using WHILE

69. Chinbalus says:

given_list4 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

total7 = 0

a = 0

for element in given_list4:

if element < 0:

total7 += element

print(total7)
-17 is this okay or wrong???

given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
w = -1

while given_list[w] < 0:
total += given_list[w]
w -= 1
print(total)

71. SHIVANG GAUR says:

What to do when we do not want to add repeated elements in first problem (#5 tutorial)?
AND
What to do when we want to add repeated elements in first problem (#5 tutorial)?

72. Xa Agripha says:

I like the stuff you cover but you need to show your work a bit more
thoroughly in terms of the math I watched the " for j in range(I + 1):" like 15 times just to get how you added the list.
I know it sounds dumb but you would make your tutorial a 100x better with aspects covering that.

73. Xuanyi Du says:

Thanks YK, really helped! Cheers from China.

74. Nokibul Islam says:

Can you please upload a whole playlist where we can be "ZERO TO HERO" in python? if you cant make vidoes ,atleast give me some links or anything from where i can be that :"(

75. Edward Lam says:

does anyone mind explaining the concept of nesting an if inside another if? 🙁

76. Daffa Anzunatama says:

Thankyou YK, very helpfull. Cheers from Indonesia!

77. NOOBDRAGON says:

if i use this code
list=["ball","cat","dog"]

for i in range(len(list)):
for j in range(i+1):
print(list[j])
i get this result:
ball

ball

cat

ball

cat

dog
CAN ANYONE TELL ME WHY

78. Xi Ming Yap says:

i still cannot understand why j = len(given_list) – 1

79. Xi Yuan says:

I cannot understand why j = len(given_list) – 1

at 4:29 value like 15 and 30 should be added twice but he gets the right answer dont know how. :/

81. Hanyu Li says:

YK, I don't exactly understand the "for j in range(i + 1)" or the solution to the problem in tutorial 6. Could you please explain the solutions? I'm having trouble reasoning and I simply don't quite understand.

82. Yogesh Gupta says:

list1=[]

N=int(input())

for i in range(0,N):

x=int(input())

list1.append(x)

print(list1)

sum=0

for i in range(len(list1)):

if (list1[i]<0):

sum=sum+list1[i]

print(sum)

83. bolivia bba says:

Why do we have to put -1 at the end

84. Shuu says:

The variable 'i' used in the problem from tutorial 5 isn't referring to the index right? Is it referring to the element in the list?

85. Nitish Kumar says:

list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
#sum of all negative numbers
total = 0
for i in list:
if i < 0:
total = total + i
print(total)

86. Susobhan Sahoo says:

for i in range(len(a)):

for j in range(i+1):

print(a[i])

Couldn't understand this, can you please explain this?

87. Ayub Haji says:

for i in range (len(a)):# 0,1,2

for j in range(i + 1):

print(a[i])

I put this exact code in and tried to run it, It give me one of each like before, I don't understand what i did wrong?, can anyone clarify my mistake?
Edit:If there are another way of doing of this code please tell me, I been trying many solutions but don't seem to work as well.

88. Andres Garay says:

Can talk with you?, i want to learn how to use python, but i dont know how its work, i need help!!! Please.
[email protected] this is my email,
I hope you have time to check out this message🙏🏽

Ty

90. achyut shahi says:

hi cs dojo,
my jupyter notebook is not showing any output

91. JARI says:

"j -= 1" = "j –"

92. toshalpha says:

Hello CS dojo, thanks for all the amazing videos. Could you by any chance make a video on some useful shortcuts for Python? I appreciate it!

93. M Gonzalez says:

How can I print a list with comma's in between of all number divizeble by 3 and as a horizontal list, not just printing the result.

94. Love Moon says:

can anyone please tell me how we can do the sum of only the negative number in a sorted list using a "for loop"?

95. Mariano says:

Hello YK I just started learning python and your tutorials have been of so much help… Can you please explain the concept behind the looping of multiples of 3 & 5 and summing them up?? (What I think is that multiples of 3 and 5 mean that we should sum only numbers which are divisible by both 3 and 5 at the same time and that will be ) 15, 30, 45, 60, 75 and 90 = 315??

96. Atharv Nuthi says:

hey cs dojo , i think the solution for the last exercise you gave in this video is a little complicated , i tried it in a different way, i got the same result , please next time , try to give easier solutions , by the way thanks for these videos , its really helping me

97. Odean Mcfury says:

for the second exercise this worked too

total9 = 0
for i in a:
if i < 0:
total9 -= 0 – i
print(total9)

I have also solved it using for

99. Nick Wiley says:

So I definitely was overthinking the first exercise and got it wrong….
This is what I thought the answer was lol:
a = [ ]

b = [ ]

for i in range(1, 101):

if i % 3 == 0:

a.append(i)

if i % 5 == 0:

b.append(i)

print(a)

print(b)

#ADDING THE MULTIPLES TOGETHER IN EACH LIST

total4 = 0

for i in a:

total4 += i

print(total4)

total5 = 0

for i in b:

total5 += i

print(total5)