Algorithms and Programming Python 3
Control Structure I
This is a paradigm for directing the flow of a program in a specific direction. To master the material on control structures in Python programming, there are several key points that need to be understood.
Control structures are divided into two categories:
- Branching Structure
- Looping Structure
Conditional Structure (Conditional Statement)
Branching Constructs & Program Block
A branching construct is a program that, when executed, creates branches into sub-branches containing a program block based on the specified conditions and logic. In Python, branching constructs are generally created using the keywords if, elif, and else. Here’s a table illustrating this:
Information | Keyword |
---|---|
There is 1 decision option | if |
There is 2 decision option | if/else |
There are more than 2 decision options | if/elif/else |
A program block contains a set of expressions and statements for the computer to execute. In Python, a program block is identified by a colon (“:”) followed by an indentation after the declaration of constructs like if, elif, else, for, while, or during function definitions. The program block under the if condition will execute if the condition evaluates to True.
The program block under the elif condition (which stands for “else if”) will execute if the previous conditions do not hold true, and this condition can evaluate to true.
The program block under the else condition will execute when the previous conditions (if/elif) evaluate to False.
Here’s a simple example of a program using branching constructs with comparison operators:
Code Editor:1
2
3
4
5
6
7credits = 45
if (credits >= 120): # Entering IF Block Program
print("Senior") # Inside IF Block Program
elif (credits >= 90): # Entering ELIF Block Program
print("Junior") # Inside ELIF Block Program
else: # Entering ELSE Block Program
print("New College Student") # Inside ELSE Block Program
Output:New College Student
Nested Conditional
Nested conditional refers to the concept or paradigm where one branching construct exists within another branching construct in a program. Nested branching is commonly found in more complex or intricate code. Here’s an example of a nested conditional:
Code Editor:1
2
3
4
5
6
7
8
9
10
11
12
13credits = 125
if (credits >= 120): # Entering IF Block Program
if (credits > 120): # Entering the IF Block Program Nested within the IF Block Program
print("Old Senior") # Inside Nested IF Block Program
else: # Entering the ELSE Block Program Nested within the IF Block Program
print("New Senior") # Inside Nested ELSE Block Program
elif (credits >= 90): # Entering ELIF Block Program
if (credits > 90): # Entering the IF Block Program Nested within the ELIF Block Program
print("Old Junior") # Inside Nested IF Block Program
else: # Entering the ELSE Block Program Nested within the ELIF Block Program
print("New Junior") # Inside Nested ELSE Block Program
else: # Entering ELSE Block Program
print("New College Student") # Inside Else Block Program
Output:Old Senior
Complex Logical Conditional Statement
A complex logical conditional statement is a branching paradigm that incorporates specific logic to achieve a more refined flow in the code. Generally, to create a more targeted flow, programmers tend to prefer complex logical conditional statements over nested conditionals to keep the code concise and facilitate easier maintenance in the future. To create a complex logical conditional statement, you need to add logical operators within the if/elif statements, as illustrated in the following example:
Code Editor:1
2
3
4
5
6
7
8
9
10
11credits = 95
if ( ( credits > 122 ) and ( credits <= 144 ) ):
print("Older Senior")
elif ( ( credits >= 120 ) and ( credits <= 122 ) ):
print("Younger Senior")
elif ( ( credits > 90 ) and ( credits < 120 ) ):
print("Older Junior")
elif ( ( credits <= 90 ) and ( credits > 24 ) ):
print("Younger Junior")
else:
print("New College Student")
Output:Older Junior
Exercise
Work on the Following Exercises to Test Your Understanding of Control Structures.
Exercise 1
Create a program to determine if a number is odd or even based on user input.
Clue: Use Arithmetic Operators
INPUT:
Any number
PROCESS:
Filter whether the number is odd or even
OUTPUT:
Display the result as odd/even
Exercise 2
Create a program to categorize the age input provided by the user (age input cannot be negative).
Baby = 0 - 1
Toddler = 2 - 3
Preschooler = 4 - 5
Child = 6 - 12
Teenager = 13 - 17
Young Adult = 18 - 21
Pre-adult = 22 - 30
Adult = 31 - 50
Pre-elderly = 51 - 70
Elderly = 71 and above
INPUT:
Age
PROCESS:
Filter to determine the age category
OUTPUT:
Category resulting from the filter
Exercise 3
Create a program to perform temperature conversions:
- Celsius to Fahrenheit
- Celsius to Kelvin
- Fahrenheit to Celsius
- Fahrenheit to Kelvin
- Kelvin to Celsius
- Kelvin to Fahrenheit
Search for conversion formulas in online site, then allow the user to choose one of the six options and display the conversion result.
INPUT:
- Temperature (Degrees Celsius/Fahrenheit/Kelvin)
- Selected menu option
PROCESS:
Perform the following temperature conversions:
- Celsius to Fahrenheit
- Celsius to Kelvin
- Fahrenheit to Celsius
- Fahrenheit to Kelvin
- Kelvin to Celsius
- Kelvin to Fahrenheit
OUTPUT:
Temperature (Degrees Celsius/Fahrenheit/Kelvin)
Exercise 4
Create a program to determine the type of triangle based on the lengths of its three sides, which are input by the user. After the user finishes inputting, the program should print what type of triangle it is.
- Equilateral
- Isosceles
- Not a Triangle
- Scalene
- Right-angled (Clue: using the angle) —> bonus points
Be careful in determining the conditions, as entering 1, 2, and 3 as the sides will not form a triangle.
INPUT:
- Side A
- Side B
- Side C
PROCESS:
Filter the type of triangle based on the three sides provided
OUTPUT:
Display the type of triangle (equilateral/isosceles/not a triangle/scalene/right-angled)
Exercise 5
Create a branching program to determine the solutions of a quadratic equation using the discriminant.
- If the discriminant is greater than 0, it has two distinct roots.
- If the discriminant is less than 0, it has imaginary roots.
- If the discriminant equals 0, it has a double root.
Be careful if a=0, as this does not represent a quadratic equation.
INPUT:
- Value A
- Value B
- Value C
PROCESS:
Filter the value of A to check if it is 0. If it is not, calculate the discriminant using the quadratic formula. Use the result to filter the type of roots (distinct roots/imaginary roots/double root).
OUTPUT:
If there are distinct roots:
- Print “It has distinct roots”
- Print the quadratic equation
- Print the discriminant value
- Print the value of root x1
- Print the value of root x2
If there are imaginary roots:
- Print “It has imaginary roots”
- Print the quadratic equation
- Print the discriminant value
- Print the formulas for roots x1 and x2
If there is a double root:
- Print “It has a double root”
- Print the quadratic equation
- Print the discriminant value
- Print the root value (use either root x1 or x2)
If a=0:
- Print “It is not a quadratic equation”