...

Web Technologies - PYTHON

Back to Course

Lesson Description


Lession - #275 Python While Loops



While Loops in Python

Loops in Python

Python While Loop is a programming construct that is used to repeatedly execute a set of statements until a condition is met. When the condition in the programme becomes false, the line immediately after the loop is performed. While loop is classified as an indefinite iteration. The amount of times the loop is executed isn't set explicitly in advance, which is known as indefinite iteration.

Syntax: 

while expression:
    statement(s>


After a programming construct, any statements indented by the same number of character spaces are regarded to be part of a single block of code. Python's way of grouping statements is indentation. When a while loop is run, expr is evaluated in a Boolean context first, and then the loop body is run if it is true. The expr is then checked again, and if it is still true, the body is run once more, and so on until the expression is false.

There are two basic loop instructions in Python:
  • while loops
  •  
  • for loops

The while Loop

The while loop allows us to run a series of statements as long as a condition is true.

i = 1
while i < 7:
  print(i>
i += 1


Output:
1
2
3
4
5
6


Note :Remember to increment I else the loop will go on indefinitely.

The while loop necessitates the availability of relevant variables; in this case, we need to define an indexing variable, I which we assign to 1.

The break Statement

Even though the while condition is true, we can break the loop with the break statement:

i = 1
while i < 6:
  print(i>
if (i == 3>
: break i += 1


Output:
1
2
3

What really does range (>
do in Python?

The range(>
is an in-build work in Python. It returns an arrangement of numbers beginning from zero and increment by 1 by default and stops before the given number.

python tutorial and python tutor

Python Tutorials

python yield

The Yield keyword in Python is like a return articulation utilized for returning qualities or articles in Python. Be that as it may, there is a slight distinction. The yield articulation returns a generator object to the person who calls the capacity which contains yield, rather than just returning a worth.