Python- While loop intro

Good Morning boys

Today we will take a step ahead into the concept of looping, with an introduction of the WHILE loop

--------------------------------------------------------------------------------------------------------
By the end of this session, you will be able to:

  • understand the need for while loop
  • Understand the syntax of the WHILE loop
  • Apply the loop to get the desired result
  • Predict the output for a group of statements.
----------------------------------------------------------------------------------------------------------

The Google meet link for the class today is:

Meeting ID

-------------------------------------------------------------------------------------------------------

Python while loop

  • The while loop is also known as a pre-tested loop.
  • In general, a while loop allows a part of the code to be executed as long as the given condition is true.
  • It can be viewed as a repeating if statement. 
  • The while loop is mostly used in the case where the number of iterations is not known in advance.



The syntax is given below.
  1. while expression:  
  2.     statements  

  • Here, the statements can be a single statement or the group of statements.
  • The expression should be any valid python expression resulting into true or false. 
  • The true is any non-zero value.

Python while loop

Example 1

  1. i=1;  
  2. while i<=10:  
  3.     print(i);  
  4.     i=i+1;  
Output:
1
2
3
4
5
6
7
8
9
10

Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW