0% found this document useful (0 votes)
6 views29 pages

13 For Statements

The document outlines an online course supported by UNESCO UNITWIN and Handong Global University, focusing on programming concepts such as for statements, the range() function, and the in operator. It includes exercises for practicing these concepts, along with examples of code for various scenarios. The course emphasizes the efficiency of for loops and provides practice problems to reinforce learning.

Uploaded by

Thae Thae Aung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

13 For Statements

The document outlines an online course supported by UNESCO UNITWIN and Handong Global University, focusing on programming concepts such as for statements, the range() function, and the in operator. It includes exercises for practicing these concepts, along with examples of code for various scenarios. The course emphasizes the efficiency of for loops and provides practice problems to reinforce learning.

Uploaded by

Thae Thae Aung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UNESCO UNITWIN

Online Course
Supported by

Handong Global University


Ministry of Education, Korea

* 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()

n = int(input(“Positive integer: “)) i=1


n = int(input(“Positive integer: “))
for i in range(1, n, 2) :
print(i) while i < n :
print(i)
i=i+2
for vs. while (2)
• Print a string one letter at a time
• The shorter code is using for
• For string and list, for is easier

# for() # while()

for letter in “Mango” : i=0


print(letter) fr = “Mango”

while i < len(fr) :


letter = fr[i]
print(letter)
i=i+1
Exercise 1
• Print even number between 2 and 10
• Use for, in, range()
Exercise 1, Code
• Two ways to print even numbers between 2 and 10

# 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

for i in range(1, 50, 1) :


if i%7 == 0 :
print(i)
print(i**2)

# Method 2
# Can use the step without %

for i in range(7, 50, 7) :


print(i)
print(i**2)
range() function
• Built-in function range()
• Suitable for repeating by listing numbers
• Create the basis for repetition that performs
arithmetic calculations.
in operator
• Membership operator in
• Used with range()
Exercise 2
• Look at the table below and write how many
times it will run
for() Number of executions
for i in range(1, 11):
for i in range(10):
for i in range(10, 0, -1):
for i in range(-10, 11):
for i in range(10, 0):
for i in range(-10, 11, 3):
Exercise 2, Answer
for Number of executions

for i in range(1, 11): 10 times, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


for i in range(10): 10 times, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(10, 0, -1): 10 times, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
for i in range(-10, 11): 21 times, [-10, -9, -8, -7, -6, -5, -4, -3,
-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(10, 0): 0 times, []
for i in range(-10, 11, 3): 7 times, [-10, -7, -4, -1, 2, 5, 8]
Use of for-statement 1
# String

name = “HORSTMANN”
count = 0

for char in name :


print(char)
count = count + 1

print(“number of letters = “, count)


Use of for-statement 2
# List

fruit = ["apple", "banana", "lemon", "tomato"]


count = 0

for fr in fruit :
print(fr)
count = count + 1

print(“item count = ", count)


Use of for-statement 3
# List

fruit = ["apple", "banana", "lemon", "tomato"]

for fr in fruit :
count = 0
for f in fr :
print(f)
count = count + 1

print(”word count = ", count)


Exercise 3
• Using the for loop,

#1, Print character “*” once in the first row, twice in


the second row, repeat to the 10th row

#2, Print character “*” 10 times in the first row, 9


times in the second row, repeat until tenth row with
one star.
Exercise 3, Code
#1 #2
# “*” print more and more # “*” print less and less

for i in range(1, 11) : for i in range(10, 0, -1) :


print(‘*‘ * i) print('*‘ * i)
Exercise 4
• Using the for-loop,
• Use the letters "", "*" to output a symmetrical
triangle.
Exercise 4, Code
i=0

for i in range(1, 11):


print(' '*(10-i),'*'*(i*2))
Exercise 5
• Store your name (including Last Name) to the
variable ‘name’ and use the for-loop to print
your name letter by letter
• Print out how many letters there are
• Ignore space
Exercise 5, Code
# Let's print out your last name and name.

name = “Peter Rabbit“


count = 0

for char in name :


print(char)
if char != ‘ ‘ :
count = count +1

print( ‘The total numbers of letters= ‘, count)


Lecture Summary
• Use for statements
• If there is a regular change, for loops are efficient
• Use for loops with string and list
• Understand range(), in
• for i in range([start], stop[, step])
• It starts at ’start’ and it stops at number between ‘stop’
• Change as many times as the ‘step’(default: 1)
Practice Problem 1
• 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 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.

You might also like