Loops in Python


By reading this article your following problems will be answered.

How to use loops in Python.
Number loops in Python.
String loops in Python.
Range loops in Python.

Number loops in Python

  • This type of loops can be used to repeat specific tasks a number of times which is specified by the programmer.
  • Such a loop can be interpreted as following on Python.
for i in range(3):
  • The codes which are below the loop will run the number of times set by the loop.
  • The ‘i’ is a variable, so it can be any name.
  • The above mentioned specific variable bears the current number of the loop, and it starts counting from 0.
  • The ‘for i in’ part denotes that this loop should run the specified number of times.
  • The repeating number of times need to be set as ‘range(n)’.
for i in range(n):
  • The ’n’ can be any number.!!!
  • It is required to put a colon (:) at the end of the loop line. for i in range(5):
  • Also the code which is supposed to run the number of times denoted by the loop is required to be indented as following.
for i in range(2):
  print("Run")
  print("Fast")
print("Stop")
  • The output of the above code will be looks like following.

Run

Fast

Run

Fast

Stop

  • The print(“Stop”) line is not indented, so it will be not considered as a part of the loop.
  • So every line which is needed to be inside a loop required to be indented to make it run inside the loop.

String loops in Python

  • Python loops can be run using the number of letters in a string.
  • These types of loops will look as following.
for letter in “Word”:
  • In this type of loops, the ‘i’ was replaced by the word ‘letter’.
  • So the ‘letter’ word in the loop makes the loop to be run the number of times equal to a number of characters in a specific word.
  • The specific word can be either a string or a word stored in a variable.
varWord = "Python"	
for letter in varWord:
    print("Hey")

Range in loops in Python

  • We can specify a range for ‘i’ in loops.
  • By the default, the ‘i’ starts counting from zero, but we can specify where to start counting and where to stop counting.
  • In the following code, the range will start counting from 1 and it will stop on 25.
for i in range(1, 25):
  print(i)
  • In this we need to specify the starting number followed by a comma and the ending number in the range function range(1, 25).
  • Of course we can assign any number to the starting number and the ending number. 
  • But the ending number is range exclusive.
  • That means the ending number is not regarded as a number in the range.
  • For example if we put 25 as the ending number, the actual number the counting will end is 24.
  • So actually the ending number is not included in the range itself.
  • We can also specify a step size in a range.
  • The following code shows a range from 1 to 30 with a step size of 3.
for i in range(1, 30, 3):
  print(i)
  • In this scenario the step size is mentioned after the ending number with a comma in between.
  • In general the first value will be the starting number, the second is the ending number and the third is the step size.
  • We can also mention negative step sizes.
  • The following codes shows that.
for i in range(50, 0, -2):
  print(i)
  • In the above example, the range starts counting from 50 down to 0 with a step size of 2 (or -2).
  • Specially to use negative step sizes, the starting value is always required to be grater than the ending value. Unless it won’t do anything.

Additional points on Loops in Python

  • The range of the loop is range exclusive. That means if we specify 50 as the range ending value, it will stops at 49.
  • But the total number of times that the loop will run of course will be 50, because the loop counts starting from 0 if not specified to.
  • So if we do not specify a range and only specified the ending value, it will run one more time than if we specified the starting number, because the ending number is range exclusive.
  • So if we only specified the ending number, and it’s 50 (range(50)), it will count from 0 to 49 in a total number of 50 times.
  • In the other hand, if we specified the range from 1 to 50 (range(1, 50)), it will count from 1 to 49 in a total number of 49 times.
  • So if we specified the range, it will count one time less than we specified.
  • To quit a loop from being executed further, we can use a ‘break’ command.
  • The ‘break’ command stops the loop from being executed. (The following example code explains that.)
wallet = 25
socks = 0
for price in range(10):
      if wallet >= price:
          wallet = wallet - price
          socks = socks + 1
      else:
          break
print("I have",wallet,"dollars")
print("and",socks,"socks")

Click here to know more about Python