WHILE Loops in Python: Definition & Syntax

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Python WHILE Loops (Iterators in Python) – Definition and Syntax

Loops in Python are an efficient method for optimising your code, and executing multiple statements. If the same block of code has to be executed, multiple times, over and over: you can put it into a loop, to perform multiple iterations, and get the same desired output. Consequently: it saves a lot of effort, and reduces complexity of the code, as well.

Basically, there are two types of iteration in programming languages:

– Indefinite iteration: In which the code block executes, until some condition is met. This iteration is performed using WHILE Loops in Python, and will be discussed in the next section.
– Definite iteration: In which the number of repetitions is specified, explicitly – and in advance. This kind of iteration is performed using for loops, and has been discussed in the previous video



The WHILE statement in Python is a bit different from what you usually use, in other programming languages. Rather than iterating over a numeric progression, or over the items of an iterable, like: strings, dictionaries, lists, tuples and sets – or any other iterable objects.

WHILE loops are traditionally used when you have a block of code which you want to repeat indefinitely, and as long as an expression is True – or a particular condition is met. And you generally use this loop when you do not know beforehand how many iterations are going to take place.

Do not forget to contrast the WHILE statement with the FOR loop in Python – which is used when you want to repeat a block of code, for a fixed number of times. Okay, But what does it look like, and how to control the execution flow of your program, using while loops? Let’s move forward, and have a look at the syntax of the while statement.


○ WHILE Loop Syntax

     while condition:
         statement_1
         statement_2
         ...

Here, is a simple declaration of a WHILE Loop in Python. First, the execution starts and checks whether the condition is True. When the condition is True, it enters the while loop – and executes the indented statements, inside the while loop. After one iteration, the condition is checked again. And this process continues until the condition evaluates to False. As soon as the condition is False, its execution skips the while loop, and moves to the next following statements in the program

Please also note that Python interprets any non-zero value or non-empty container as True. Consequently, None, zero and empty sequence will be interpreted as False. And if the condition is False at the start, the while loop will never be executed.


Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap