Skip to content
Ryan James
Menu

← Python10 min read

Creating Loops

Loops are seen in every programming language and just about every program out there. Loops are often referred to as iteration. At some point every program will have a section of code that it needs to repeat. In Python there are several ways of creating a loop depending on the intended purpose of the code.

Creating Loops

Loops are seen in every programming language and just about every program out there. Loops are often referred to as iteration. At some point every program will have a section of code that it needs to repeat. In Python there are several ways of creating a loop depending on the intended purpose of the code.

Using The while Loop

Lots of programming languages have a ‘while’ loop. When using the while keyword with a loop, the program will determine if a condition is true or not. The loop will keep on repeating itself until the condition is false.

i = 0
while i < 10:
   print(i*10)
   i = i + 1

In the example shown above, the code segment will create a variable, i, and set it equal to zero. Then the loop will start and run as long as i is less than 10. The loop causes i to go up by 1 each time and print the multiples of 10 from 0 to 90.

In similar fashion, you can create a loop that runs indefinitely by just putting the condition ‘True’ after the while keyword. But, this will cause the loop to run forever, so it’d be a good idea to put in a way to escape the loop.

while True:
   #run some code here
   command = input("Continue? ")
   if command == "no":
       break

In the segment shown above, the loop is set to run indefinitely by the way it is initially set up. But, with the line asking for input from the user the loop sets itself up to quit running when the user types in the word, ‘no’.

Using The for Loop

Sometimes when you’re writing a program, you know exactly how many times you want a block of code to repeat. When this is the case it’s best to use a ‘for’ loop. By using the keyword for to create your loop, you can specify an integer number for the number of iterations that your loop will carry out.

for i in range(10):
   print(i*2)

The code segment shown above will create a loop that iterates a total of 10 times. The value of the variable ‘i’ will start at 0 and run up to 9. The result will print the even numbers from 0 to 18.

Using The ‘for-each’ Loop

In programming, there is also a loop called a ‘for-each’ loop. In this kind of loop, you can write a loop that will iterate over a data type like a String, list, or array until it reaches the length of that data type. For example, you can have a loop iterate over the letters in a word and print each letter one at a time, either on the same line or on a new line.

word = "Python"
for letter in word:
   print(letter)
for letter in word:
   print(letter, end="")

In the example shown above, the 2 loops will iterate over the word, ‘Python’, one letter at a time. In the first loop, all of the letters will be printed on different lines. In the second loop all of the letters will be printed on the same line since the added parameter to override the new line command in the print statement has been added (end=“”).

Keep coding!
Ryan