0% found this document useful (0 votes)
5 views51 pages

Week 9 - Loop _ Nested Loop

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views51 pages

Week 9 - Loop _ Nested Loop

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 51

LAB MODULE A

Coding & Big Data

Week 9
Iteration in Python
Introduction to Iteration
Outline While Loops

Practice I
For Loops

Practice II

Keywords
Nested Loop
Outline
Nested While
(cont'd)
Practice III

Nested For Loop

Practice IV

Submission

References
Introduction to Iteration
What is Iteration is a repeating process to
generate outcomes
Iteration? Iteration is also called loop. Loop is
a programming structure that
repeats a sequence of instructions
until a specific condition is met,
usually until a variable reach certain
value.
Example Of Iteration In Real Life
You are playing basketball. Then your coach asking you to score the hoops 5 times. This
means, you will repeatedly shooting the ball until you can score 5 times.
While Loops
While Loops is an iteration method that will repeat
the code inside based on its condition / expression.

-If the condition / expression is TRUE, it will


repeat the code again

-If the condition / expression is FALSE, it will exit


the loop and continue to the next step
Example Steps to Solve

You are given a task to create a 1. Take the input from the user and store it into a variable

program to count up starting 2. Create a while loop with condition the number is less or
from number inputed by the user
equal to 10
until 10. This means, when the
number is above 10, the program 3. While the loop is running, print out the number value
will be terminated
4. Increment the value of the variable by 1
Result
Be Careful!
There is something called Infinite
loop. Infinite loop can happen
because of the condition of the loop
is always true (will never change to
false)
This code is the example of infinite loops, because the
number value will always be 5 (always less than 10)
Practice I
For student whose last digit of student id is odd
Recreate previous program, by changing the maximum number is 15 , and the output will have your name
similar to the following picture
For student whose last digit of student id is even
Recreate previous program, by changing the maximum number is 8 , and the output will have your last 2
student digits, similar to the following picture
For Loops
For Loops is used for iterating over a sequence.
Different than while loops, for loops by default has an
increment of the condition variable by 1. So if we
want to have an increment by 1 we don’t need to do
anything, but if we want the increment by other
values, we can define it as our liking
range()
range() is a function used in for loops to iterate over numbers inside the function. range() function can
hold 1, 2, and 3 parameters.

● range(x) [with 1 parameter] means we will iterate from 0 to x-1, with increment by 1

● range(x,y) [with 2 parameters] means we will iterate from x to y-1, with increment by 1

● Range(x,y,z) [with 3 parameters] means we will iterate from x to y-1, with increment by z
*X: Stop

Example of range(x)

Source Code

Output
*X: Start , Y: Stop

Example of range (x,y)


Source Code

Output
*X: Start , Y: Stop , X: Step

Example of range(x,y,z)
Source Code

Output
Keywords
break
break keyword is used to forcefully exit the loop even though the condition is still true

Source Code Output


continue
continue keyword is used to skip the current iteration and continue with the next iteration

Source Code Output


Practice II:
For student whose last digit of student id is odd
Create a program contains for loops that count up by 3. It also will ask for an input about the starting
number. The maximum number for the program is 21. If the number reach 7, it will print (“Lucky seven”)
and end the loop . The output is going to be something like this :
For student whose last digit of student id is even
Create a program contains for loops that count up by 2. It also will ask for an input about the starting
number. The maximum number for the program is 10.
5 If the number reach 7, it will print (“Lucky seven”)
and end the loop . The output is going to be something like this :
Steps to solve (Algorithm)
1. Get the user input and store it into a variable for starting number.
(don’t forget to convert the input to integer)
2. Create the for loop with range (x,y,z).
3. Inside of loop:
a. Check if the number reaches 7, print ("Lucky Seven")
and end the program.
b. Print the iteration number
Nested Loop
What is Nested Basically, a loop inside a loop.

Loop?
Loop can be nested in python. When a
loop is nested inside another loop, the
inner loop runs many times inside the
outer loop. In each iteration of the outer
loop, the inner loop will be re-started.
Nested While Loop
Nested While
Loop

A while Loop inside a while loop.


Question
You want to make the program that shows the asterisk (*) triangle with 5 lines that the base side

is facing down.
Steps to solve (Algorithm)
1. Create a variable for the outer long

2. Create the outer loop to decide the number of the lines.

a. Create a variable for the inner long

b. Create the inner loop to generate the *


Result

Output

Source Code
Practice III:
For student who have the last digit of student ID is odd, create a code to generate output as picture 1.

For student who have the last digit of student ID is even, create a code to generate output as picture 2.

Picture 1 Picture 2
8 lines 10 lines
Nested For Loop
Nested For Loop

A for Loop inside a for loop.


Question
You want to make the program that shows the triangle that contains a sequential number with 4

lines that the base side is facing up.


Steps to solve (Algorithm)
1. Create the outer loop to decide the number of the lines

a. Create the inner loop to generate the numbers


Result

Source Code Output


Practice IV:
For student who have the last digit of student ID is odd, create a code to generate output as picture 1.

For student who have the last digit of student ID is even, create a code to generate output as picture 2.

Picture 1 Picture 2
All loop types can be used inside other
loops with different types too

For loop can be used inside while loop, and vice versa
Additional Practice
(Optional)
Optional Excercise
Create a program to do login validation with username and password. In this case, the correct
username and password are admin. If the password and username are not correct and not
matched, it will do an iteration output to do login attempts (Max 3 times). This exercise will
need a combination between conditional statements and looping.

*Note: You will need to learn the break statement to help this exercise.

You may have different output but make sure you use looping to do this exercise.
Optional Exercise
Expected Output

Wrong Answer!

Correct Answer!
Submission
Submission
Screenshot your codes and terminals, put them into pdf file. Don’t forget to put your name and
student id in the top of the file.

Practice I->Screenshot your program code and output depending on your student id last digit
Practice II->Screenshot your program code and output depending on your student id last digit
Practice III->Screenshot your program code and output depending on your student id last digit
Practice IV->Screenshot your program code and output depending on your student id last digit
Questions?
References
Python Loops - For, While, Nested Loops With Examples (softwaretestinghelp.com)
Python While Loops
Python For Loops

You might also like