
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
21 questions
KIỂM TRA VỀ MẠNG MÁY TÍNH VÀ MẠNG INTERNET
Presentation
•
10th Grade
19 questions
Transformations & Parent Functions
Presentation
•
10th Grade
15 questions
Lessons 7-8: Our Tradition
Presentation
•
10th Grade
20 questions
Perangkat lunak Pengolah Kata
Presentation
•
10th Grade
16 questions
39 JavaScript Zmienne
Presentation
•
KG
18 questions
Unit 1 Solving & Creating Equations Review
Presentation
•
9th - 10th Grade
17 questions
Multiplying radicals with variables
Presentation
•
10th - 11th Grade
21 questions
Программирование на Python
Presentation
•
10th Grade
Popular Resources on Wayground
20 questions
"What is the question asking??" Grades 3-5
Quiz
•
1st - 5th Grade
20 questions
“What is the question asking??” Grades 6-8
Quiz
•
6th - 8th Grade
10 questions
Fire Safety Quiz
Quiz
•
12th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
34 questions
STAAR Review 6th - 8th grade Reading Part 1
Quiz
•
6th - 8th Grade
20 questions
“What is the question asking??” English I-II
Quiz
•
9th - 12th Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
47 questions
8th Grade Reading STAAR Ultimate Review!
Quiz
•
8th Grade
Discover more resources for Computers
20 questions
“What is the question asking??” English I-II
Quiz
•
9th - 12th Grade
10 questions
Fire Prevention
Quiz
•
9th - 12th Grade
50 questions
STAAR English 2 Review
Quiz
•
10th Grade
20 questions
Figurative Language Review
Quiz
•
10th Grade
20 questions
Grammar
Quiz
•
9th - 12th Grade
16 questions
AP Biology: Unit 1 Review (CED)
Quiz
•
9th - 12th Grade
20 questions
verbos reflexivos en español
Quiz
•
9th - 12th Grade
11 questions
Expectations Review
Quiz
•
9th - 12th Grade