0% found this document useful (0 votes)
5 views

Class005_ Loops

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Class005_ Loops

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Session Code CODR-912-BC-005

Module Basic

Teaching Unit Loops

Learning Outcome Loops: while, for

Resources Teacher:
1. Laptop along with audio and video
exchange
2. Notebook and Pen(To note any
development from session)
Student Resources
1. Laptop along with audio and video
exchange
2. Notebook and Pen(To keep note of
important parts in the session)

Duration 50 Mins

Structure Warm-up 2 Mins


Pace-up Activity 5 Mins
Knowledge Transfer 10 Mins
Student Led Activity 20 Mins
Short Quiz 8 Mins
Heads up tip for next class 5 Mins

Things to keep in mind while using the script


1. The examples given in the scripts are suggestive, you can use them exactly as
they are or build upon them.
2. Cover each concepts with different examples (even if it is not mentioned in
script)
3. If the student catches up quickly, build upon the content covered, for example
after the program to display factors, you can teach find sum of factors and
check if a number is “Perfect” or not, or find number of factors and check if a
number is Prime or not.
4. If the student is able to catch a+=b easily, explain a-=b, a*=b, a/=b with
examples.

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

1
Step Say Perform

Warm up Hi s​ tudent name​, ​how are you? Try to make the


(2 Mins) Are you excited for the class? student speak.
Do you remember the last class? Engage with the
student in
conversation.

Interaction In the last class, we learnt about some data Revise the
(5 Mins) structures in Python: concepts learned
Lists, Tuples, Dictionary, Sets, String in the previous
We also used a few functions on these data class.
structures.

Today we will learn about Loops in Python.


Loops are iterative statements in Python.
Iteration means repetition.
If we want to execute a command again and
again, we use Loops.

Teacher shares the screen and open Repl.it

Knowledge In python we have two kinds of loops, w


​ hile ​loop
Transfer and ​for​ loop.
(10 min)

Let’s first study w


​ hile ​loop. Teacher Activity 1:
While loop syntax
In “While loop”, the set of statements gets Show the image
executed as long as the given condition is true. and explain the
syntax

To understand the while loop let’s take an


example, let’s write a program to display
numbers from 1 to 10

First we define variable ​n​ with the initial value 1


​ ,
Now, we will display n and increase its value by
1, this has to be done until the value of n
becomes 10.
So the c​ onditional statement ​of the loop is
n<=10

The statement which changes the value of n is


called an ​update statement.
Another way of writing this statement is​ n+=1
It increases the value of n by one, and is called
increment operator.

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

2
Similarly, n
​ -=1​ is called decrement operator and
it will decrease the value of n by 1.
Both these increment and decrement operators
make up the update statements.

After displaying 1 on screen and changing the Ask students


value of n from 1 to 2, the condition is again what will happen
checked and the lines are again executed. if there is no
This process happens unless the condition update statement
becomes false, which in this case is n becoming in loop.
11.

So the loop had three basic parts,


● Initial value of variable, n=1
● Conditional statement, n<=10
● Update statement, n+=1
Now, it's your turn to try the while loop.

● Stop sharing your screen


● Ask the student to share screen and click on Student Activity 1:

In this activity, we have to take a number as Ask a student the


input from the user and display all its factors meaning of
factor.
Factors of a given number are all the numbers
which divide the given number completely.

For example, factors of the number 6 are 1,2,3 Ask for factors of
and 6 itself. another number
from the student.

To find all the factors of a given number we start Student Activity 1:


from 1 to the number itself. While Loop
All the numbers which divide the given number
are its factors.

So, our loop will start at f​ actor=1​ and will


increment by 1, i.e. ​factor+=1
The condition will be ​factor<number.

To check if ​factor divides the number Explain the


completely,​ we need a i​ f statement. statement.
Revising Modulus
if(number%factor==0): operator.

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

3
Remind them the
input function
and the need of
int function

Great!! So, we displayed all the factors of a Give hints on how


number. we can find the
We can modify the same program, to find the sum and count of
sum or count of the factors. factors.

● Ask the student to stop sharing screen


● Share your screen and open repl.it

Now, it's time to learn about ​for l​ oop. Teacher Activity 2:


For loop syntax
For loop can be used to iterate over a sequence.
And this sequence can be a list, set, dictionary,
tuple or string.

The syntax of for loop is simple.

Let’s consider a list of items Show examples


ds=[“list”,”tuples”,”set”,”dictionary”,”String”] of list, tuple, set,
dictionary and
To display all the elements of list ​ds​ we can String to explain
simply use for loop as: the for loop.

for i in ds:
print(i)

Another way of using for loop is using the Explain each


function ​range() point with
different
It is used to form a sequence of numbers. examples.

We can input three things in this function,


● Starting value
● Ending value
● Stepping value

For eg. ​range(10,15,1) ​ will have numbers

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

4
10,11,12,13,14
So the number less than the ending value is
returned, not the ending value itself.

Also the default value of stepping value is one,


so the function can work even without putting
the stepping value.

Even the starting value is optional and its


default value is 0

Now let’s use range in for loop.

To display all the even numbers between 100 Explain any


and 120 example of your
for i in range(100,121,2): choice
print(i)

Now it's your turn to use for loop.

● Stop sharing screen


● Ask the student to share screen and click on Student activity 2

In the first activity we need to display Student Activity 2:


multiplication table of 25 For Loop

So for this we need to run the for loop ten times


and that can be simply done by using the
function range

for i in range(1,11): Remember the


print(25,”x”,i,”=”,(25*i)) ending value is
not included

Now, for the second activity we need to take Help the student
number as input from the user to solve both
activities.

● Ask the student to stop sharing the screen

Revision So today we learned about for loop, while loop Revise all the
and range function. concept learned
in class briefly

Q. Can you tell me what are the three parts Ask basic
necessary in a while loop? questions and
A. Initial value, Conditional statement and prompt the
Update statement student to give

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

5
Q. What will happen if there is no update answers
statement in loop and the condition is true?
A. The loop will run infinitely.

Q. What is the use of range function?


A. It returns a sequence of numbers.

You did great today, all these concepts we are


learning are the building blocks of coding,
learning these will help us build programs with
more functions.

Heads up for the In the next class we will be using all the concepts
next class we have learned so far and also learn about
Functions.

BID GOOD BYE & END CLASS

Resources:

Activity Name Links

Teacher Activity 1 Repl link https://repl.it/languages

Teacher Activity 2 While loop syntax https://drive.google.com/file/d/1Y


r7j2FuTz6veHKQERjn9_fs5yi6fSsig/
view?usp=sharing

Teacher Activity 3 For Loop syntax https://repl.it/@ShailjaGupta/forL


oop#main.py

Student Activity 1 While Loop https://repl.it/@ShailjaGupta/whil


eLoop

Student Activity 2 For Loop https://repl.it/@ShailjaGupta/forL


oop#main.py

Student Activity 3

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

You might also like