while loop and for loop

The 2 main types of loops in Python (While And For Loops)

Python For Loop

With a for loop, you can iterate over a list, variable, or any object that has a sequence. You will be able to access what is inside that item that you used to iterate over it and then print it or do whatever you want with this value.

Example:

#____ProgrammerCodes.com____
for x in "Python":
    print(x)
#____ProgrammerCodes.com____

Result:

P
y
t
h
o
n
for loop in python

This statement: for x in “Python” means that we define a variable x that will iterate over a sequence of a string “Python”. The phases of this for loop are defined by the number of letters in this string “Python” so every phase a letter will be stored in the x variable and then we will print that x in that phase.

If we want to change the string “Python”, we can use a list instead of it. The for loop will iterate over the items of the list.

Example:

#____ProgrammerCodes.com____
animals = ["Cat","Dog","Rabbit","Lion"]
for animal in animals:
    print(animal)
#____ProgrammerCodes.com____

Result:

Cat
Dog
Rabbit
Lion
for loop example in python

Python While Loop

While loop allows you to repeat any code as long as the condition that you set is true. The while loop will see if the condition is true then it will run the code after the first loop, it will see again if the condition is still true. When the condition is no longer true, the while loop will stop.

Example:

#____ProgrammerCodes.com____
time = 1
while ( time < 6):
   print("Time now is: " , time)
   time = time + 1
print("Time is over")
#____ProgrammerCodes.com____

Result:

Time now is: 1
Time now is: 2
Time now is: 3
Time now is: 4
Time now is: 5
Time is over
while loop in python

We have assigned a variable that contains the number 1 because we want to start our loop from the 1. The while loop’s condition is “time < 6” so as long as the variable time has a number less than 6 our loop will keep working and going to the next loop phase.

In the while loop code, we print the time with a phrase then we change the variable time. We add 1 number to the last number in order to keep going until we will arrive in the phase that the variable time will be 6. When the time variable will be 6, the condition will be false so the while loop will stop its work. The program will continue working in the next lines.

Break, Continue, And Pass Loop Control Statements

For loop and while loop run with phases. Depending on your program you will have different phases such as repeated codes or iterating over multiple items inside a list. You will need to control these loops. Break, continue, and pass will allow you to control your loops.

Break statement

When you write a break statement in a code inside a loop, the loop will stop working.

Example 1:

#____ProgrammerCodes.com____
time = 1
while ( time < 6):
   print("Time now is: " , time)
   time = time + 1
   break
print("Time is over")
#____ProgrammerCodes.com____

Result:

Time now is: 1
Time is over
break statement in python example 1

Example 2:

#____ProgrammerCodes.com____
time = 1
while ( time < 6):
    if (time == 3):
        break
    print("Time now is: " , time)
    time = time + 1
print("Time is over")
#____ProgrammerCodes.com____

Result:

Time now is: 1
Time now is: 2
Time is over
break statement in python example 2

In this example, the program will run and do a loop but when it will find time 3, it will run the break statement to stop the while loop. The program won’t care about the next loops.

Continue statement

If the program runs the code in a loop and sees a continue statement there then the program will go to the next loop without running any code in that phase of the loops.

Example:

#____ProgrammerCodes.com____
animals = ["Cat","Dog","Rabbit","Lion"]
for animal in animals:
   if animal == "Rabbit":
      continue
   print (animal)
#____ProgrammerCodes.com____

Result:

Cat
Dog
Lion
continue statement in python

Pass statement

Pass will allow the program to run the next lines of the code that exist in the loop. When pass exists, it means that nothing will happen in that line of code.

Example:

#____ProgrammerCodes.com____
animals = ["Cat","Dog","Rabbit","Lion"]
for animal in animals:
   if animal == "Rabbit":
      pass
      print("Pass Because Nothing Is Here")
   print (animal)
#____ProgrammerCodes.com____

Result:

Cat
Dog
Pass Because Nothing Is Here
Rabbit
Lion
pass statement in python

Difference between break, continue, and pass control statements

Break stops the loop from going to the next loops, continue prevents the program from running the next lines of the loop’s code, and pass will allow the program to run the next lines of code so it says to the program that nothing is here just pass to the next lines.

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *