Chapter 20 - Repetition Structures in Python
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Selection Structures for Decision Making
In previous Chapter, I presented the following main concepts:
- Real-world Problem
- Definition
- A Real-world Problem is defined as a matter or situation regarded as unwelcome or harmful and needing to be deal with and overcome
- Purpose – Why Solve a Real-world Problem?
- To improve the quality of human life
- Definition
- Programming Problem
- Definition
- To analyze the program before writing it is known as Programming Problem
- Purpose
- The main purpose of Programming Problem is to identify the following parameters from the given Program:
- Identify the input
- Identify Purpose of Program and Operators performed on a Program
- Display the Output
- The main purpose of Programming Problem is to identify the following parameters from the given Program:
- Definition
- Python Control Structures
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Selection Structure
- Repetition Structure
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Definition
- A Sequence Structure executes a Sequence of Python Statements one by one in the order in which they are written
- Purpose
- The main purpose of Sequential Structure it to
- Execute Python Statements one by one in the order
- The main purpose of Sequential Structure it to
- Definition
- Selection Structure
- Definition
- A Selection Structure (a.k.a. Conditional Structure) is a feature of Python Programming Language that is used to perform different actions / tasks based on Decision Making (whether a specific Condition is True or False)
- Purpose
- The main purpose of Selection Structures is
- Decision Making
- The main purpose of Selection Structures is
- Definition
- Repetition Structure
- Definition
- A Repetition Structure 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 Task
- Definition
- Decision
- Definition
- Decision can be defined as an act of Selecting / Choosing one action from several alternatives
- Definition
- Decision-Making
- Definition
- Decision-Making is defined as the process of making Decision
- Purpose
- The main purpose of Decision-Making is to
- Take Most Suitable Actions to Perform Various Real-world Tasks
- The main purpose of Decision-Making is to
- Definition
- Decision Making in Python
- Definition
- In Python Program, Outcome of a Condition (Relational Expression, logical Expressions, Boolean Expressions and Arithematic Expressions) is used to make a Decision (i.e., which Block of Code will be executed)
- Definition
- Conditionals in Python
- Definition
- A statement that controls the flow of execution depending on some condition
- Purpose
- The main purpose of Conditional Statements is to perform different operations depending upon the condition
- Definition
- Conditions in Python
- Definition
- The Boolean Expression is a conditional statement which determines which block will be executed
- Purpose
- Conditions control the flow of execution of Programs based on conditionals
- Definition
- Relational Operator
- Definition
- Relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities
- Definition
- If Statement
- Definition
- if statement is an expression that can be evaluated as True or False and is called a conditional test
- It executes a set of statements conditionally, based on the value of a logical expression
- Definition
- Purpose
- The main purpose if statement is that it takes the form of an if test, followed by one or more optional elif (“else if”) tests and a final optional else block
- Python if Statement is used for decision-making operations
- The main purpose if statement is that it takes the form of an if test, followed by one or more optional elif (“else if”) tests and a final optional else block
- Else Statement
- Definition
- else condition can be optionally used to define an alternate block of statements to be executed if the boolean expression in the if condition evaluates to False
- Definition
- Purpose
- In programming languages, The else keyword is used in conditional statements (if statements), is an alternative statement that is executed if the result of a previous test condition evaluates to false
- Elif Statement
- Definition
- The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed
- Purpose
- The main purpose is that the elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions. The elif block is executed if the specified condition evaluates to True
- Definition
- Nested IF
- Definition
- if statements inside if statements, this is called nested if statements
- Purpose
- The main purpose of Nested if statements are often used when you must test a combination of conditions before deciding on the proper action
- Definition
Real-world Repetitive Task
- Real-world Repetitive Task
- Definition
- A Real-world Repetitive Task is made up of a set of action(s) that are repeated over and over again (and may involve small variations in each iteration)
- Purpose
- The main purpose of a Real-world Repetitive Task is to
- Break a big Real-world Task into several (very similar) Sub-tasks and repeat a set of actions(s) over and over again on Sub-tasks to complete the big Real-world Task
- The main purpose of a Real-world Repetitive Task is to
- Importance
- In any field of life, you cannot become an Authority in that field, unless and until you Repeat (Practice) the same set of Real-world Tasks under the supervision of a Guru (i.e., a Teacher, who is an Authority in that field)
- Remember the famous quote 😉
- Practice enables a Man to achieve Excellence in any Real-world Task
- Advice of My Respected Teacher
- Adeel! Always put 100% Effort with Sincerity to Achieve PERFECTION in your field (i.e., in any Real-world Task your Role Model MUST BE Our Beloved Prophet Hazrat Muhammad S.A.W.W. (حضرت محمد صلی اللہ علیہ وسلم) and Sahaba Karam R.A. (صحابہ کرام رضی اللہ عنہ). However, you will never achieve PERFECTION, but you will achieve EXCELLENCE. Also, when you put 100% Effort into Sincerity then do not bother about the RESULTS (OUTCOMES). Sometimes you will get your desired RESULTS and sometimes you will not get your desired RESULTS. Just keep putting 100% Effort with Sincerity till Death 😊
- To become an Authority in your field, never ever compromise on Character.
- The 3 Main Pillars of Character which we learn from the pious lives of Our Beloved Prophet Hazrat Muhammad S.A.W.W. (حضرت محمد صلی اللہ علیہ وسلم) and Sahaba Karam R.A. (صحابہ کرام رضی اللہ عنہ) are:
- Truth
- Honesty
- Justice
- May Allah give us the taufeeq (توفیق) to become a Man of Character and safeguard our Character till Death. Ameen
- Applications
- To perform any Real-world Task with the highest level of quality, you need to
- Practice it (i.e., REPEAT the same set of action(s) repeatedly)
- To perform any Real-world Task with the highest level of quality, you need to
- Strengths
- Using the phenomenon of REPETITION, you can achieve Excellence in any Real-world Task
- Challenges
- REPEATING the same set of action(s) is a very tough Task and majority of people in this world fail to do this
- Challenges
- Using the phenomenon of REPETITION, you can achieve Excellence in any Real-world Task
- Question
- How to overcome the problem of not being able to REPEAT the same set of actions(s) repeatedly?
- Answer
- To perform the same set of action(s) REPEATEDLY to Become an Authority in any field of life, you need to follow a Two-Step Process till Death
- Step 01: Have a Guru (Mentor) in Life
- Step 02: Report your Daily Work Progress on your Real-world Tasks to your Guru on DAILY BASIS
- To perform the same set of action(s) REPEATEDLY to Become an Authority in any field of life, you need to follow a Two-Step Process till Death
- Motivational Seminar – Guru ke Har Baat Gur Hoti Hai. Gur Ko Nhe Guru Ko Pakar (گرو کے ہر بات گر ہوتی ہے۔ گر کو نہیں گرو کو پکڑ)
- Suitable to Use
- To learn any Real-world Task, we should use the phenomenon of REPETITION 😊
- Real-world Repetitive Task
- Some of the Real-world Repetitive Tasks (to become an authority in your field), which we learned from the lives of Our Beloved Prophet Hazrat Muhammad S.A.W.W. and Sahaba Karam R.A. are:
- Task 01: Go to bed immediately after Namaz.e.Isha
- Task 02: Start performing your Real-world Tasks after Namaz.e.Fajr
- Hazrat Muhammad S.A.W.W. said to the nearest meaning
- Task 03: Take Three Healthy Meals on Daily Basis (for e.g., 1 Liter Milk, at least 10 Dates and 5 – 10 Spoons of Honey)
- My Respected Teacher said
- Task 04: At least 30 Mins Brisk Walk / Running / Game (with Family) on Daily Basis
- My Respected Teacher said
- My Respected Teacher said
- If you Practice Real-world Repetitive Task 01, Real-world Repetitive Task 02, Real-world Repetitive Task 03 and Real-world Repetitive Task 04 then you can Achieve Excellence in Physical Health In Sha Allah
- Task 05: Be Happy, Keep Happy
- My Respected Teacher said
- If you Practice this Real-world Repetitive Task, then you can Achieve Excellence in Mental Health In Sha Allah
- My Respected Teacher said
- Task 06: Do one good deed with any Creature of Allah (to please Allah) on Daily Basis
- My Respected Teacher said
- If you Practice this Real-world Repetitive Task, then you can Achieve Excellence in Social Health In Sha Allah
- My Respected Teacher said
- Task 07: Offer 5 Namaz (نماز) in Masjid (مسجد) with Takbeer.e.Owla (تکبیر اولی) on Daily Basis
- Task 08: Recite Durood Sharif (درود شریف) for at least 30 minutes on Daily Basis
- Task 09: Do Dua (دعا) for at least 5 minutes on Daily Basis
- Task 10: Fast in the Month of Holy Ramazan
- Task 11: Give Sadqa (صدقہ) on Daily Basis
- Task 12: Give Zakat (زکوۃ) (if Zakat is obligatory on you)
- Task 13: Perform Hajj (حج) (if Hajj is obligatory for you)
- My Respected Teacher said
- If you Practice the Real-world Repetitive Task from 07 to 13, then you can Achieve Excellence in Spiritual Health In Sha Allah
- My Respected Teacher said
- Task 14: Love, Respect and Serve Humanity to please Allah
- My Respected Teacher said
- Iteration
- Definition
- Iteration is defined as the repetition of a set of action(s) to produce an Outcome (or Result).
- Note
- In one Iteration, the entire set of action(s) is
- executed only once
- The Outcome (Result) of each Iteration is the Starting Point for the next Iteration
- The Sequence of Iterations will approach the Outcome (Result)
- In one Iteration, the entire set of action(s) is
- Types of Real-world Repetitive Tasks
- The Two Main Types of Real-world Repetitive Tasks are
- Real-world Repetitive Tasks with Fixed Number of Iterations
- Real-world Repetitive Tasks with Variable Number of Iterations
Real-world Repetitive Tasks with Fixed Number of Iterations
- Real-world Repetitive Tasks with Fixed Number of Iterations
- Definition
- Real-world Repetitive Tasks with Fixed Number of Iterations are those Real-world Tasks in which the Number of Iterations needed to complete a Real-world Task are known before we start executing that Real-world Task
- Examples
- Some of the Real-world Repetitive Tasks with Fixed Number of Iterations are as follows
- Fasting in the Holy Month of Ramazan
- Calculating Average Marks of Class in Python Course
- Writing the following Text 10 times on a page
- Allah is One and He has control over everything. Hazrat Muhammad S.A.W.W. is the last Prophet of Allah is the Role Model for us in all walks of life. Alhamdulillah 😊
- Some of the Real-world Repetitive Tasks with Fixed Number of Iterations are as follows
Real-world Repetitive Tasks with Variable Number of Iterations
- Real-world Repetitive Tasks with Variable Number of Iterations
- Definition
- Real-world Repetitive Tasks with Variable Number of Iterations in which the Number of Iterations needed to complete a Real-world Task are not known before we start executing that Real-world Task
- Examples
- Some of the Real-world Repetitive Tasks with a Variable Number of Iterations are as follows
- Number of Times you Recite Durood Sharif (درود شریف) (in 30 minutes) every day
- Number of Glasses of water you drink everyday
- Number of friends you meet (or talk to) everyday
- Some of the Real-world Repetitive Tasks with a Variable Number of Iterations are as follows
Solving Real-world Repetitive Tasks using Programming Languages
- Control Structures of Life
- Our Life comprises three main Structures i.e.,
- Sequential Structure
- Selection Structure
- Repetition Structure
- One day we were born, and a day will come when we die i.e., a Sequential Structure from Birth to Death 😊
- During this Journey from Birth to Death (Sequential Structure), we take various Very Important Decisions (i.e., Selection Structure) and these Decisions force us to repeat certain Real-world Tasks (i.e., Repetition Structure)
- For example, if you take the Decision to do BS(CS) from COMSATS University, Lahore then you will have to Repeatedly attend University for the next 4 years of your life 😊
- My Respected Teacher advised me
- Adeel! Always Remember that
- دنیا امتحان کی جگہ ہے اطمینان کی جگہ نہیں ہے
- Adeel! Always Remember that
- On the Day of Judgment, we will have to answer about all the deeds (good or bad) to Allah and based on our performance in this world we will either enter Paradise or Hell.
- The only Goal of our Lives is to
- Achieve Excellence in Friendship of Allah
- Therefore, in this Journey from Birth to Death (Sequential Structure), whenever you take a Decision (Selection Structure) and Repeat a Real-world Task (Repetition Structure) based on your Decision, always check
- Whether you are getting close to Allah OR far from Allah?
- Always Perform those Real-world Tasks which take you Close to Allah and Never Ever Perform those Real-world Tasks which take you far away from Allah
- May Allah bless all of us with His highest level of Friendship Ameen 😊
- Solving Real-world Repetitive Tasks using Programming Languages
- Question
- How can we solve Real-world Repetitive Tasks using Programming Languages?
- Answer
- Use Repetition Structures of Programming Languages
- Recall - Python Control Structures
- Since Python Programming Language aims to solve Real-world Tasks using Programming Control Structures, therefore, similar to Control Structures of Life, the three main Python Control Structures are
- Sequential Structure
- Selection Structure
- Repetition Structure
- Sequential Structure
- Definition
- A Sequence Structure executes a Sequence of Python Statements one by one in the Order in which they are written
- Purpose
- The main purpose of Sequential Structure it to
- Execute Python Statements one by one in the Order in which they are written
- The main purpose of Sequential Structure it to
- Programming Languages based on Sequential Structures
- Some of the popular and widely used Programming Languages which execute Programs using Sequential Structure are
- Java
- C
- C++
- Python
- FORTRAN
- Basic
- PL1
- Pascal
- Some of the popular and widely used Programming Languages which execute Programs using Sequential Structure are
- Selection Structure
- Definition
- A Selection Structure (a.k.a. Conditional Structure) is a feature of Python Programming Language that is used to perform different actions / tasks based on whether a specific Condition is True or False
- Purpose
- The main purpose of Selection Structures is
- Decision Making and
- Branching i.e., Choosing between two or more alternative paths
- The main purpose of Selection Structures is
- Examples of Selection Structures in Python
- Selection Structures used in Python Programming Language are
- if
- elif
- else
- Selection Structures used in Python Programming Language are
- Note
- For details on Selection Structures
- See Chapter 14 – Decision Making Statements in Python
- For details on Selection Structures
Repetitive Programming Task
- Repetitive Programming Task
- A Repetitive Programming Task is defined as a Programming Task in which there is little variation from one item to the next item of the Programming Task
- Types of Repetitive Programming Task
- The Two Main Types of Repetitive Programming Tasks are
- Repetitive Programming Task with Fixed Number of Iterations
- Repetitive Programming Task with Variable Number of Iterations
Repetitive Programming Task with Fixed Number of Iterations
- Repetitive Programming Task with Fixed Number of Iterations
- Definition
- Repetitive Programming Task with Fixed Number of Iterations are those Repetitive Programming Tasks in which the Number of Iterations is known at the Code Time
- Examples
- Some of the Repetitive Programming Tasks with Fixed Number of Iterations are as follows
- Example 01
- Programming Task 01
- Display the following Message 5 times on the Output Screen
- Number of Iterations
- 5 (i.e., Fixed)
- Number of Iterations
- Display the following Message 5 times on the Output Screen
- Programming Task 01
- Example 02
- Programming Task 02
- Display Names of 30 Students enrolled in Python Course
- Number of Iterations
- 30 (i.e., Fixed)
- Number of Iterations
- Display Names of 30 Students enrolled in Python Course
- Programming Task 02
- Example 03
- Programming Task 03
- Display all Even Numbers in the Range 51 – 100
- Number of Iterations
- 50 (i.e., Fixed)
- Number of Iterations
- Display all Even Numbers in the Range 51 – 100
- Programming Task 03
- Example 04
- Programming Task 04
- Display Average Marks of 100 Students enrolled in Python Course
- Number of Iterations
- 100 (i.e., Fixed)
- Number of Iterations
- Display Average Marks of 100 Students enrolled in Python Course
- Programming Task 04
- Example 05
- Programming Task 05
- Print the Table of 2 on the Output Screen
- Number of Iterations
- 10 (i.e., Fixed)
- Number of Iterations
- Print the Table of 2 on the Output Screen
- Programming Task 05
- Note
- For majority of Repetitive Programming Tasks, the Number of Iterations is
- Fixed
Repetitive Programming Task with Variable Number of Iterations
- Repetitive Programming Task with Variable Number of Iterations
- Definition
- Repetitive Programming Task with Variable Number of Iterations are those Repetitive Programming Tasks in which the Number of Iterations are not known at the Code Time
- Examples
- Some of the Repetitive Programming Tasks with Variable Number of Iterations are as follows
- Example 01
- Programming Task 01
- Ms. Samavi running a Python Program (called addition.py) to strengthen and practice basic addition concepts of Math. The addition.py Program works as follows: (1) Takes Two Integer Numbers as Input (from User) and displays their Sum on the Output Screen, and (2) Asks Ms. Samavi whether she wants to calculate Sum of Two Integer Number again or quit?
- Number of Iterations
- Variable
- Programming Task 01
- Reason for Variable Number of Iterations
- In above Programming Task, the Number of Iterations will be Variable each time Ms. Samavi runs addition.py Python Program because
- We don’t know, how many times (Iterations) Ms. Samavi would like to calculate Sum of Two Integer Numbers?
- In above Programming Task, the Number of Iterations will be Variable each time Ms. Samavi runs addition.py Python Program because
- Example 01
- Some of the Repetitive Programming Tasks with Variable Number of Iterations are as follows
Menu – in Programming
- Menu – in Programming
- Definition
- In Programming, a Menu is defined as a List of Options (Choices)
- Example – Menu in Programming
- Consider the following Menu for a Library System developed using Python Programming Language
- Enter a to Add a New Book
- Enter d to Delete an Existing Book
- Enter u to Update an Existing Book
- Enter s to Search a Book
- Enter e to Exit
- Please Enter Your Choice:
- Consider the following Menu for a Library System developed using Python Programming Language
- Menu Driven Program
- Definition
- A Menu Driven Program is a Program which takes Input from a User by displaying a Menu (i.e., a List of Options (Choices)) and the User indicates his / her Choice form the given Menu
- Examples – Menu Driven Programs
- Some of the popular and widely used Examples of Menu Driven Program are as follows
- ATM Machine
- Online Shopping Websites
- Microwave Oven
- Washing Machine
- iPod or Mobile Phone
- And Many More 😊
- Some of the popular and widely used Examples of Menu Driven Program are as follows
- Note
- For majority of Menu Driven Repetitive Programming Tasks, the Number of Iterations is
- Variable
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
- Real-world Repetitive Task
- Definition
- A Real-world Repetitive Task is made up of a set of action(s) that are repeated over and over again (and may involve small variations in each iteration)
- Purpose
- The main purpose of a Real-world Repetitive Task is to
- Break a big Real-world Task into several (very similar) Sub-tasks and repeat a set of actions(s) over and over again on Sub-tasks to complete the big Real-world Task
- The main purpose of a Real-world Repetitive Task is to
- Definition
- Iteration
- Definition
- Iteration is defined as the repetition of a set of action(s) to produce an Outcome (or Result).
- Definition
- Types of Real-world Repetitive Tasks
- The Two Main Types of Real-world Repetitive Tasks are
- Real-world Repetitive Tasks with Fixed Number of Iterations
- Real-world Repetitive Tasks with Variable Number of Iterations
- The Two Main Types of Real-world Repetitive Tasks are
- Repetitive Programming Task
- A Repetitive Programming Task is defined as a Programming Task in which there is little variation from one item to the next item of the Programming Task
- Types of Repetitive Programming Task
- The Two Main Types of Repetitive Programming Tasks are
- Repetitive Programming Task with Fixed Number of Iterations
- Repetitive Programming Task with Variable Number of Iterations
- The Two Main Types of Repetitive Programming Tasks are
- Menu Driven Program
- Definition
- A Menu Driven Program is a Program which takes Input from a User by displaying a Menu (i.e., a List of Options (Choices)) and the User indicates his / her Choice form the given Menu
- Definition
- 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
- Definition
- 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
- In Python Programming, the Four Main Components of a Loop are
- while Loop
- Definition
- In Python Programming, a while loop is used to execute a Set of Python Statements (i.e., Perform a Repetitive Programming Task) if a Condition is True
- Purpose
- The main purpose of while Loop is to
- Perform Repetitive Programming Tasks
- The main purpose of while Loop is to
- for Loops
- 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
- Definition
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Functions in Python