More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
Вставка
- Опубліковано 24 січ 2018
- This entire series in a playlist: goo.gl/eVauVX
Download the sample file: www.csdojo.io/python7
The courses I mentioned at the end of this video:
Get Ready for Your Coding Interview: goo.gl/RMCaxW
Introduction to Data Visualization with Python: goo.gl/fZ5oVX
Keep in touch on Facebook: / entercsdojo
Subscribe to my newsletter: www.csdojo.io/news
Support me on Patreon: / csdojo
You can download the sample file for this video here: www.csdojo.io/python7
And here are the courses I mentioned at the end of this video:
Get Ready for Your Coding Interview: goo.gl/RMCaxW
Introduction to Data Visualization with Python: goo.gl/fZ5oVX
CS Dojo more videos r coming on python or it's the end
Please make a video of make a small project in Python
In #5, the problem you pose is actually:
# Can you compute all multiples of 3, 5
# that are less than 100?
It doesn't mention finding the sum, so:
r = list()
for n in range(1, 100):
if (n % 3 == 0):
r.append(n)
elif (n % 5 == 0):
r.append(n)
print(r)
Ur content is super amazing and accurate.. No extra explaination 😄
2²222223²2222232²2
"if you don't want to wait for the next tutorial"
me, watching them 3 years after they were uploaded: yeah...
same
same here
me being here 6 years later lol
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
thanks this helped me out
Ty, helped a lot
ily this helped me so much
THANK YOU!
TYSM!
just figured out a shorthand for running the code: shift + enter
Finally! It's been getting annoying to keep on pressing run. Thanks
I'm new too... i noticed when i shift+enter the "In[num] increments everytime no matter what cell I am in.
Wondering if this action is more than just running the specific cell 🤔
I found another one that works, on mac; cmd + shift + q + enter, on windows; alt + f4
You just saved my life
Thanks dude.
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)
It is simpler - only thing is that you are iterating through all the positive numbers first instead of starting with the negative numbers in the list.
@@samore11 Hey, what would you do if there were negative numbers in between the list and not just at the beginning or ending. I tried his and my own solutions to this problem with while, if else statements but it just doesn't seem to fully work that way. using 'for' worked for me in this regard very easily.
@@sujaljain2436 do you mean if the data was not sorted? This works for that case:
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
i=0
while 1==1:
if given_list3[i] < 0:
total6 += given_list3[i]
i+=1
if i == len(given_list3):
break
print(total6)
and vice versa
very simple but why are we told to use the while loop
@@samore11 i checked many comments but only your solution worked for me. Thanks, I'm grateful.
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
Yes it works , and its easy to accept . Thanks .
Much easier tanks
@@olowoseadebola2069 In [ ]: sum(i for i in given_list3 if i < 0) #dont need to add print
it works! thanks
ty
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.
I never thought it would be this much easier to learn for a beginner like me... You are awesome..thanks for the videos
Amazing! I ve learnt so much today! Please carry on that way CS Dojo.
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.
me too
yup. I've watched every video up to this one 2 or 3 times so far. i effin love this guy!
thanks CS Dojo! Your solutions and the way you explain things is so simple and effective! Occam's Razor in action
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"
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. :-)
Thank you CS Dojo, i am learning more from your channel.
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
so how is ur uni?
@@gbnepali I enjoyed the CS coursework and did well in my first programming assessment but ended up switching to a more people-orientated degree (paramedicine).
Absolutely loving it! Thanks YK :)
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!
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.
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)
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
list = given_list[::-1]
for i in list:
if i >= 0:
break
total += i
print(total)
It's a little more complicated but I figured it can be a good practice to get in handle of break statement
@@sentinelbl91 what the purpose -1 in variable list
nice ... that with for loop
As he said, that would go through all the numbers in order. If you have a list of 2 million numbers and only the last two are negative, you would need to go through almost the entire list to get somewhere
He is always there to help us for free. I have seen UA-camrs 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.
Thankyou YK, very helpfull. Cheers from Indonesia!
Thank you so much, keep it up CS Dojo.
I took a basic logic course and learned the difference between finding things in union, such as multiples that fit two numbers (one AND the other) and finding intersecting points, such as multiples of two numbers (either one OR the either). Seeing the basics of python so far, it seems like this logic is best understood for future work assignments. This is all stemming from the explanation of tutorial 5's practice problem where I realized we all had different answers mostly from the way the problem was asked by YK. Like any new knowledge, it's best to see WHY people got different answers and HOW they did rather than boasting about who understood YK's way of presenting the problem.
This will effectively expound on one's foundation for learning programming concepts.
Cheers
thank you Dojo, for you works, if you can upload more tutorials about Python, it will be great!
Great content as always! Thanks
It's been great. Thanks for the help, I really appreciate it.
I did the second exercise using for loops on my own!! I think I'm a genius!
Thank you so much sir; great content as usual!
Actually a good series known python in a very simple way
Thank you so much for explaining the previous problems! =))
I'm not disappointed at all for the pace of your course.
I think the videos have the nice length furthermore.
In order to learn Python properly, I think that no more than 2 lessons a week should be made.
Bruh and here i am trying to learn a lesson per day
Everyone has their own learning curve u can attempt any pace.
@@sisamghale866 yup yup
Thanks YK, really helped! Cheers from China.
Thanks mannn. you're a really good teacher
Thanks man, You are a great teacher.
Loved these problems
YK ;) thnx ! I like small bit size vids along with the longer ones, both help.
Wow. Should of thought of initializing my index as the final number like you. Very helpful.
I went a very roundabout way of doing it haha, but it still worked.
This is the code i used:
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] #you don't actually know the values. Only that they are descending.
total = 0
index = 0
while index < len(given_list):
if given_list[index] >= 0:
index += 1
else: # OR elif given_list[index] < 0:
total += given_list[index]
index += 1
print(total)
-17
love the video so helpful and clear
Great tutorials =D
Hey CS Dojo, just wanted to say that during the range(1, 100) and earlier u didn`t told about ELIF :P or i missed it :/ nor the OR btw. But if we assume that we could use ELIF i personally did this this way:
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total3 = 0
i = 0
while i < len(given_list3):
if given_list3[i] > 0:
i += 1
elif given_list3[i]
Great vid. Thanks.
good tutorial, thanks.
you can use negative indexes to reference items from right to left
Thank you so much. 😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘
for i in range(len(a)): #0,1,2
for j in range(i+1):
#i=0 --->j=0 #i=0,run a[0] once, print a[0], then go back to for loop i=1--> a[1]
#i=1---->j=0,1 #j in range 2, print a[1] twice, then back to for loop i=2-->a[2]
#i=2---->j=0,1,2 #j in range 3, print a[2] 3 times
print(a[i])
I hope I need this for data analysis
Hi, you have very good tutorials. It would help the learning process if you wrote down the original problem statement before writing the code!
You are awesome!
Im having a hard time understanding loops :(
maybe you should watch the video over and over again
just put it on replay until you understand or ask questions in the comments people are ready to help
@@arkamondal6049 I see what you did there
@@filippians413 huh? he just recommended what he usually does when he does not understand a topic.
@@prodigymedia1659 I do it too when I don't understand something. It really a good way of understanding complex topics
hey YK instead of such a long method we can use this method:-
a = [7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7]
tot = 0
for i in a:
if i
Thank you ☺
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
i = (len(given_list) - 1)
while i < len(given_list) and given_list[i] < 0:
total += given_list[i]
i = i - 1
print (total)
It's correct to put the i < len(given_list) condition in the loop? Otherwise you'd get an error runnin out of the index, if there's not positive number in the list
#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 .
Thanks yk its really helpful
I like this series! When correcting exercise 6, I am not sure to fully grasp the reason it was that technique instead of: In [ ]: sum(i for i in given_list3 if i < 0) ,was it because of speeding the process if a lot of data?
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.
no pace of you ,its interesting please more on python
# printing apple 1 times, banana 2 times, apple 3 times
# while loop
a=['apple','banana','orange']
j=0
while j
you can also do this way to find the sum of all negative integers
list = [7,5,3,1,-1,-3,-5,-7]
sum = 0
for i in list:
if i>0:
continue
sum+=i
print(sum)
Think I understand the 3:10 example better now. I'm pretty sure that the integer value the iteration(i) of the for loop stores in contingent on the array it's going through. So for example, the integer value of the first iteration of range(3) will be 0, but for range(1, 4), it will be 1. I hope that makes sense. In other words, the integer value of the iteration of the for loop is dependent on the array (list, range etc.) it's iterating through, it literally gets the value from the array.
i had this solution for summing the negative numbers, seems clean
b = somelist
t2 = 0
for i in b:
if i * -1 > 0:
t2 = t2 + i
print(t2)
Do you have a video explaining how to add up individual integers of a single value for example input = 451, output = 10? I have found some solutions to this, but I am not understanding the overall meaning and process. Thank you. I love your videos.
Thank you.
for loop is my fevorite wow
Thanks sir
you should add to this playlist :( im almost done w it
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)
a=("badger", "mushroom", "snake", "oooh its big!")
for i in range (10):
print (a[0])
for i in range (2):
print (a[1])
for i in range (2):
print (a[2])
print (a[3])
more on python please
Can you make a video on Matrices(3x3 matrix), addition,.. using for loop?
Thanks for the video! That was such an smart answer compared to my solution.
New to programming. Thanks for all great lessons. Which of the loops is more appropriate to write a function that accepts an exponent and returns the corresponding Mersenne number . I can't seem to get get it. How can you help?... pls...
Thank u so much.😊😊❤️ YK
Can u tell me normally,how long it takes to learn python to get into the advanced level ?
@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.
thank you
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
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?
i do the exercise seems more easy like this:
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
i = 0
while i < len(given_list):
if given_list[i] < 0:
total += given_list [i]
i+=1
print(total)
it's easier to understand and looks shorter as well. I hope I did it correctly.
for those to use "while loop", "for loop" is simple as commented
What about using a for loop to iterate over a bunch of shapefiles you found previously to be able to print out each filename?
You rock!
@CS Dojo: I have tried the following
t= 0
for i in range(1,100):
if i % 3 ==0 or i % 5 ==0:
print(i)
t += i
print("total:",(t))
Will you make videos regarding data visualization on UA-cam in future?
given_list = (5, 9, -12, 15, -15, -20, -7, -1)
totalx = 0
for i in given_list:
if i < 0:
totalx += i
print(totalx)
Who else loves the way he says banana? lol
Hi Cs Dojo , what hardware do you use ?
if you got struggled with the nested for loops, consider that
for a in range(4):
print("CS dojo")
==>
CS dojo
CS dojo
CS dojo
CS dojo
I real like much your tutorial ,do you have any other languages like php,MySql,Js, etc to do same
hi = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
i = -1
while hi[i] < 0:
total += hi[i]
i -= 1
print(total)
knowing that the last elment on the list is considered as -1 was so helpful
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
basically if i = 0 then i + 1 = 1 so you go back to the original list 1 time and that value is 0
Ty
Hi Dojo,
Can you please help me on below problem:
This is my list:
given_list2 = [5, 4, 3, 2, 1-1,-2,-3,-5]
print (given_list2)
When I tried to calculate the sum of negative integers using below code:
i = 0
total4 = 0
while given_list2[i]
for the second exercise this worked too
total9 = 0
for i in a:
if i < 0:
total9 -= 0 - i
print(total9)
My solution to Tutorial 5's assignment was:
hundred = list(range(0, 100))
total = 0
for element in hundred:
if element % 3 == 0 or element % 5 == 0:
total += element
print(total)
output was 2318
my solution to Tutorial 6's assignment
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
minusTotal = 0
ei = - 1
while list[ei] < 0:
minusTotal += list[ei]
ei -= 1
print(minusTotal)
i am done with python ..........but the loops thing is damn hard for me .......good luck!!!!
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.
love your videos..
will u publish soon another interview? please!
I did it this way :
total = 0
i = -1
while given_list[i] < 0:
total += given_list[i]
i -= 1
print(total)
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??
Why did you use if( i%3==0 or i%5==0) instead of if (i%3==0 and i%5==0)? as the question asks??
Could someone explain the loop inside of the loop 3:09 in more detail? I don't understand why the results are like that and especially don't get how the "i + 1" part is working.
I'll try. Think of this code as a bowl of fruits to put on the table.
1st time: take a fruit from the bowl to put on the table.
2nd time: take a different fruit from the bowl to put on the table and do it again once.
3rd time: take a different fruit from a bowl to put on the table and do it again twice.
And so on as long as the number of fruits within the bowl.
a = [0,1,2,3,4]
for i in a:
for j in range(i+1):
print(i,j)
i j
0 0 # initially i = 0, and j = range(i + 1)= range (0+1) = range(1) so for this
value of j for this will be 0
1 0 # after that i = 1, and j = range(i + 1)= range (1+1) = range(2) so for this
values of j for this will be 0, 1
1 1
2 0
2 1
2 2
3 0
3 1
3 2
3 3
4 0
4 1
4 2
4 3
4 4
(sorry for my english, im not a native speaker and i might have a wording mistake)
the lines with the "#" are just to explain the code
So let's analyze the code part by part
# so what the first line is doing is that for any value in range of the length of "a" (which is 3), is iterating what's inside # the i loop. Since we know that the length of a is 3 is easy to know that this first loop is going to iterate 3 times with # the values of 0, 1, 2
for i in range(len(a)):
# below this i loop we have another loop.
# See that what the next two lines are doing is printing the element [i] of the list "a" i+1 times.
# So if i = 0 you are printing the element 0 of "a" 0+1 times
# if i = 1 you are printing the elemtn 1 of "a" 1+1 times
# if i = 2 you are printing the element 2 of "a" 2+1 times
for j in range (i+1):
print(a[i])
comments:
-Remember to take in count 0 for iterations because even though the value of len(a) is 3. You are not iterating i in values of 1, 2, 3 you are iterating i in values of 0, 1, 2
-Be careful with the condition of the second loop. We are just doing i + 1 because if you were to iterate "for j in range(0)" it would do nothing, but if you were to do "for j in range(0+1)" it would iterate the loop once. i + 1 Doesn't mean we are printing the element a[i+1] also.
- never forget that the first element of a list is always 0 not 1.
I hope i was as clear as possible, im also a begginer but just wanted to help. I tried to cover any doubt you could have.
Thanks for your help!
Thank you!