This is the next of Python Tutorials for Beginners. You can see the other Python tutorials in this blog here. This Python beginner tutorial explains the while statement in Python with multiple examples. The while statement repeats execution of a code block for as long as a condition is True. Please view the Python video tutorial 7 or read on...
The while statements in Python programming have two formats, the while statement and the while else statement. Now, let us see these formats with Python examples. Note : the formats are followed by Python samples
In the first format of Python while statement, the condition is evaluated. If the condition is True, the code block is run. Again, the condition is evaluated. If the condition is still True, the code block is run again. This goes on until the condition becomes False (by some code in the code block). The while statement ends when the condition becomes False.
while condition:
code block
# Python code example
# print integers from 1 to 5
i = 1
while i <= 5:
print (i)
i = i + 1 # add 1 to the value of i and assign it back to i (in other words, increase i's value by 1)
If you make a mistake in the while condition e.g. i >= 1 in the while loop above, the while condition will be always True. In such a situation, the while statement runs endlessly. Such a while loop is called an infinite loop. You have to manually stop the execution of an infinite loop.
In Python code, you can put the continue statement. The continue statement skips the remaining statements in the current iteration of the while loop.
# Python example
# print integers from 1 to 5, except 3
i =0
while i < 5:
i = i + 1
if i == 3:
continue # bypass the printing of i's value of 3
print (i)
You can use the break statement in Python programming. The break statement ends the while loop immediately.
# Python example
# print integers from 1 to 2
i =0
while i < 5:
i = i + 1
if i == 3:
break # terminate the while loop
print (i)
In the second format of Python while statement, the condition is evaluated. If the condition is True, the code block1 (the while block) is run. Again, the condition is evaluated. If the condition is still True, the code block1 is run again. The while loop goes on until the condition becomes False (by some code in the code block1). After the condition becomes False, the else block is run once and the entire while statement ends.
while condition:
code block1
else:
code block2
# Python code example
# print a multiplication table
n = int(input("Enter an integer : ")) # int function converts the input string into an integer
i = 1
while i <= 10:
print (n, "*", i, "=", n*i)
i += 1 # increment i's value by 1
else:
print ("The table is now complete.")
The while statements in Python programming have two formats, the while statement and the while else statement. Now, let us see these formats with Python examples. Note : the formats are followed by Python samples
In the first format of Python while statement, the condition is evaluated. If the condition is True, the code block is run. Again, the condition is evaluated. If the condition is still True, the code block is run again. This goes on until the condition becomes False (by some code in the code block). The while statement ends when the condition becomes False.
while condition:
code block
# Python code example
# print integers from 1 to 5
i = 1
while i <= 5:
print (i)
i = i + 1 # add 1 to the value of i and assign it back to i (in other words, increase i's value by 1)
If you make a mistake in the while condition e.g. i >= 1 in the while loop above, the while condition will be always True. In such a situation, the while statement runs endlessly. Such a while loop is called an infinite loop. You have to manually stop the execution of an infinite loop.
In Python code, you can put the continue statement. The continue statement skips the remaining statements in the current iteration of the while loop.
# Python example
# print integers from 1 to 5, except 3
i =0
while i < 5:
i = i + 1
if i == 3:
continue # bypass the printing of i's value of 3
print (i)
You can use the break statement in Python programming. The break statement ends the while loop immediately.
# Python example
# print integers from 1 to 2
i =0
while i < 5:
i = i + 1
if i == 3:
break # terminate the while loop
print (i)
In the second format of Python while statement, the condition is evaluated. If the condition is True, the code block1 (the while block) is run. Again, the condition is evaluated. If the condition is still True, the code block1 is run again. The while loop goes on until the condition becomes False (by some code in the code block1). After the condition becomes False, the else block is run once and the entire while statement ends.
while condition:
code block1
else:
code block2
# Python code example
# print a multiplication table
n = int(input("Enter an integer : ")) # int function converts the input string into an integer
i = 1
while i <= 10:
print (n, "*", i, "=", n*i)
i += 1 # increment i's value by 1
else:
print ("The table is now complete.")
Want to learn how these Python samples work? View my Python video tutorial 7. Thank you.
That's a great information with a fresh content. Thanks for sharing such an informative Blog.
ReplyDeleteMobile App Development Company in Chandigarh