

Loops (Part 2)
Presentation
•
Science, Computers
•
University
•
Medium
Vaishnavi Sundararajan
Used 1+ times
FREE Resource
7 Slides • 4 Questions
1
2
Saw for loops last time
Use for when you know how long the loop should last
for x in range(a, b)
for x in {a1, a2, a3, a4}
3
Open Ended
Write a function printn, which takes as input a positive integer n, and prints all the numbers from 0 through n (both inclusive)
4
Indeterminately long loops?
What if we don't know how many iterations a loop might have?
Example: Write a program nprime that takes as input a positive integer n, and prints the first n prime numbers.
Needs us to try indeterminately large sets of numbers, as n grows!
But the terminating condition ("Is the number of prime numbers printed so far less than or equal to n?") is easy to check!
5
A while loop
def nprime(n) :
.........ct = 0
.........indx = 1
.........while ct < n :
....................if isprime(indx) :
.................................print(indx)
.................................ct = ct + 1
....................indx = indx + 1
6
Another example
A while lets us loop on inputs too!
Can ask the user for input till the input matches some expected pattern ("Your password must contain an uppercase letter, a lowercase letter, a number, a special character, the blood of your firstborn...")
7
Multiple Choice
Fill in the blanks so that the following program asks for input till it receives "Ready".
str = ____________
while _________ :
................................................................print("Please enter the exact string 'Ready')
str = input()
''
str != 'Ready'
'Ready'
str != 'Ready'
42
str != 'ready'
''
'Ready'
8
Nested loops
We saw two types of loops -- for and while
We can have nested loops too
Often very useful for pretty printing
Write a program which prints the numbers 1 through 20 in four rows, with five elements on each row, separated by tabs
9
Nested for loops
def print1to20() :
.........for i in range(1, 5) :
.................for j in range(1, 5) :
............................print(5*(i-1) + j, end='\t')
.................print(5*i, end='\n')
10
Open Ended
Write a function strspace which takes as input a string and returns the number of spaces in the string
11
Open Ended
Write a function xtable which, given a number n, prints the times table (multiples from 1 through 10) for every number from 1 through n
Show answer
Auto Play
Slide 1 / 11
SLIDE
Similar Resources on Wayground
8 questions
Dasar pemrograman
Presentation
•
University
6 questions
Technology
Presentation
•
University
7 questions
Computers
Presentation
•
University - Professi...
9 questions
Brain Scans
Presentation
•
12th Grade
11 questions
Electromagnet
Presentation
•
KG - Professional Dev...
8 questions
Referents
Presentation
•
University
8 questions
Tiểu học Đặng Xá
Presentation
•
KG
Popular Resources on Wayground
10 questions
Safety Day 2026 – Student Review
Quiz
•
9th Grade
21 questions
Continents and Oceans
Quiz
•
6th Grade
20 questions
Parts of Speech
Quiz
•
5th Grade
16 questions
Subject & Predicate
Quiz
•
5th Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
12 questions
Map Skills
Quiz
•
3rd Grade
22 questions
Continents and Oceans
Quiz
•
5th Grade
15 questions
Rounding Decimals
Quiz
•
5th Grade
Discover more resources for Science
20 questions
Disney Trivia
Quiz
•
University
10 questions
Spanish Greetings and Goodbyes!
Presentation
•
6th Grade - University
20 questions
Physical or Chemical Change/Phases
Quiz
•
8th Grade - University
39 questions
Unit 7 Key Terms
Quiz
•
11th Grade - University
25 questions
SER and Subject Pronouns
Quiz
•
8th Grade - University
38 questions
Unit 6 Key Terms
Quiz
•
11th Grade - University
19 questions
Key Concepts in Buddhism
Quiz
•
University
0 questions
Cell Domain Basic Review (Biology)
Quiz
•
University