13 For Statements
13 For Statements
Online Course
Supported by
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
For Statements
13
Global Leadership School
Handong Global University
Learning Objectives
• Use for statements
• Understand range(), in
• Use range(), in
for vs. while (1)
• Output only odd numbers in a specific
section
• If there is a regular change, for loops are efficient
• Easier to read and understand
# for() # while()
# for() # while()
# Method 1
for i in range(2,12,2) :
print (i)
# Method 2
for i in range(5) :
print ((i+1)*2)
Exercise 2
• Print the multiples of 7 in between 1 and 50
and the squares of those numbers
• Use for, in, range()
Exercise 2, Code
• 2 methods to print the multiples of 7 in between 1 and 50
and the squares of those numbers
# Method 1
# Method 2
# Can use the step without %
name = “HORSTMANN”
count = 0
for fr in fruit :
print(fr)
count = count + 1
for fr in fruit :
count = 0
for f in fr :
print(f)
count = count + 1
• 6 times
• 7 times
• 43 times
• 50 times
Practice Problem 1, Answer
• How many times does the “for” statement
execute? for i in range(7, 50, 7) :
print(i)
• 6 times
• 7 times
• 43 times
• 50 times
Practice Problem 2
• Which statement is not appropriate?
• range(1,5) => [1, 2, 3, 4, 5]
• range(1,5) => [1, 2, 3, 4]
• range(1,15,4) => [1, 5, 9, 13]
• range(-1,6,2) => [-1, 1, 3, 5]
Practice Problem 2, Answer
• Which statement is not appropriate?
• range(1,5) => [1, 2, 3, 4, 5]
• range(1,5) => [1, 2, 3, 4]
• range(1,15,4) => [1, 5, 9, 13]
• range(-1,6,2) => [-1, 1, 3, 5]
Thank you
13 For Statements
Contact Info
[email protected]
https://ecampus.handong.edu/
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.