Page 1 :
Unit I: Computational Thinking and Programming - 1, , , , Chapter- 12 Looping in Python, , “There is a world of answers, outside the loop.”, , , , , , Sa. BK te), , , , , , , , , , , , , , , , , , , , , , , , , , 1 Introduction: Looping in python, 2 while loop, , 3 while loop with else block:, , 4 The range ( ) function, , 5 for loop, , 6 Range( ) Function with For Loop, 7 for loop with else block, , 8 Infinite loop, , 9 Nested Loop, , 10 Jump Control Statements:, , 11 1) Break statement, , 12 2) Continue statement, , 13 3) pass Statement:
Page 2 :
Unit I: Computational Thinking and Programming - 1, Chapter- 12 Looping in Python, , After completing this lesson on Python Loops, you will be able to:, , > Define loops and their types in python, > Explain Nested loop, > Describe the range function, , > Explain the break and continue statements in a loop., , > Often, we repeat a task, for example, we work hard to get paid at the, end of each month. This is done every month., , > This kind of repetition is also __ start |, called iteration. Repetition of a, set of statements in a program is, , made possible using looping me, , constructs., , . FALSE, > Let us understand with the help a stop |, , of small program, , , , Program: Write a program to print the first five natural numbers., , Output:, #Print first five natural numbers print(1) 1, print(2) 2, print(3) 3, print(4) 4, print(5) 5, , “Page 2 of zz
Page 3 :
Unit I: Computational Thinking and Programming - 1, , Chapter- 12 Looping in Python, , What should we do if we are asked to print the first 1000 natural, numbers? Writing 1000 print statements would not be an efficient, solution., , It would be tedious and not the best way to do the task., , Writing a program having a loop or repetition is a better solution., , The program logic is given below:, , 1. Take a variable, say count, and set its value to 1., , 2. Print the value of count., , 3. Increment the variable (count += 1)., , 4. Repeat steps 2 and 3 as long as count has a value less than or equal to, 1000 (count <= 1000)., , So, looping constructs provide the facility to execute a set of statements, in a program repetitively, based on a condition., , There are two looping constructs in Python - while and for., , “Page 3 of 22
Page 4 :
Unit I: Computational Thinking and Programming - 1, , Chapter- 12 Looping in Python, , > The while statement allows you to, repeatedly execute a _ block of, statements as long as a condition is, true, , > It is entry-controlled loop i.e. it first, , check the condition and if it is true, , , , then allows entering in loop., , % while loop contains various loop, , , , Next Statement, , elements: initialization, test, condition, body of loop and update statement, , while condition:, #body_of_while, , Example 1: Program to print first 10 natural numbers using while loop., , num = 1 iat relay a), , # num <= 10 remains true, , while num <= 10: —==_==__» Ca deh ity), , , , print (num), , #incrementing the value of num Body of loop, , num = num + 1——» Bei, , , , , , # loop will repeat itself as long as, , “Page 4 of 22
Page 5 :
Unit I: Computational Thinking and Programming - 1, Chapter- 12 Looping in Python, , 1. Initialization: it is used to give starting value in a loop variable from, where to start the loop. In above example num is initializing with 1, , 2. Test condition: it is the condition or last value up to which loop will be, executed. In above example num<=10 is test condition, , 3. Body of loop: it specifies the action/statement to repeat in the loop, 4. Update statement: it is the increase or decrease in loop variable to, reach the test condition. In above example num=num#1 is update, , condition., , Example 2: The program takes a number and generates all the divisors, of the number., , Coding:, , n=int(input("Enter an integer:")), , print("The divisors of the number are:"), , , , tet OUTPUT, while i<=n: Enter an integer: 10, if(n%i==0): The divisors of the number, print(i) ares, 1, i=i+1 2, 5, , , , , , , , “Page 5 of 22