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

preview_player
Показать описание


The courses I mentioned at the end of this video:

Рекомендации по теме
Комментарии
Автор


And here are the courses I mentioned at the end of this video:

CSDojo
Автор

"if you don't want to wait for the next tutorial"
me, watching them 3 years after they were uploaded: yeah...

monarchofrymden
Автор

Maybe this will help someone understand 3:10 . It took me a long time to figure out.

We can divide the code in 2 parts:
# 1 - Outside Loop
for i in range(len(a)):
# 2 - Inside Loop
for j in range(i + 1)
print(a[i])



Outside loop based on range(3) means there are 3 items in the range (0, 1, 2) and the code inside the loop will run for each item in the range:
for 0
run Inside loop
for 1
run Inside loop
for 2
run Inside loop



Now figure out the inside loop for each element of the Outside loop. Remember j is just the variable that denotes each item in the range. It's not about calculating the value of j but how many js there will be. How many js there will be is given by the range(i + 1).


- range for i = 0 -> i + 1 = 1, range(1) means the inside loop will run only once because there is only one item in the range, this item has a value of 0. The value 0 itself doesn't matter because it still counts as one item.
- range for i = 1 -> i + 1 = 2, range(2) means the inside loop will run twice because there are 2 items in this range, these items are 0 and 1
- range for i = 2 -> i + 1 = 3, range(3) means the inside loop will run three times because there are 3 items in this range, these items are 0, 1 and 2

Artdeto
Автор

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

kylercrank
Автор

Personally, adding the negative numbers is simpler with a for loop.
list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total3 = 0
for num in list3:
if num < 0:
total3 += num
print(total3)

pilesTV
Автор

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

camilocarrillo
Автор

I never thought it would be this much easier to learn for a beginner like me... You are awesome..thanks for the videos

shubhindia
Автор

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.

henrybs
Автор

Amazing! I ve learnt so much today! Please carry on that way CS Dojo.

anvegar
Автор

Even if you are going at a slower pace I just repeat the series so I can really understand the idea and concepts and stuff I missed on first viewing. Its very informative and should be viewed multiple times to really get what you cover in these vids keep it up.

vindicatedafi
Автор

Thanks CSDOJO for your python series videos. Am understanding a lot now with it. Please do one more videos on loops, for better understanding. Thanks.
Paul from "csdojogang"

paulinz
Автор

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

dumbfoundtown
Автор

Thanks you are a great guy for helping out so many people.
(Interesting your first python video has 9k likes and the 7th has 315, it is like the videos on how to solve rubick's cube.)
Wish you all the best. :-)

laurentiustefan
Автор

This is the best python for beginners series I have found on youtube. please please keep it up :). I'm doing computer science at uni this year :0

Squiderrant
Автор

Thank you CS Dojo, i am learning more from your channel.

biggee
Автор

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)

nitish
Автор

He is always there to help us for free. I have seen YouTubers who make coding videos and then say to join their paid course but YK is giving his courses for free I have liked each and every video in both the channels.

cakanwalarora
Автор

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.

techitright
Автор

dude its just so much fun programming i dont know how i didn't program before. Thanks for your videos they are helping me so much!

manuelocana
Автор

Thankyou YK, very helpfull. Cheers from Indonesia!

daffaanzunatama