Building Your Own FOR Loops

Bryson Payne
A free video tutorial from Bryson Payne
Best-Selling Author & Professor w/students in 163 countries
4.6 instructor rating • 4 courses • 56,651 students

Lecture description

Learn to code faster and cleaner using loops to repeat a set of statements as many times as you wish!

Learn more from the full course

Teach Your Kids to Code: Learn Python Programming at Any Age

Short, colorful apps and games you & your child can program right away, for ages 5 to 95! Chromebook, PC, Mac and Linux!

06:19:26 of on-demand video • Updated February 2021

  • Code your own apps and games in Python 3.9 for Chromebook, PC, Mac, or Linux, from pre-K to college level
  • Draw colorful spirals and shapes with Turtle graphics in Python
  • Create fun, playable games like War, Yahtzee, and Rock-Paper-Scissors
  • Build interactive apps that respond to user input and mouse clicks
  • Master variables, loops and functions to prepare for more advanced programming courses (and a brighter future)!
English Let's start our section on loops by learning how to build our own for loops we've used for loops in our previous drawing programs to generate beautiful shapes in Python's turtled graphics. Well now we're going to see how to build our own loops from scratch every time a for loop is the first kind of loop We'll talk about this iterates over or repeats a list of items like the numbers 1 through 100 or zero through three. We've got an example of that right here. We can import our turtle set up a pen and then for all of the numbers in the range up to 4 so that 0 through 3 0 1 2 3 Remember our computer begins numbering at zero. We should draw a circle then turn left 90 degrees. Well I do understand for loops in Python and make the best use of them. We really need to understand how to create our own ranges the range function helps us generate a series of numbers easily. This is perfect for a for loop. We can write a list out of it. We can make a list out of range 10 range three range five and we'll get 10 numbers zero through 9 3 numbers 0 1 and 2 and 5 numbers 0 1 2 3 4. Let's switch over to our idol Ed. python shell and let's run a few of these examples. So first type of list because we want to turn these into a list range will generate all the numbers for us. We just use the list command to group them together so that we can show them on the screen list out of a range of 10 numbers. By default our computer starts counting at zero so 10 numbers includes the numbers 0 1 2 3 4 5 6 7 8 9. So we have 10 numbers from zero to just less than the number we put in our range statement. One less than 10 9. Let's try that with a smaller number list range 3. We get the 3 numbers 0 1 and 2. Finally let's do a list range five. Wow that gives us five numbers from zero through four. Now that we understand a little bit about how range works we can build our own four loops from scratch and we'll see that in the next lesson