Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Explore Courses

Introduction to Python

  • September 27, 2022
  • Home
  • Free Book

Table of Contents

Chapter 22 - for Repetition Structure

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab
  • Supporting Material
  • Download Supporting Material (Code): here

Repetition Structure

  • Repetition Structure
  • Definition
    • A Repetition Structure (a.k.a. Loop) is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
  • Purpose
    • The main purpose of Repetition Structure it to perform Repetitive Programming Tasks 
  • Repetition Structures in Python
  • Question
    • How many Repetition Structures are there in Python Programming Language?
  • Answer
    • The Two Repetition Structures of Python Programming Language are
      • for loop
      • while loop
  • Four Main Components of a Loop (Repetition Structure)
  • In Python Programming, the Four Main Components of a Loop are 
    • Initialization
    • Condition (Test)
    • Body of Loop
    • Increment / Decrement 
  • Loop Counter Variable
  • Question 
    • What is a Loop Counter Variable?
  • Answer
    • A Loop Counter Variable is an Integer Variable which is used to control the Number of Iterations of a Loop
  • for Loop
  • Definition
    • The for loop in Python is used to iterate the statements or a part of the program several times
  • Purpose
    • The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence objects
  • Importance
    • It is frequently used to traverse the data structures like the array and linked list
    • With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc.
  • Applications
    • The for statement works on strings, lists, tuples, other built-in iterables, and new objects
  • Advantages
    • In a for loop, the index of iteration is always a clearly defined variable
    • For loops can easily be used to iterate through elements of multidimensional arrays using nested for loops
  • Disadvantages
    • It cannot traverse through the elements in reverse fashion
    • You cannot skip any element as the concept of index is not there
    • You cannot choose to traverse to odd or even indexed elements too
  • Situatable to use
    • The for loop takes a collection of items and executes a block of code once for each item in the collection
  • Syntax - for Loop
				
					'''
if Condition is True the block will be executed otherwise the control will move to the endif statement
'''

for(initialization; condition; increment/decrement)
	BODY OF LOOP
				
			
  • Flowchart - for Loop
  • Steps – Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop
  • In Sha Allah, we will Solve Repetitive Programming Tasks with Fixed Number of Iterations using for Loop, in the following Steps:
    • Step 01: Completely and Correctly Understand the Real-world Repetitive Task
      • Step 1.1: Real-world Repetitive Task
      • Step 1.2: Real-world Repetitive Task Description
      • Step 1.3: Your Job – As a Software Developer
    • Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task?
      • Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task?
      • Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description
    • Step 03: Completely and Correctly Understand the Repetitive Programming Task Description 
      • Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description 
      • Step 3.2: Requirements Gathering 
      • Step 3.3: Requirements Analysis 
    • Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
      • If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
        • THEN
          • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop
          • Go to Step 05
        • ELSE
          • Repetitive Programming Task will be Solved with Variable Number of Iterations using for Loop
          • Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using for Loop (See Section:………………..for details) 
    • Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
      • Step 5.1: Write Down the Range / Pattern 
      • Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
        • START_VALUE
        • END_VALUE
        • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern)
    • Step 06: Plan and Design Solution to the Repetitive Programming Task 
      • Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2)
      • Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task 
      • Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed 
      • Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3)
      • Step 6.5: Select Suitable Programming Environment to Write Software (Code)
    • Step 07: Implementation 
      • Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5)
  • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
  • In Sha Allah, in the next Slides, we will step-by-step Solve a Real-world Programming Repetitive Task using the Steps mentioned in the previous Section

 

  • Example 1 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task
Step 1.1: Real-world Repetitive Task
  • Printing the First Five Positive Integer Numbers 
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Positive Integer Numbers on a Screen and teach her students about Positive Integer Numbers. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the First Five Positive Integer Numbers on the Output Screen 😊
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task?
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 5 times with little variations. Therefore, it can be treated as a Programming Repetitive Task.
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description
  • Write a Python Program which prints the First Five Positive Integer Numbers on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display First Five Positive Integer Numbers on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Only Consider First Five Positive Integer Numbers
    • Requirement 02
      • Display the First Five Positive Integer Numbers on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider First Five Positive Integer Numbers
    • Only Take First Five Positive Integer Numbers 
      • Zero is not a Positive Integer Number. Therefore, First Positive Integer Number will be 1 and the Last will be 5 (as per Requirements of the Client i.e., Ms. Samavi)
  • Analysis of Requirement 02 – Display the First Five Positive Integer Numbers on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
    • If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
    • THEN
      • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop
      • Go to Step 05
    • ELSE
      • Repetitive Programming Task will be Solved with Variable Number of Iterations using for Loop
      • Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using for Loop (See Section:………………..for details) 
  • Analysis – Repetitive Programming Task
    • Printing the First Five Integer Number i.e., 1 2 3 4 5
      • Total Number of Iterations = 5
      • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
    • Therefore, 
      • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop 😊
  •  
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • First Five Positive Integer Numbers, i.e.
    • 1 2 3 4 5
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 5
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +1 
Step 06: Plan and Design Solution to the Repetitive Programming Task
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2)
  • Input
    • Data
      • First Five Positive Integer Numbers, i.e.
        • 1 2 3 4 5
    • Instruction(s)
      • Display the First Five Positive Integer Numbers on the Output Screen
  • Output
    • 1 2 3 4 5
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the for Loop)
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task

SET start_value and end_value

SET counter to start_value

for counter < end_value

Print – counter 

INCREMENT by 1

EndFor

Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed
  • Step 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 1
    • Step 2.2: INIT – end_value = 6
    • Step 2.3: INIT – counter = start_value
  • Step 03: FOR counter < end_value
    • Step 3.1: DISPLAY – counter
    • Step 3.2: INCREMENT by +1
  • ENDFOR
  • Step 04: STOP
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3)
Step 6.5: Select Suitable Programming Environment to Write Software (Code)
				
					''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Salman
__license__               = Public Domain
__program__name__         = Display First Five Positive Integers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-05-2021
____End__Date___          = 01-06-2021
'''
				
			
Step 07: Implementation 
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5)
				
					'''
Purpose of Program
------------------
The main purpose of this Program is to Display the First Five Positive Integer Numbers on the Output Screen'''
try:
    # Input
    # Set up start_value and end_value
    start_value = 1
    end_value = 6

    # Initialize Loop Counter Variable 
    counter = start_value    # Initialization

    # Processing + Output
    # for Loop – To perform Repetitive Programming Task 

    for counter in range(start_value,end_value): # Condition
        print(counter, end= ' ')                 # Body of Loop

except:
    print("Exception Occurred")

'''
Extracting Four Components of a Loop from For Loop (written using range Function) Components of Loop Extracted from Code

======================================

INITIALIZTION: counter = start_value
CONDITION: counter < end_value
BODY OF LOOP: Print - number
INCREMENT: counter = counter + 1
'''

counter = start_value
for counter < end_value
    print - number
    counter = counter + 1
				
			

 

Execution of Code (Dry Run)
Python Statement
Processing
Memory
Output Screen

start_value = 1

Initialize Variable start_value with START_VALUE i.e., 1

start_value = 1

–

end_value = 1

Initialize Variable end_value with END_VALUE i.e., 6

start_value = 1

end_value = 6

–

counter = start_value

for counter < end_value

    print – number

    counter = counter + 1

Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code)

Iteration 01 

counter = start_value

start_value = 1

end_value = 6

counter = 1

–

Condition

  •  counter < end_value
  • 1 < 6
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 6

counter = 1

–

Body of Loop

print(counter)

start_value = 1

end_value = 6

counter = 1

1 –

counter = counter + 1

start_value = 1

end_value = 6

counter = 2

1 –

Iteration 02

Condition

  • counter < end_value 
  • 2 < 6
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 6

counter = 2

1 –

Body of Loop

Print(counter)

start_value = 1

end_value = 6

counter = 2

1 2 –

counter = counter + 1

start_value = 1

end_value = 6

counter = 3

1 2 –

Iteration 03

Condition

  • counter < end_value 
  • 3 < 6
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 6

counter = 3

1 2 –

Body of Loop

Print(counter)

start_value = 1

end_value = 6

counter = 3

1 2 3 –

counter = counter + 1

start_value = 1

end_value = 6

counter = 4

1 2 3 –

Iteration 04

Condition

  • counter < end_value 
  • 4 < 6
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 6

counter = 4

1 2 3 –

Body of Loop

Print(counter)

start_value = 1

end_value = 6

counter = 4

1 2 3 4 –

counter = counter + 1

start_value = 1

end_value = 6

counter = 5

1 2 3 4 –

Iteration 05

Condition

  • counter < end_value 
  • 5 < 6
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 6

counter = 5

1 2 3 4 –

Body of Loop

Print(counter)

start_value = 1

end_value = 6

counter = 5

1 2 3 4 5

counter = counter + 1

start_value = 1

end_value = 6

counter = 6

1 2 3 4 5

Iteration 06

Condition

  • counter < end_value 
  • 6 < 6
  • False 

Condition is False, So Loop will Terminate 

start_value = 1

end_value = 6

counter = 6

1 2 3 4 5

Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Steps given in this Lecture i.e.,
      • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
  •  

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Four Multiples of 10 on a Screen and teach her students about Multiples of a Number. 

Required Output

10 20 30 40 

 

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Three Multiples of 100 on a Screen and teach her students about Multiples of a Number. 

Required Output

100 200 300

 

                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Odd Numbers on a Screen and teach her students about Odd Numbers.

Required Output

1 3 5 7 9

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
    • For each Task write a Python Program using the following Steps mentioned in this Lecture i.e.,
      • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
  •  
  • Example 2 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task
Step 1.1: Real-world Repetitive Task
  • Printing the First Five Even Numbers in Reverse Order
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. To teach Even Numbers to her students, she wants to develop a Program which displays First Five Even Numbers in Reverse Order on a Screen. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the First Five Even Numbers in Reverse Order on the Output Screen 😊
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task?
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 5 times with little variations. Therefore, it can be treated as a Programming Repetitive Task.
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description
  • Write a Python Program which prints the First Five Even Numbers in Reverse Order on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display First Five Even Numbers in Reverse Order on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Only Consider First Five Even Numbers in Reverse Order
    • Requirement 02
      • Display the First Five Even Numbers in Reverse Order on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider First Five Even Numbers in Reverse Order
    • Only Take First Five Even Numbers in Reverse Order
      • First Five Even Numbers in reverse order will be starting from 8 and the Last will be 0 (as per Requirements of the Client i.e., Ms. Samavi)
  • Analysis of Requirement 02 – Display the First Five Even Numbers in Reverse Order on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
    • If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
    • THEN
      • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop
      • Go to Step 05
    • ELSE
      • Repetitive Programming Task will be Solved with Variable Number of Iterations using for Loop
      • Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using for Loop (See Section:………………..for details) 
  • Analysis – Repetitive Programming Task
    • Printing the First Five Even Numbers in Reverse Order i.e., 8 6 4 2 0
      • Total Number of Iterations = 5
      • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
    • Therefore, 
      • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop 😊
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • First Five Even Numbers in Reverse Order, i.e.
    • 8 6 4 2 0
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 8
  • END_VAUE = 0
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = -2
Step 06: Plan and Design Solution to the Repetitive Programming Task
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2)
  • Input
    • Data
      • First Five Even Numbers in Reverse Order, i.e.
        • 8 6 4 2 0
    • Instruction(s)
      • Display the First Five Even Numbers in Reverse Order on the Output Screen
  • Output
    • 8 6 4 2 0
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the for Loop)
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task

SET start_value and end_value

SET counter to start_value

for counter > end_value

Print Value of counter 

DECREMENT by -2

EndFor

Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed
  • Step 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 8
    • Step 2.2: INIT – end_value = -2
    • Step 2.3: INIT – counter = start_value
  • Step 03: FOR counter > end_value:
    • Step 3.1: DISPLAY – counter
    • Step 3.2: DECREMENT by -2
  • ENDFOR
  • Step 04: STOP
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3)
Step 6.5: Select Suitable Programming Environment to Write Software (Code)
 

 

				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Salman
__license__               = Public Domain
__program__name__         = First Five Even Numbers in Reverse Order
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-05-2021
____End__Date___          = 01-06-2021
'''
				
			
Step 07: Implementation 
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5)
				
					'''
Purpose of Program
-----------------
The main purpose of this Program is to Display the First Five Even Numbers in Reverse Order on the Output Screen'''

try:
    # Set up start_value and end_value
    start_value = 1
    end_value = 11
    number = 2

    # Initialize Loop Counter Variable 
    counter = start_value    # Initialization

    # Processing + Output
    # for Loop – To perform Repetitive Programming Task 
    for counter in range(start_value, end_value): # Condition
        print(number, "x", counter,"=",number * counter) # Body of Loop
except:
    print("Exception Occurred")

'''
Extracting Four Components of a Loop from For Loop (written using range Function) Components of Loop Extracted from Code

======================================

INITIALIZTION: counter = start_value
CONDITION: counter > end_value
BODY OF LOOP: Print - number
INCREMENT: counter = counter - 2
'''

counter = start_value
for counter > end_value
    print - counter
    counter = counter – 2
				
			

 

Execution of Code (Dry Run)
Python Statement
Processing
Memory
Output Screen

start_value = 8

Initialize Variable start_value with START_VALUE i.e., 8

start_value = 8

–

end_value = -2

Initialize Variable end_value with END_VALUE i.e., -2

start_value = 8

end_value = -2

–

counter = start_value

for counter > end_value

    print – counter

    counter = counter – 2

Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code)

 

counter = start_value

start_value = 8

end_value = -2

counter = 8

–

Iteration 01 

Condition

  • counter > end_value 
  • 8 > -2
  • True

Condition is True, So Execute Body of Loop

start_value = 8

end_value = -2

counter = 8

–

Body of Loop

Print(counter)

start_value = 8

end_value = -2

counter = 8

8 –

counter = counter – 2

start_value = 8

end_value = -2

counter = 6

8 –

Iteration 02

Condition

  • counter > end_value 
  • 6 > -2
  • True

Condition is True, So Execute Body of Loop

start_value = 8

end_value = -2

counter = 6

8 –

Body of Loop

Print(counter)

start_value = 8

end_value = -2

counter = 6

8 6 –

counter = counter – 2

start_value = 8

end_value = -2

counter = 4

8 6 –

Iteration 03

Condition

  • counter > end_value 
  • 4 > -2
  • True

Condition is True, So Execute Body of Loop

start_value = 8

end_value = -2

counter = 4

8 6 –

Body of Loop

Print(counter)

start_value = 8

end_value = -2

counter = 4

8 6 4 –

counter = counter – 2

start_value = 8

end_value = -2

counter = 2

8 6 4 –

Iteration 04

Condition

  • counter > end_value 
  • 2 > -2
  • True

Condition is True, So Execute Body of Loop

start_value = 8

end_value = -2

counter = 2

8 6 4 –

Body of Loop

Print(counter)

start_value = 8

end_value = -2

counter = 2

8 6 4 2 –

counter = counter – 2

start_value = 8

end_value = -2

counter = 0

8 6 4 2 –

Iteration 05

Condition

  • counter > end_value 
  • 0 > -2
  • True

Condition is True, So Execute Body of Loop

start_value = 8

end_value = -2

counter = 0

8 6 4 2 –

Body of Loop

Print(counter)

start_value = 8

end_value = -2

counter = 0

8 6 4 2 0

counter = counter – 2

start_value = 8

end_value = -2

counter = -2

8 6 4 2 0

Iteration 06

Condition

  • counter > end_value 
  • -2 > -2
  • False

Condition is False, So Loop will Terminate

start_value = 8

end_value = -2

counter = -2

8 6 4 2 0

Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Steps given in this Lecture i.e.,
      • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
  •  

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Positive Integer Numbers in Reverse Order on a Screen and teach her students about Positive Integer Numbers.

Required Output

5 4 3 2 1

 

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Four Multiples of 10 in Reverse Order on a Screen and teach her students about Multiples of a Number.

Required Output

40 30 20 10

 

                        Task  3

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Five Odd Numbers in Reverse Order on a Screen and teach her students about Odd Numbers.

Required Output

9 7 5 3 1

 

                        Task  4

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. She wants to display First Three Multiples of 100 in Reverse Order on a Screen and teach her students about Multiples of a Number.

Required Output

300 200 100

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
    • For each Task write a Python Program using the following Steps mentioned in this Chapter i.e.,
      • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
  • Example 3 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task
Step 1.1: Real-world Repetitive Task
  • Printing Table of 2
Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Math to Grade 1 students. To teach Tables to her students, she wants to develop a Program which displays the Table of 2 on the Output Screen. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the Table of 2 on the Output Screen 😊
Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task?
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 10 times with little variations. Therefore, it can be treated as a Programming Repetitive Task.
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description
  • Write a Python Program which prints the Table of 2 on the Output Screen.
Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description
  • The main objective of this Software is to display Table of 2 on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 01
      •  Only Consider Table of 2 from 1 to 10
    • Requirement 02
      • Display the Table of 2 from 1 to 10 on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider Table of 2
    • Only Take a Number (i.e., 2)
      • Table of 2 will be starting from 1 and the Last will be 10 (as per Requirements of the Client i.e., Ms. Samavi)
  • Analysis of Requirement 02 – Display the Table of 2 on the Output Screen
    • No need to Print or Save Output in any Format
    • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
    • If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
    • THEN
      • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop
      • Go to Step 05
    • ELSE
      • Repetitive Programming Task will be Solved with Variable Number of Iterations using for Loop
      • Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using for Loop (See Section:………………..for details) 
  • Analysis – Repetitive Programming Task
    • Printing the Table of 2
    •  Total Number of Iterations = 10
    • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore, 
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop 😊
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • Display the Table of 2 on the Output Screen, i.e.
    •  2 x 1 = 2
    • 2 x 2 = 4
    •  2 x 3 = 6 
    • 2 x 4 = 8
    • 2 x 5 = 10
    • 2 x 6 = 12
    • 2 x 7 = 14
    • 2 x 8 = 16
    •  2 x 9 = 18
    •  2 x 10 = 20
Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 10
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +1 
Step 06: Plan and Design Solution to the Repetitive Programming Task
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2)
  • Input
    • Data
      • Table of 2
    • Instruction(s)
      • Display the Table of 2 on the Output Screen
  • Output
    • 2 x 1 = 2
    • 2 x 2 = 4
    • 2 x 3 = 6 
    • 2 x 4 = 8
    • 2 x 5 = 10
    • 2 x 6 = 12
    • 2 x 7 = 14
    • 2 x 8 = 16
    • 2 x 9 = 18
    • 2 x 10 = 20
  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the for Loop)
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task

SET START_VALUE, END_VALUE and NUMBER

SET counter to START_VALUE

for counter < end_value

Print NUMBER, “x”, counter,”=”, NUMBER * counter

INCREMENT by +1

EndFor

Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed
  • Step 01: START
  • Step 02: Initialize Variables
    • Step 2.1: INIT – start_value = 1
    • Step 2.2: INIT – end_value = 11
    • Step 2.3: INIT – number = 2
    • Step 2.4: INIT – counter = start_value
  • Step 03: FOR counter < end_value:
    •  Step 3.1: DISPLAY – number, “x”, counter,”=”,number * counter
    • Step 3.2: INCREMENT by +1
  • ENDFOR
  • Step 04: STOP
Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3)
Step 6.5: Select Suitable Programming Environment to Write Software (Code)
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Salman
__license__               = Public Domain
__program__name__         = Display Table of 2
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System

__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-05-2021
____End__Date___          = 01-06-2021
'''
				
			
Step 07: Implementation 
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5)
				
					'''
Purpose of Program
------------------
The main purpose of this Program is to Display Table of 2 on the Output Screen'''
try:
    # Set up start_value and end_value
    start_value = 1
    end_value = 11
    number = 2
    # Initialize Loop Counter Variable 
    counter = start_value    # Initialization
    # Processing + Output
    # for Loop – To perform Repetitive Programming Task 

    for counter in range(start_value, end_value): # Condition
        print(number, "x", counter,"=",number * counter) # Body of Loop
except:
    print("Exception Occurred")

'''
Extracting Four Components of a Loop from For Loop (written using range Function) Components of Loop Extracted from Code

======================================
INITIALIZTION: counter = start_value
CONDITION: counter < end_value
BODY OF LOOP: Print - counter
INCREMENT: counter = counter + 1
'''

number = 2
counter = start_value
for counter < end_value
    print - (number, "x", counter,"=",number * counter)
    counter = counter + 1
				
			
Execution of Code (Dry Run)
Python Statement
Processing
Memory
Output Screen
start_value = 1Initialize Variable start_value with START_VALUE i.e., 1start_value = 1–
end_value = 11Initialize Variable end_value with END_VALUE i.e., 10

start_value = 1

end_value = 11

–
number = 2Initialize Loop counter Variable i.e., counter = start_value

start_value = 1

end_value = 11

number = 2

–

number = 2

counter = start_value

for counter < end_value

    print – (number, “x”, counter,”=”,number * counter)

    counter = counter + 1

Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code)

Iteration 01 

counter = start_value

start_value = 1

end_value = 11

number = 2

counter = 1

–

Condition

  • counter < end_value 
  • 1 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 1

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 1

2 x 1 = 2

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 2

2 x 1 = 2

–

Iteration 02

Condition

  • counter < end_value 
  • 2 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 2

2 x 1 = 2

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 2

2 x 1 = 2

2 x 2 = 4

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 3

2 x 1 = 2

2 x 2 = 4

–

Iteration 03

Condition

  • counter < end_value 
  • 3 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 3

2 x 1 = 2

2 x 2 = 4

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 3

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 4

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

–

Iteration 04

Condition

  • counter < end_value 
  • 4 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 4

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 4

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 5

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

–

Iteration 05

Condition

  • counter < end_value 
  • 5 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 5

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 5

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 6

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

–

Iteration 06

Condition

  • counter < end_value 
  • 6 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 6

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 6

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 7

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

–

Iteration 07

Condition

  • counter < end_value 
  • 7 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 7

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 7

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 8

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

–

Iteration 08

Condition

  • counter < end_value 
  • 8 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 8

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 8

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 9

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

–

Iteration 09

Condition

  • counter < end_value 
  • 9 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 9

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 9

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

–

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 10

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

–

Iteration 10

Condition

  • counter < end_value 
  • 10 < 11
  • True

Condition is True, So Execute Body of Loop

start_value = 1

end_value = 11

number = 2

counter = 10

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

–

Body of Loop

Print(counter)

start_value = 1

end_value = 11

number = 2

counter = 10

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

counter = counter + 1

start_value = 1

end_value = 11

number = 2

counter = 11

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

Iteration 11

Condition

  • counter < end_value 
  • 11 < 11
  • False

Condition is False, So Loop will Terminate

start_value = 1

end_value = 11

number = 2

counter = 11

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6 

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Steps given in this Chapter i.e.,
      • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while Loop)

 

 

                        Task  1

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. To teach Tables efficiently and quickly to her students, she wants to develop a Program, which takes a Positive Integer Number as Input and prints its Table on the Output Screen.

Required Output

Enter a Positive Integer Number: 3

3 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

3 x 5 = 15

3 x 6 = 18

3 x 7 = 21

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

 

                        Task  2

Completely and Correctly Understand the Task

Task Description

  • Ms. Samavi is teaching Math to Grade 1 students. To teach Multiples of a Number efficiently and quickly to her students, she wants to develop a Program, which takes a Positive Integer Number, Start Value and End Value as Input and prints its Multiples of the Positive Integer Number from Start value to the End value on the Output Screen.

Required Output

Enter a Positive Integer Number: 3

Enter Start Value: 8

Enter End Value: 15

 

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

3 x 11 = 33

3 x 12 = 36

3 x 13 = 39

3 x 14 = 42

3 x 15 = 45

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
    • For each Task write a Python Program using the following Steps mentioned in this Chapter i.e.,
      • Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using while Loop)

Nested for Loop

  • Example 1 - Steps (Solving a Repetitive Programming Tasks with Fixed Number of Iterations using Nested for Loop)
Step 01: Completely and Correctly Understand the Real-world Repetitive Task
Step 1.1: Real-world Repetitive Task
  • Printing the following Pattern 

*

**

***

****

*****

Step 1.2: Real-world Repetitive Task Description
  • Ms. Samavi is teaching Drawing to Grade 1 students. She wants to display a Triangle on a Screen and teach her students about Sides of Triangle. 
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which prints the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 02: Can we treat the Real-world Repetitive Task (Step 1.2) as a Repetitive Programming Task?
Step 2.1: Check Whether the Real-world Repetitive Task can be treated as a Programming Repetitive Task?
  • Yes, The Real-world Repetitive Task can be treated as a Programming Repetitive Task
    • Reason
      • The Real-world Repetitive Task repeats 5 times with little variations. Therefore, it can be treated as a Programming Repetitive Task.
Step 2.2: Convert the Real-world Repetitive Task Description (Step 1.2) into Repetitive Programming Task Description
  • Write a Python Program for Ms. Samavi, which prints the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 03: Completely and Correctly Understand the Repetitive Programming Task Description
Step 3.1: Completely and Correctly Understand the Repetitive Programming Task Description 
  • The main objective of this Software is to display the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 3.2: Requirements Gathering     
  • The main Requirements of this Software are as follows:
    • Requirement 01
      • Display the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 3.3: Requirements Analysis
  • Analysis of Requirement 01 – Only Consider Pattern
    • Use Nested Loop
      • Number of Lines = 5         ==> Output Loop
      • Number of * per Line       ==> Inner Loop
  •  
    •  
  • Analysis of Requirement 01 – Display the Pattern on the Output Screen
    • No need to Print or Save Output in any Format
      • Just Display it on the Output Screen
Step 04: Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
  • Check Whether the Repetitive Programming Task has Fixed Number of Iterations OR Variable Number of Iterations?
    • If (Number of Iterations of the Repetitive Programming Task are Known before Writing Program / Software == True)
    • THEN
      • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop
      • Go to Step 05
    • ELSE
      • Repetitive Programming Task will be Solved with Variable Number of Iterations using for Loop
      • Follow the Steps used to Solve a Repetitive Programming Task with Variable Number of Iterations using for Loop (See Section:……………..for details) 
  • Analysis – Repetitive Programming Task
    • Printing the Pattern
      • Total Number of Iterations = 5
      • Total Number of Iteration of the Repetitive Programming Task are Known before Writing Program / Software
  • Therefore, 
    • Repetitive Programming Task will be Solved with Fixed Number of Iterations using for Loop 😊
  •  
Step 05: Extract the following Information (based on Steps 3.1, 3.2 and 3.3)
Step 5.1: Write Down the Range / Pattern
  • Display the following Pattern on the Output Screen 😊

*

**

***

****

*****

Step 5.2: Extract the following Information from Range / Pattern (Step 5.1)
  • START_VALUE = 1
  • END_VAUE = 5
  • OL_COUNTER = START_VALUE
  • IL_COUNTER = END_VAUE
  • DIFFERENCE (i.e., Difference Between Two Consecutive Values in the Range / Pattern) = +1 
Step 06: Plan and Design Solution to the Repetitive Programming Task 
Step 6.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3, 5.1 and 5.2)
  • Input
    • Data
      • First Five Positive Integer Numbers, i.e.

*

**

***

****

*****

    • Instruction(s)
      • Display the First Five Positive Integer Numbers on the Output Screen
  • Output

*

**

***

****

*****

  • Processing
    • Use a Repetition Structure (In Sha Allah, we will use the for Loop)
  •  
Step 6.2: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task 

SET START_VALUE and END_VALUE

SET ol_counter to START_VALUE

for ol_counter <= end_value

     SET il_counter

for il_counter <= ol_counter:

    PRINT – *

         INCREMENT – il_counter by +1

     

     PRINT – newline

     INCREMENT – ol_counter by +1

EndFor

Step 6.3: Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed 
  • Step 01: START
  • Step 02: Initialize Variables

·        Step 2.1: INIT – start_value = 1

·        Step 2.2: INIT – end_value = 5

·        Step 2.3: INIT – counter = start_value

  • Step 03: FOR ol_counter <= end_value

·        Step 3.1: INIT – il_counter

·        Step 3.2: FOR il_counter <= ol_counter

·        Step 3.2.1: DISPLAY – *

·        Step 3.2.2: INCREMENT – il_counter by +1

·        Step 3.3: DISPLAY – newline

·        Step 3.4: INCREMENT – ol_counter by +1

  • EndFor

Step 6.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3)

Step 6.5: Select Suitable Programming Environment to Write Software (Code)
 
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Salman
__license__               = Public Domain
__program__name__         = Display the Pattern of * to form a Triangle
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-05-2021
____End__Date___          = 01-06-2021
'''
				
			
Step 07: Implementation 
Step 7.1: Write Software (based on Step 6.2, 6.3, 6.4 and 6.5)
				
					'''
Purpose of Program
------------------
The main purpose of this Program is to Display the Pattern on the Output Screen'''

try:
    # Set up start_value and end_value
    start_value = 0
    end_value = 5
    # Processing + Output
    # Initialize Outer Loop Variable
    ol_counter = start_value
    for ol_counter in range(start_value, end_value):
        # Initialize Inner Loop Variable 
        for il_counter in range(start_value, ol_counter + 1):
            print("*", end='')
        print("")
except:
    print("Exception Occurred")

'''
Extracting Four Components of a Loop from For Loop (written using range Function)

Components of Loop Extracted from Code

======================================
INITIALIZTION: counter = start_value
CONDITION: counter < end_value
BODY OF LOOP: Print - number
INCREMENT: counter = counter + 1
'''

ol = start_value
for ol_counter <= end_value:
    il_counter = 1
    for il_counter <= ol_counter:
        print("*", end = "")
        il_counter += 1
   print()
    ol_counter += 1
				
			

 

     Execution of Code (Dry Run)
Python Statement
Processing
Memory
Output Screen
Initialization
start_value = 0Initialize Variable start_value with START_VALUE i.e., 1start_value = 1–
end_value = 5Initialize Variable end_value with END_VALUE i.e., 5

start_value = 1

end_value = 5

–

ol_counter  = start_value

for ol_counter <= end_value:

    il_counter = 1

    for il_counter <= ol_counter:

        print(“*”, end = “”)

        il_counter += 1

    print()

    ol_counter += 1

Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code)

Iteration 01 – Outer Loop Condition – Outer Loop
 ol_counter = start_value

start_value = 1

end_value = 5

ol_counter = 1

–
 

Condition

  • ol_counter <= end_value
  • 1 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 1

–
 Body of Outer Loop
Iteration 01 – Inner LoopInitialization – Inner Loop Counter Variable (il_counter)
il_counter = 1

start_value = 1

end_value = 5

ol_counter = 1

il_counter = 1

–
Condition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 1 <= 1
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 1

il_counter = 1

 
Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 1

il_counter = 1

*
Increment – Inner Loop Counter (il_counter)

il_counter += 1

1 += 1

start_value = 1

end_value = 5

ol_counter = 1

il_counter = 2

*
Condition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 2 <= 1
  • False

Condition is False, Loop will Terminate

start_value = 1

end_value = 5

ol_counter = 1

il_counter = 2

*
 Print Statement of Outer Loop
 print()

start_value = 1

end_value = 5

ol_counter = 1

il_counter = 2

*

–

 Increment – Outer Loop Counter (ol_counter)
 

ol_counter += 1

1 += 1

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 2

*

–

Iteration 02 – Outer Loop Condition – Outer Loop
 

Condition

  • ol_counter <= end_value
  • 2 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 2

*

–

 Body of Outer Loop
Iteration 01 – Inner LoopInitialization – Inner Loop Counter Variable (il_counter)
il_counter = 1

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 1

*

–

Condition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 1 <= 2
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 1

*

–

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 1

*

*

Increment – Inner Loop Counter (il_counter)

il_counter += 1

1 += 1

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 2

*

*

Iteration 02 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 2 <= 2
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 2

*

*

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 2

*

**

Increment – Inner Loop Counter (il_counter)

il_counter += 1

2 += 1

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 3

*

**

Iteration 03 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 3 <= 2
  • False

Condition is False, Loop will Terminate

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 3

 
 Print Statement of Outer Loop
 print()

start_value = 1

end_value = 5

ol_counter = 2

il_counter = 3

*

**

–

 Increment – Outer Loop Counter (ol_counter)
 

ol_counter += 1

2 += 1

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 3

*

**

–

Iteration 03 – Outer LoopCondition – Outer Loop
 

Condition

  • ol_counter <= end_value
  • 3 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 3

*

**

–

Body of Outer Loop
 Initialization – Inner Loop Counter Variable (il_counter)
Iteration 01 – Inner Loopil_counter = 1

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 1

*

**

–

Condition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 1 <= 3
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 1

*

**

–

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 1

*

**

*-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

1 += 1

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 2

*

**

*-

Iteration 02 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 2 <= 3
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 2

*

**

*-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 2

*

**

**-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

2 += 1

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 3

*

**

**-

Iteration 03 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 3 <= 3
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 3

*

**

**-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 3

*

**

***-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

3 += 1

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 4

*

**

***-

Iteration 04 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 4 <= 3
  • True

Condition is False, Loop will Terminate

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 4

*

**

***-

 Print Statement of Outer Loop
 print()

start_value = 1

end_value = 5

ol_counter = 3

il_counter = 4

*

**

***

–

 Increment – Outer Loop Counter (ol_counter)
 

ol_counter += 1

3 += 1

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 4

*

**

***

–

Iteration 04 – Outer LoopCondition – Outer Loop
 

Condition

  • ol_counter <= end_value
  • 4 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 4

*

**

***

–

Body of Outer Loop
Iteration 01 – Inner LoopInitialization – Inner Loop Counter Variable (il_counter)
il_counter = 1

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 1

*

**

***

–

Condition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 1 <= 4
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 1

*

**

***

–

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 1

*

**

***

*-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

1 += 1

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 2

*

**

***

*-

Iteration 02 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 2 <= 4
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 2

*

**

***

*-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 2

*

**

***

**-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

2 += 1

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 3

*

**

***

**-

Iteration 03 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 3 <= 4
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 3

*

**

***

**-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 3

*

**

***

***-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

3 += 1

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 4

*

**

***

***-

Iteration 04 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 4 <= 4
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 4

*

**

***

***-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 4

*

**

***

****-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

4 += 1

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 5

*

**

***

****-

Iteration 05 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 5 <= 4
  • True

Condition is False, Loop will Terminate

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 5

*

**

***

****-

 Print Statement of Outer Loop
 print()

start_value = 1

end_value = 5

ol_counter = 4

il_counter = 5

*

**

***

****

–

 Increment – Outer Loop Counter (ol_counter)
 

ol_counter += 1

4 += 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 5

*

**

***

****

–

Iteration 05 – Outer LoopCondition – Outer Loop
 

Condition

  • ol_counter <= end_value
  • 5 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 5

*

**

***

****

–

Body of Outer Loop
Iteration 01 – Inner LoopInitialization – Inner Loop Counter Variable (il_counter)
il_counter = 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 1

*

**

***

****

–

Condition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 1 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 1

*

**

***

****

–

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 1

*

**

***

****

*-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

1 += 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 2

*

**

***

****

*-

Iteration 02 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 2 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 2

*

**

***

****

*-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 2

*

**

***

****

**-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

2 += 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 3

*

**

***

****

**-

Iteration 03 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 3 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 3

*

**

***

****

**-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 3

*

**

***

****

***-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

3 += 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 4

*

**

***

****

***-

Iteration 04 – Inner fwhLoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 4 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 4

*

**

***

****

***-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 4

*

**

***

****

****-

Increment – Inner Loop Counter (il_counter)

il_counter += 1

4 += 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 5

*

**

***

****

****-

Iteration 05 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 5 <= 5
  • True

Condition is True, Execute Body of Loop

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 5

*

**

***

****

****-

Body of Inner Loop
print – *

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 5

*

**

***

****

*****

Increment – Inner Loop Counter (il_counter)

il_counter += 1

5 += 1

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 6

*

**

***

****

*****-

Iteration 06 – Inner LoopCondition – Inner Loop

Condition

  • il_counter <= ol_counter
  • 6 <= 5
  • False

Condition is False, Loop will Terminate

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 6

*

**

***

****

*****-

 Print Statement of Outer Loop
 print()

start_value = 1

end_value = 5

ol_counter = 5

il_counter = 6

*

**

***

****

*****

–

 Increment – Outer Loop Counter (ol_counter)
 

ol_counter += 1

5 += 1

start_value = 1

end_value = 5

ol_counter = 6

il_counter = 6

*

**

***

****

*****

–

Iteration 06 – Outer LoopCondition – Outer Loop
 

Condition

  • ol_counter <= end_value
  • 6 <= 5
  • True

Condition is False, Loop will Terminate

start_value = 1

end_value = 5

ol_counter = 6

il_counter = 6

*

**

***

****

*****

–

Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊
Chapter 21 - while Repetition Structure​
  • Previous
Chapter 23 - Functions in Python
  • Next
Share this article!
Facebook
Twitter
LinkedIn
About Us

Ilm O Irfan Technologies believes that honest dedicated hard work with the ethical approach of commitment true to every word, and trust-building is the road to success in any field of business or life.

Quick Links
  • About Us
  • Contact
Useful Links
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Support
  • FAQ
Subscribe Our Newsletter
Facebook Twitter Linkedin Instagram Youtube

© 2022 Ilmo Irfan. All Rights Reserved.