Posts

Loops and Functions Session 8

Image
Loops in Python Sometimes we want to repeat a set of statements in our program. For instance: Print 1 to 1000 Loops make it easy for a programmer to tell the computer, which set of instructions to repeat, and how! Types of loops in Python Primarily there are two types of loops in Python While loop For loop We will look into this one by one! While loop The syntax of a while loop looks like this: ''' while condition: #Body of the loop ''' Copy The block keeps executing until the condition is true/false In while loops, the condition is checked first. If it evaluates to true, the body of the loop is executed, otherwise not! If the loop is entered, the process of condition check and execution is continued until the condition becomes false. Quick Quiz:  Write a program to print 1 to 50 using a while loop. An Example: i = 0 while i < 5 : print ( “CodesforYou” ) i = i + 1 Copy (Above program will print CodesForYou 5 times) Note:  if the condition ...