While Loops in Python!

While Loops in Python!

Β·

4 min read

Hi Legends!!πŸ‘‹

In this article we're gonna learn about While loops in Python!

Let's start: πŸ˜‰

1. What are while loops? πŸ‘‘

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. We generally use this loop when we don't know the number of times to iterate beforehand.

2. Code on while loops!! ✨

Print z as long as it is less than 8

z = 1
while z < 8:
    print(z)
    z = z + 1

Output:

image.png

Explanation:

  • First of all I have made the variable z and set this at 1.

  • Then we had used this while function!! And Code: while z < 8 python will execute while 1 < 8And also we are also putting colon(:) after the while statement because with the colon (:) it helps to indent and indentation is must in python If you would not put the (:) then it does not work!!

  • Then print statement is there!!

  • Then I am incrementing z Like it will execute the code as long as it is less than 8 and it stops!!

Note: Remember to increment z, or else the loop will continue forever.

The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, z which we set to 1.

2.1 Shortcut of (z = z + 1) 🎈

So, If I do like that: πŸ‘‡

z = 1
while z < 8:
    print(z)
    z += 1

If it is z += 1 then also it would work and it is the easy way to do that!!πŸ˜‰

Hopefully you are done with it, let's move to the break statement now!!

3. The break statement 🀚

With the break statement we can stop the loop even if the while condition is true:

# The Break statement

x = 10
while x > 5:
    print(x)

    if (x == 8):
        break
    x -= 1

Output:

image.png

Explanation:

  • So here simply we had set the value of x as 10.

  • Then we had used the while statement!! Code: while x > 5: Python will execute while 10 > 5:.

  • After that there is print() statement which is printing x.

  • Now there is an if statement and Code is: if (x == 8) means if it comes to 8 then there is an break statement which helps to stop the code!!

  • Then there is iteration and Code is: x = x - 1 which helps to iterate!

I want you to write, understand and run this codeπŸ’œ

4. The Continue Statement 🏏

With the continue statement we can stop the current iteration, and continue with the next:

Example for continue statement...

Continue to the next iteration if i is 2.

# The continue statement

i = 0
while i < 5:
    i += 1
    if (i == 2):
        continue
    print(i)

Output:

image.png

Explanation:

  • So, here first of all I have made a variable i and set it as 0.

  • Then we have used the while function and written while i < 5: Python will execute while 0 < 5:.

  • Then we have written i += 1 means i = i + 1.

  • If statement comes: if (i == 2): Python will execute if (0 == 2): then it will continue means what that it will stope the current iteration which is 2. After that we have done print(i). Look at the Output above !!

So legends, 😎 I am sure that you all are totally clear by the concepts!! Let's dive in to the next one:

5. The else statement πŸͺ€

With the else statement we can run a block of code once when the condition no longer is true:

Example of else statement...

Print a message once the condition is false:

# The else statement

a = 1
while a < 11:
    print(a)
    a += 1
else:
    print("a is no longer less than 10")

Output:

image.png

Explanation: It is so simple for you!!😎

  • So, it is like if the condition is once false so can use the else statement and print a message to make the user understand the code!

Super guysπŸ₯³πŸŽ‰, we have cracked the while loops!!

Mini question 1 πŸŽƒ

image.png

I want you to solve this!!πŸ’œ

Mini question 2 πŸŽƒ

image.png

Can you solve it??

Mini question 3 πŸŽƒ

image.png

If you solve this one you are pro😎

Mini question 4 πŸŽƒ

image.png

Let me see if you can solve this! πŸ˜‰

So, that is it for today guys!!😊 If you wanna check your answers run in the compiler!!

And if you have any doubt any query please mention it in the comments belowπŸ’™

And for now GoodbyeπŸ‘‹

Β