
LOOPS
Presentation
•
Computers
•
10th Grade
•
Medium
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?
To iterate over a sequence of items
To define a function
To execute a set of statements as long as a condition is true
To create a list
12
Multiple Choice
What will happen if you forget to increment the variable in a while loop?
The loop will continue forever
The loop will execute twice
The loop will execute only once
The program will crash
13
Multiple Choice
What does the break statement do in a loop?
It pauses the loop
It skips the current iteration
It stops the loop immediately
It restarts the loop
14
Multiple Choice
What will the following code print? fruits = ['apple', 'banana', 'cherry']; for x in fruits: print(x)
apple banana cherry
banana cherry apple
cherry banana apple
apple, banana, cherry
15
Multiple Choice
What is the purpose of the continue statement in a loop?
To skip the current iteration and continue with the next
To stop the loop completely
To exit the loop if a condition is met
To restart the loop from the beginning
16
Multiple Choice
Which of the following is NOT a type of loop in Python?
while loop
for loop
nested loop
do-while loop
17
Multiple Choice
How do you define a for loop in Python?
for each item in sequence:
for item = sequence:
for (item in sequence):
for item in sequence:
18
Multiple Choice
What will the following code print? fruits = ['apple', 'banana', 'cherry']; for x in fruits: print(x)
apple banana cherry
banana cherry apple
cherry banana apple
apple, banana, cherry
19
Multiple Choice
What will the following code output? for x in 'banana': print(x)
b a n a n a
b a n
banana
b, a, n, a, n, a
20
Multiple Choice
What does the following code do? for x in fruits: if x == 'banana': break
It prints all fruits
It skips 'banana' and prints the rest
It stops the loop when 'banana' is found
It prints 'banana' only
21
Multiple Choice
What will the following code output? for x in 'banana': print(x)
b a n a n a
b a n
banana
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)
It will print 'banana'
It will skip 'banana' and print the rest
It will print nothing
It will cause an error
LOOPS IN PYTHON
By Tempa Rinchen
Show answer
Auto Play
Slide 1 / 22
SLIDE
Similar Resources on Wayground
16 questions
Python List Lesson
Presentation
•
9th Grade
17 questions
NPA Digital Media (Video) - File Formats 2
Presentation
•
10th Grade
18 questions
Pengukuran
Presentation
•
10th Grade
20 questions
SPtLDV
Presentation
•
10th Grade
19 questions
Factoring Polynomials
Presentation
•
9th - 10th Grade
15 questions
Author's Purpose
Presentation
•
10th - 11th Grade
14 questions
Belum Berjudul
Presentation
•
10th Grade
14 questions
R_WK7: OZch2m1
Presentation
•
10th Grade
Popular Resources on Wayground
24 questions
PBIS-HGMS Day 10
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Review 3
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
15 questions
HCS SCI 05 Summer School Assessment 3 Review
Quiz
•
5th Grade
35 questions
Lufkin Road Middle School Student Handbook & Policies Assessment
Quiz
•
7th Grade
18 questions
Geo 11.3 Area of Circles and Sectors
Quiz
•
9th - 11th Grade