While loop in Python

Python While:

In while loop, if the expression becomes true, only then the block of statements present inside the while loop will be executed.

If the condition is false then whatever statements after that while loop will not execute.

But in for loop the  loop will execute till the last elements of that sequence.Read our article for loop here.

Syntax:
while condition/expression:

     # execute these statements
     ......

Below the loop there is a indent block, the loop will run as per the condition.

Example:
num = 1
while num < 10:
    print (num)
    num = num +1

Output:

1
2
3
4
5
6
7
8
9

Explanation:

  • The loop will execute as long as the num < 10
  • Here we are incrementing the loop by num = num +1
  • where num will increment  and run till it reaches 10.

While Loop with else statement:

The else block will execute only if the condition is false or not satisfied as mention above.

Syntax:

while condition: 
     # execute these statements 
else: 
     # execute these statements
Example:
num = 1
while num < 5:
    print (num)
    num = num +1

else:
    print ('End of while loop')

Output:

1
2
3
4
End of while loop

Explanation:

  • In above loop the loop run for 4 times till it reaches 5.
  • Then it jumps to else block, execute that statement and exit from the program.

Hope you understood while loop .

If you liked the post share with your friends and colleagues .Give your valuable comments in the comments section below.

You may like Some more useful links in python:

Wants to become a master in python join our crash course today here.

7 thoughts on “While loop in Python”

  1. COVID 19 Related Neuropsychiatric Symptoms in Patients With Alcohol Abuse Conditions During the SARS CoV 2 Pandemic A Retrospective Cohort Study Using Real World Data From Electronic Health Records of a Tertiary Hospital tamoxifen dose

  2. When I read an article on this topic, totosite the first thought was profound and difficult, and I wondered if others could understand.. My site has a discussion board for articles and photos similar to this topic. Could you please visit me when you have time to discuss this topic?

  3. When I initially commented I clicked the -Notify me when new comments are added- checkbox and now every time a remark is added I get four emails with the identical comment. Is there any method you may remove me from that service? Thanks!

  4. I抦 impressed, I need to say. Really not often do I encounter a weblog that抯 each educative and entertaining, and let me tell you, you might have hit the nail on the head. Your idea is outstanding; the problem is something that not sufficient individuals are talking intelligently about. I’m very completely satisfied that I stumbled throughout this in my seek for one thing referring to this.

  5. This website online is known as a walk-by means of for all of the info you wanted about this and didn抰 know who to ask. Glimpse right here, and you抣l undoubtedly uncover it.

  6. After examine a few of the weblog posts on your web site now, and I actually like your method of blogging. I bookmarked it to my bookmark web site list and might be checking again soon. Pls take a look at my website online as well and let me know what you think.

  7. Once I originally commented I clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get 4 emails with the same comment. Is there any method you may take away me from that service? Thanks!

  8. Read reviews and was a little hesitant since I had already inputted my order. or possibly a but thank god, I had no issues. particularly the received item in a timely matter, they are in new condition. you ultimately choose so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans https://www.realjordansshoes.com/

  9. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  10. FAA response Currently required EMK medications as well as the following medications proposed for the EMK s have an expiration date of approximately 1 year atropine, bronchodilator inhaler, dextrose, epinephrine, saline solution, and lidocaine; aspirin, non narcotic analgesic, antihistamine, and nitroglycerin tablets cialis 5 mg best price usa Transfusion and Coagulation Therapy

  11. I do not even know how I ended up here but I thought this post was great I do not know who you are but certainly youre going to a famous blogger if you are not already Cheers

Leave a Reply

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