Search Header Logo
LOOPS

LOOPS

Assessment

Presentation

Computers

10th Grade

Medium

Created by

Tempa Rinchen

Used 11+ times

FREE Resource

10 Slides • 12 Questions

1

LOOPS IN PYTHON

By Tempa Rinchen

2

Types of Loops in Python

Python has two types of loops:

  • while loop

  • for loop

    Both loops help in repeating a block of code

3

while Loop – Basics

i = 1

while i < 6:

print(i)

i += 1

-Repeats as long as the condition is true
- Don’t forget to
increment or modify the loop variable, or it becomes an infinite loop

4

while Loop – break

i = 1

while i < 6:

print(i)

if i == 3:

break

i += 1


- break exits the loop even if the condition is still true

5

while Loop – continue

i = 0

while i < 6:

i += 1

if i == 3:

continue

print(i)

Skips the current iteration when i == 3, then continues

6

For Loop

for x in range(6):

print(x)






- A for loop is used for iterating over a sequence (list,tuple,strings)
- Generates a sequence: 0 to 5 (not including 6)

​fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

7

For Loop - break statement

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break


Exit the loop when x is "banana"

8

For Loop - continue statement

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)


Exit the loop when x is "banana"

9

Using for & while loop in Turtle

from turtle import*
fd(100)
rt(90)
fd(100)
rt(90)
fd(100)
rt(90)
fd(100)

​from turtle import*
for i in range(4):
fd(100)
rt(90)

10

Activity

Sonam wants to generate multiplication table of 1, 2 and 3 upto 10 using nested while loop. Write the code

11

Multiple Choice

What is the purpose of a while loop in Python?

1

To iterate over a sequence of items

2

To define a function

3

To execute a set of statements as long as a condition is true

4

To create a list

12

Multiple Choice

What will happen if you forget to increment the variable in a while loop?

1

The loop will continue forever

2

The loop will execute twice

3

The loop will execute only once

4

The program will crash

13

Multiple Choice

What does the break statement do in a loop?

1

It pauses the loop

2

It skips the current iteration

3

It stops the loop immediately

4

It restarts the loop

14

Multiple Choice

What will the following code print? fruits = ['apple', 'banana', 'cherry']; for x in fruits: print(x)

1

apple banana cherry

2

banana cherry apple

3

cherry banana apple

4

apple, banana, cherry

15

Multiple Choice

What is the purpose of the continue statement in a loop?

1

To skip the current iteration and continue with the next

2

To stop the loop completely

3

To exit the loop if a condition is met

4

To restart the loop from the beginning

16

Multiple Choice

Which of the following is NOT a type of loop in Python?

1

while loop

2

for loop

3

nested loop

4

do-while loop

17

Multiple Choice

How do you define a for loop in Python?

1

for each item in sequence:

2

for item = sequence:

3

for (item in sequence):

4

for item in sequence:

18

Multiple Choice

What will the following code print? fruits = ['apple', 'banana', 'cherry']; for x in fruits: print(x)

1

apple banana cherry

2

banana cherry apple

3

cherry banana apple

4

apple, banana, cherry

19

Multiple Choice

What will the following code output? for x in 'banana': print(x)

1

b a n a n a

2

b a n

3

banana

4

b, a, n, a, n, a

20

Multiple Choice

What does the following code do? for x in fruits: if x == 'banana': break

1

It prints all fruits

2

It skips 'banana' and prints the rest

3

It stops the loop when 'banana' is found

4

It prints 'banana' only

21

Multiple Choice

What will the following code output? for x in 'banana': print(x)

1

b a n a n a

2

b a n

3

banana

4

b, a, n, a, n, a

22

Multiple Choice

What will happen if you use continue when x is 'banana' in the following code? for x in fruits: if x == 'banana': continue; print(x)

1

It will print 'banana'

2

It will skip 'banana' and print the rest

3

It will print nothing

4

It will cause an error

LOOPS IN PYTHON

By Tempa Rinchen

Show answer

Auto Play

Slide 1 / 22

SLIDE