Python Programming Series (Loops 4): Nested loops

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ค. 2016
  • A look at how to nest loops and an example of what can be done with when you do.
    www.leftpeel.com

ความคิดเห็น • 105

  • @joshuaf.5398
    @joshuaf.5398 3 ปีที่แล้ว +28

    I've been working on this class assignment for my coding class for 2 days, should've taken no more than 15 minutes. Couldn't get the code to output the way expected. Your video basically helped me finally connect the dots on how to complete the assignment! Thank you!

  • @basalduat
    @basalduat 6 ปีที่แล้ว +8

    Excellent visual presentation! Great voice. Thank you for using a large font. You are a good teacher.

  • @papie5151
    @papie5151 3 ปีที่แล้ว +4

    I was struggling with nested for loops, the range function and pattern printing until this video. I've watched several videos and this was the ah ha moment. Thanks.

  • @myenjoyablehobbies
    @myenjoyablehobbies 5 ปีที่แล้ว +17

    excellent explanation, I learned a lot as you went thru this step by step. Thank you.

  • @satyajitdas2780
    @satyajitdas2780 6 ปีที่แล้ว +3

    Excellent, Sir. I really liked your approach. It is unique and most logical. I can connect the dots after every steps .

  • @AMAerik
    @AMAerik 2 ปีที่แล้ว +1

    I had doubts for months about the shit for loop, I wish I watched this tutorial on my first programming day. Thank you bro

  • @MSMEJanardhanMuralidhar
    @MSMEJanardhanMuralidhar 2 ปีที่แล้ว

    Thank you for explaining these concepts so well.

  • @Honda-nn3ee
    @Honda-nn3ee 5 ปีที่แล้ว +10

    Awesome video. This really helped me learn nested loops. For anyone wondering how to print this triangle without using nested loops, here’s one approach:
    i=8
    for j in range(1,17,2):
    print(i*" "+j*"*")
    i=i-1
    # i is the number of spaces, and j is the number of stars being printed, which goes down by 2 each row.

  • @abyazkarim6755
    @abyazkarim6755 ปีที่แล้ว

    great video, appreciate the step-by-step thinking approach

  • @sunnyveerpratapsingh1102
    @sunnyveerpratapsingh1102 4 ปีที่แล้ว

    I have been to so many videos for understanding loops by far u are the best one . thanks pls keep up the good work I subscribed

  • @mehdighafari7672
    @mehdighafari7672 ปีที่แล้ว

    You taught us this in the best possible way

  • @rajeshbabu1972
    @rajeshbabu1972 4 ปีที่แล้ว

    Very clean and neat presentation.. Its really very helpful..

  • @ananthbyju
    @ananthbyju 3 ปีที่แล้ว +1

    Keep up the Good Work Mate! Understood the concept...Thanks!

  • @danielbriller3714
    @danielbriller3714 2 ปีที่แล้ว

    it was a nice, simple, and quick explanation, i understood it very quickly

  • @julianash3379
    @julianash3379 3 ปีที่แล้ว

    iv been trying to solve this for a day. Thank you so much

  • @alexhicks930
    @alexhicks930 6 ปีที่แล้ว +17

    You are my savior!!

  • @deadbutt
    @deadbutt 2 ปีที่แล้ว

    Clear and objective. Really good video, thank you man, it helped a lot! Liked and subscribed!!

  • @reyesdelamakenyistiwar2872
    @reyesdelamakenyistiwar2872 ปีที่แล้ว

    Thank you very much, I was able to learn this subject better thanks to your help.

  • @davidusharauli2227
    @davidusharauli2227 6 ปีที่แล้ว +2

    why does nested for loop iterates 7 times? outside for loops iterates 7 times * nested range but why nested loop should do it 7 times too? should not it loop only 1 time as an independent loop would do?

  • @BrendanMetcalfe
    @BrendanMetcalfe 4 ปีที่แล้ว

    those 2 for loops at the same indentation - think its possible to do that in a list comp?

  • @adrianeduardo6222
    @adrianeduardo6222 4 ปีที่แล้ว

    what a nice/clear way to teach. ty

  • @marcusmorrow3900
    @marcusmorrow3900 5 ปีที่แล้ว

    This was beautiful. thanks.

  • @bongajewelmsimang5337
    @bongajewelmsimang5337 8 หลายเดือนก่อน

    Thank you for the great explanation , this really helped me intepret another project that was using different symbols. defintely gonna practise the pyramid for fun though.

  • @dp0813
    @dp0813 5 ปีที่แล้ว +12

    Great vid! I got this in 4 tries, but my code was a little cleaner just because I used a variable ('x') for the number of 0s printed:
    x = 1 # number of 0s
    for i in range(0,7): # number of rows
    for j in range(0,7-i): # number of spaces over, starting with 7 but decreasing by 1 each time
    print(' ', end = '')
    print('0' * x) # number of zeros to print on each line
    x += 2 # increment the number of zeros by 2 to get increasing, consecutive odd numbers for each row (1,3,5,...)

  • @bdaman_2595
    @bdaman_2595 4 ปีที่แล้ว

    Thanks! This vid helped a lot!

  • @shivanshdadhwal6989
    @shivanshdadhwal6989 5 ปีที่แล้ว

    pls give a link of nested loops questions with solutions

  • @zukofire6424
    @zukofire6424 ปีที่แล้ว

    thanks for this! loved it

  • @trevthegamedev
    @trevthegamedev 4 ปีที่แล้ว

    where are these exercises you speak of?

  • @roselinevimala9151
    @roselinevimala9151 4 ปีที่แล้ว

    Super sir your video are helpful for my online classes

  • @loganhawkins6486
    @loganhawkins6486 3 ปีที่แล้ว

    if you had a list of numbers how could you group every set of 4 numbers together? so if you had 12 numbers you would have 3 lists of 4.

  • @Michaelzonneveld
    @Michaelzonneveld 5 ปีที่แล้ว +1

    very clean

  • @jonnierobinson8579
    @jonnierobinson8579 4 ปีที่แล้ว

    How do I get the triangle upside down

  • @Ryan-bv7hb
    @Ryan-bv7hb 5 ปีที่แล้ว

    Please cover the topic for regular expression in python

  • @53strat55
    @53strat55 3 ปีที่แล้ว +1

    How did you comment the piramide all at once? Seems like a usefull hotkey.

    • @FT4YOU
      @FT4YOU 3 ปีที่แล้ว

      i can't figure it out too. there is a command key for commenting blocks of code at ones. i just don't understand it.

    • @DeeBeeGames101
      @DeeBeeGames101 ปีที่แล้ว

      @@FT4YOU me neither.

  • @muzik6808
    @muzik6808 4 ปีที่แล้ว +2

    for i in range(1,8):
    for j in range(1,8-i):
    print(" ",end="")
    print("0"*(2*i-1))

    •  4 ปีที่แล้ว

      Why 8-i

    • @muzik6808
      @muzik6808 4 ปีที่แล้ว

      @ in the pattern we left spaces then print 0.. it means after 7 spaces we have to print 0..then on the next line we have left 5 spaces then print 0.. likewise it goes on .. and because we have to print 2 more 0's on each next line thats why we multiply by 2 and decrease it by 1..

    • @stevenewbank
      @stevenewbank 4 ปีที่แล้ว

      Now that is a nice bit of code...thanks for sharing

  • @dolfinho87
    @dolfinho87 2 ปีที่แล้ว

    Amazing! Thank you!

  • @manoharsagunthalla9215
    @manoharsagunthalla9215 หลายเดือนก่อน

    Will you please tell me what mathematics involved in printing pyramid. Will explain how it works?

  • @anita_cr7
    @anita_cr7 6 ปีที่แล้ว

    In my program print(" ",end="") is not running.it say end="" syntax error .please give me a suggestion to solve this problem

    • @user-es1hu5xj5p
      @user-es1hu5xj5p 5 ปีที่แล้ว

      becaus you have 2 pythone vershion!

  • @Pankaj-Verma-
    @Pankaj-Verma- 3 หลายเดือนก่อน

    That was awesome 😎

  • @freemanfreed1581
    @freemanfreed1581 2 ปีที่แล้ว

    but how it internally implement the iteration protocol is the real magic. how does it implement it??

  • @ntwaliobadiah425
    @ntwaliobadiah425 ปีที่แล้ว +1

    for i in range(6):
    for j in range(0,6-i):
    print(' ',end="")
    for k in range(0,2*i):
    print('0',end="")
    print("0")

  • @MohammedAli-lr9rd
    @MohammedAli-lr9rd 2 ปีที่แล้ว +1

    4:16 how do you comment it?

  • @4ikemir
    @4ikemir 3 ปีที่แล้ว

    Thanks for the video

  • @dineshroyal4108
    @dineshroyal4108 5 ปีที่แล้ว

    Sir can u write a python program calendar without built in function

    • @RecursiveTriforce
      @RecursiveTriforce 4 ปีที่แล้ว +1

      You probably don't even know what a leap year is.
      You can but working with time is tedious.

  • @pectenmaximus231
    @pectenmaximus231 3 ปีที่แล้ว

    for i in range(0, 7):
    for j in range(0, 7-i):
    print(" ", end="")
    for k in range(i*2):
    print("0",end="")
    print("0")
    This worked for me. One thing I don't understand here, when I look at this on PythonTutor, is why in the k-index, on the first line (where i=0, and thus k=0 as 0*2=0), the program doesn't start the k loop, but instead jumps down to the print statement and then starts again at i=1. While this program accomplishes what I want, I don't understand what is going on. Since indexes start at 0, as for example in our for-loops, where the range starts at 0, allowing n to be what we want (since range goes up to n-1, if we start at 0, range will go to n), I wonder why, when i=0, k should also be 0, and then the print statement under k's loop should be printed? When following along in PythonTutor, k doesn't start until we get to the next line, with i=1 (so k's range is 1*2 = 2, so index of 0 and 1). So why does Python(?) require that i be a non-zero value, for a loop whose range is i*some-number? I just don't understand this, and any clarification from some knowing person would be amazing.

    • @ghassanjenainaty4212
      @ghassanjenainaty4212 2 ปีที่แล้ว

      what was your output please? It showed me some errors

  • @priscillaton7669
    @priscillaton7669 4 ปีที่แล้ว

    thank you!

  • @jeremiahv6498
    @jeremiahv6498 11 หลายเดือนก่อน

    Okay, I like to try before I watch the solution. I didn't really get the isosceles triangle you got, but I did make right triangle. I just need to work on the spaces, But I don't have time right now, so heres my code so far.
    z,x,w,v = "*" , 1 , " ", 6
    for i in range(0,4):
    for j in range(0,5):
    print(v*w+z*x)
    x+=1

  • @MANISHKUMAR-be3wb
    @MANISHKUMAR-be3wb 6 ปีที่แล้ว

    How to apply nested for loop for float numbers?

  • @lazartutulic7577
    @lazartutulic7577 3 ปีที่แล้ว

    ##'QUESTION HERE##
    for i in range(0,7):
    for j in range(7 - i) /// but yours starts at (0,7):
    print(' ', end = '')
    print('0')
    ''
    why is this code giving me the same output as yours? For me its logical that second loop starts at 7 so it prints 7 spaces at start of the first iteration.

    • @lazartutulic7577
      @lazartutulic7577 3 ปีที่แล้ว

      okay i got it. I forgot that the outer loop iterates only once, and the inner one iterates to the end of the range it haves. xD

  • @pythonocean7879
    @pythonocean7879 5 ปีที่แล้ว +8

    try this way:
    for x in range(1,10):
    print(' '*(9-x)+'*'*(x*2))

    • @RecursiveTriforce
      @RecursiveTriforce 4 ปีที่แล้ว +2

      for x in range(6):
      print( " "*(7-x) + "0"*(2*x+1) )
      In this exact problem.

    • @RecursiveTriforce
      @RecursiveTriforce 4 ปีที่แล้ว +2

      @@DeViLTh0rn why?

    • @taylorbell6879
      @taylorbell6879 4 ปีที่แล้ว

      @@RecursiveTriforce How would you resolve this code to execute range values higher than 8? I am learning, and when I use your code I see that numbers 9 and above do not make perfect pyramids, so I asked myself well what should you do to fix that and make it perfect. But my problem is I barely understand your code. On top of answering this question, if you would, can you point me to some good learning websites? How did you learn this so well?

    • @RecursiveTriforce
      @RecursiveTriforce 4 ปีที่แล้ว +1

      @@taylorbell6879
      for x in range(size-1):
      print(" "*(size-x) + "0"*(2*x+1) )
      For syntax I would recommend:
      th-cam.com/play/PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M.html
      But this is just string formatting. Draw a pyramid with dimensions and you see...
      Uneven number of 0's:
      [ "0"*(2*x+1) ]
      In front of the zeros is always one less space:
      [ " "*(start-x) ]
      After the zeros:
      Newline [implied in print]

    • @interezting3940
      @interezting3940 4 ปีที่แล้ว

      @@RecursiveTriforce this code makes error

  • @sapnamanohara2234
    @sapnamanohara2234 ปีที่แล้ว

    thank you so much..

  • @murodalisharipov5929
    @murodalisharipov5929 5 ปีที่แล้ว

    How to get rid of that last one space?

  • @031219400
    @031219400 6 ปีที่แล้ว

    thank for you this

  • @jinkazama3647
    @jinkazama3647 ปีที่แล้ว

    t=int(input("put the number here:"))
    for i in range(t):
    print(" " * (t-i-1) + "*"" " * (i+1))
    Can u tell me how to double the rectangle bro?

  • @mehdighafari7672
    @mehdighafari7672 ปีที่แล้ว

    Perfect 👏👍🙏🏽

  • @jakeeaves218
    @jakeeaves218 5 ปีที่แล้ว

    why does i = 1

    • @imtiazshahed944
      @imtiazshahed944 5 ปีที่แล้ว

      here i is the number of rows in the pyramid.

  • @davidconiglio7577
    @davidconiglio7577 8 หลายเดือนก่อน

    I did for loop with 4, and later, I will do with 5 .The program takes all combinations for mega million and powerball 😅 .I did run this program in liberty basic and truebasic it will take more than 4 hours, and in phyton, it will take less. THIS WHY I AM LEARNING PHYTON .It was free to download others I paid.

  • @adamumustapha7868
    @adamumustapha7868 5 ปีที่แล้ว

    Hello please i need help, So I have this gridded data of dimention (144,72,636). this is montthy precipitation data and I want to calculate sliding window percentiles for 10 years window for 5th 50th and 95 percentiles. who can please help me.

  • @jessp5966
    @jessp5966 ปีที่แล้ว

    genius

  • @tangaucheplease9621
    @tangaucheplease9621 2 ปีที่แล้ว

    Noice

  • @user-ep6pd2ss8g
    @user-ep6pd2ss8g 3 ปีที่แล้ว +3

    pyramid depends on ' i '. illuminati confirmed

    • @mojo6112
      @mojo6112 3 ปีที่แล้ว

      ركز الله يهديك😂

  • @GoogleAccount-ho6ng
    @GoogleAccount-ho6ng ปีที่แล้ว

    i just dont get it

  • @justinharris6898
    @justinharris6898 4 ปีที่แล้ว +1

    *1 second into the video of speaking*
    me: OH THANK GOD SOME CLEAR ENGLISH SPEAKING PERSON
    seriously. all love no offense to the other videos I seen but it was literally like listening to gibberish

  • @omervnl
    @omervnl 2 ปีที่แล้ว

    i=8
    for j in range(-1,17,2):
    print(i*" "+j*"0")
    i=i-1
    way more easier tbh

    • @gomitos8866
      @gomitos8866 ปีที่แล้ว

      when u put "for j in range(-1,17,2)" its going to add 2 to -1 until it reaches 17, right???

  • @gomitos8866
    @gomitos8866 ปีที่แล้ว

    even if i watch this 200 times, i wont get it. The only thing i could do was for i in range(7):
    for j in range(7):
    its so confusing and idk what to do. i watched this tutorial " Python Tutorial - Python Full Course for Beginners " and repeated the nested loops part like 10 times , i still cant understand a SHIT

    • @vaby7789
      @vaby7789 ปีที่แล้ว

      same here this shit is annoying as hell

    • @turtlecodes8319
      @turtlecodes8319 ปีที่แล้ว

      @@vaby7789 fr

  • @user-vl6ld2is1j
    @user-vl6ld2is1j 3 ปีที่แล้ว

    this my way
    n = input('inter number')
    n = int(n)
    for i in range (n):
    i = i + 1
    print((n-i) * " " ,(i * 2 - 1) * " *")

  • @antoniobanderas5316
    @antoniobanderas5316 ปีที่แล้ว

    This is so confusing to wrap your head around... I mean this when not following the video steps.

  • @kikejoo2240
    @kikejoo2240 ปีที่แล้ว

    hard to understand

  • @DeepakSingh-bt7nn
    @DeepakSingh-bt7nn 5 ปีที่แล้ว

    Sir can you make your code more visible it pains my eyes watching for long time simply by adjustment of themes like that

  • @DeViLTh0rn
    @DeViLTh0rn 4 ปีที่แล้ว +1

    4:27

  • @jerrynguyen9895
    @jerrynguyen9895 ปีที่แล้ว

    illoopminati

  • @ShermanSitter
    @ShermanSitter 4 ปีที่แล้ว

    I've never used a wa-loop :)

  • @user-cg4xz3bn4l
    @user-cg4xz3bn4l ปีที่แล้ว

    how to do print something then loop it forever