For loops (Python)

Cards (13)

  • If you iterate something, then you repeat it
  • There are 2 key loops to use in Python - for loops and while loops
  • A for loops is count controlled
  • An example of a for loop could be "For 10 seconds I will jump up and down" - This loop is count controlled and the loop will continue until the count (10 seconds) has finished
  • A while loop is condition controlled
  • An example of a while loop could be "While I am not out of breath, I will jump up and down" - This loop is condition controlled and will continue until the condition is no longer true
  • The "i" is a count variable - It is used to measure each iteration (turn) of the loop
  • In this program, the indented code will be looped ten times , as seen in the range brackets
  • This for loop repeats for 5 times and prints the name Olu
  • We can refer to the iteration we are currently on inside the loop itself using i
  • This loop will not print numbers from 1 to 10 as Python will always start counting at 0 , so numbers 0-9 will be printed instead - We need to specify in the range brackets that we need to start at 1 and continue up until (not including) 11
  • In this code, we have specified in our range brackets that we want to start at 1 , continue up until (not including) 11 , which means numbers 1-10 will be printed
  • Instead of numbers, you could also use a variable in a range bracket