14 Nested For
14 Nested For
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.
Nested for
14
Global Leadership School
Handong Global University
Learning Objectives
• Understand nested for
• Understand break
• Exercise using nested for
Nested for
• Using for() in for() loop
• Example below
# List
for fr in fruit :
count = 0
for f in fr :
print(f)
count = count + 1
i =0
i =0
i =1
i =2
i =1
nest for
for i in range(2,7,2) : # times table : 7, 8
print ("1st level = ", i)
for i in range(7, 9) :
for j in range(1,5,1) : print (i, "th table")
print(" 2nd level = ", j)
print(" i * j = ", i*j) for j in range(1,10,1) :
print( " ", i, " * ", j, " = ", i*j)
What will be printed out?
# what is the value of a
a=0
for i in range(10) :
a=a+1
for j in range(10) :
a=a+1
print(a)
What will be printed out?, Answer
# what is the value of a
a=0
for i in range(10) :
a=a+1
for j in range(10) :
a=a+1
print(a)
What will be printed out?
# what is the value of a
a=0
for i in range(10) :
a=a+1
for j in range(10) :
a=a+1
print(a)
What will be printed out?, Answer
# what is the value of a
a=0
for i in range(10) :
a=a+1
for j in range(10) :
a=a+1
print(a)
What will be printed out?
# What will be printed next?
fruit = "banana"
count = 0
for char in fruit :
if char == ’a’ :
count = count + 1
print(count)
What will be printed out?, Answer
# # What will be printed next?
fruit = "banana"
count = 0
for char in fruit :
if char == ’a’ :
count = count + 1
print(count)
Bubble sort
• Reorganizing numbers and characters in a certain
order.
• The simplest way to arrange.
Unsorted
Sorted
Bubble sort
numbers = [ 5, 4, 1, 3, 9, 2, 15 ]
print(numbers)
Bubble sort Exercise
• Use the Bubble sort from the previous
example
• Print out the larger numbers first
• After, find the difference between the largest
number and smallest.
Bubble sort Exercise
number01 = int(input(”Enter the first number: "))
number02 = int(input(”Enter the second number: "))
number03 = int(input(”Enter the third number: "))
number04 = int(input(”Enter the fourth number: "))
number05 = int(input(”Enter the fifth number: "))
print(numbers)
print(”The difference between the largest and the smallest is",
int(numbers[0])-int(numbers[4]))
break
• Stop to get out of the loop
• After the loop, the execution flow passes over to
the first instruction.
fruit = "banana"
count = 0
print(count)
break example
• Nested for
count = 0
for i in range(5) :
for j in range(3) :
if (i + j) % 5 != 0 :
count = count + 1
print('i='+str(i), 'j='+str( j), "i+j =", i+j, "and count =", count)
else :
break
print('i='+str(i), 'j='+str( j), "i+j =", i+j, "last count: ", count)
break Exercise
• Calculate how many items you can buy with
the amount you currently have.
• Receive the how much you have, the price
and the quantity of the product you want to
buy.
• After every purchase, print the balance
• If the balance is insufficient, end the
repetition using break
break Exercise, Code
balance = int(input(”How much do you have? "))
cost = int(input(”What is the price of the product? "))
num = int(input(”How many do you want? "))
• Check process
• Starting with 2, divide the number of inputs,
• Repeat dividing up to (number – 1)
• If it is divided, it’s not a prime number
• If it is not divided, it is a prime number
• If it is divided, change the “flag” variable to False
Prime Number Check
# Check prime number
prime_yes = True
if prime_yes == True :
print(num, "is a prime number")
else :
print(num, "is not a prime number")
Exercise 1
• Enter an English word
• Enter one of the letter in the received word
• Find where the letter is located
• If the letter doesn’t exist
Exercise 1, Code and Result
word = input(”Enter a word: ")
letter = input(”Enter a letter to find the location: ")
count = 0
for c in s :
if c == "a" :
count += 1
• Understand break
• Stop to get out of the loop
• 2 times
• 7 times
• 9 times
• 14 times
Practice Problem 1, Answer
• How many times does the “print()” statement
execute? for i in range(2) :
for j in range(7) :
print (“ $")
• 2 times
• 7 times
• 9 times
• 14 times
Practice Problem 2
• What is the output?
a=0
for i in range(5) :
a=a+1
for j in range(5) :
a=a+2
print(a)
• 50
• 55
• 60
• 65
Practice Problem 2, Answer
• What is the output?
a=0
for i in range(5) :
a=a+1
for j in range(5) :
a=a+2
print(a)
• 50
• 55
• 60
• 65
Thank you
14 Nested for
Contact Info
unitwinlms@handong.edu
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.