Complete Python Programming Book | Interactive PDF
Interactive Python Programming Textbook
Module 1: Python Programming
Control Flow: Decisions and Loops
Last Updated: December 2024 Author:Dr. Deepak D. Shudhalwar, Professor PSSCIVE

Understanding Program Flow

In our daily lives, we encounter situations where we must follow fixed sequences of steps to complete tasks, while in others we have choices. Understanding these patterns is crucial for programming.

Airport Boarding Process

  1. Show ticket and identity proof at the main entrance of the Airport.
  2. Scanning of luggage at the security checkpoint.
  3. Take your boarding pass from the check-in counter.
  4. Pass security check including personal screening.
  5. Finally, board the plane at the designated gate.

This fixed sequence represents sequential flow in programming.

Note: Passengers can take their boarding pass from the airline's check-in counter OR they can also get a boarding pass at an electronic kiosk nearby. This represents conditional execution in programming.
Programming Analogy

Just like passengers have multiple ways to get boarding passes (counter OR kiosk), programs often need to make decisions:

  • IF the passenger chooses counter, THEN follow counter procedure
  • ELSE IF passenger chooses kiosk, THEN follow kiosk procedure
  • ELSE print error message

Try It Yourself: Python Control Flow

Practice the concepts you've learned by modifying the code below. This interactive editor simulates Python execution in your browser.

Python Code Editor
Output will appear here after running the code...
Page 1 of 6
Page 1
Interactive Python Programming Textbook
Module 1: Python Programming
Session 1: Control Structures in Python
Last Updated: December 2024 Author: Dr. Deepak D. Shudhalwar, Professor PSSCIVE

Understanding Control Structures

Control structures are the building blocks of programming that determine the flow of execution. They allow programs to make decisions, repeat actions, and handle different scenarios.

Types of Control Structures

  1. Sequential Structures: Code executes line by line
  2. Selection Structures: if, if...else, if...elif...else
  3. Iteration Structures: for loops, while loops
  4. Jump Structures: break, continue, return
Key Concept: Control structures allow your program to respond to different situations dynamically, making programs intelligent and adaptable.

The if Statement

The if statement is the most basic control structure. It allows your program to execute code only when a certain condition is true.

Syntax of if Statement
if condition:
    # Code to execute if condition is True
    statement1
    statement2
    # ... more statements

The indented block under if only executes when the condition evaluates to True.

Practice: if Statement
Output will appear here after running the code...

Comparison Operators

Comparison operators are used in conditions to compare values. They return either True or False.

Python Comparison Operators

== Equal to
5 == 5 → True
!= Not equal to
5 != 3 → True
> Greater than
5 > 3 → True
< Less than
3 < 5 → True
>= Greater than or equal
5 >= 5 → True
<= Less than or equal
3 <= 5 → True
Page 2 of 6
Page 2
Interactive Python Programming Textbook
Module 1: Python Programming
Example 1.1: Voting Eligibility Check
Last Updated: December 2024 Author:Dr. Deepak D. Shudhalwar, Professor PSSCIVE

Practical Example: Voting Eligibility

Let's examine a complete example to understand the control flow of the if statement. This program checks if a person is eligible to vote based on their age.

Voting Rule: In most countries, if the age is 18 or older, the person is eligible to vote.

Complete Python Program

Example 1.1: Voting Eligibility Program
Output will appear here after running the code...
Program Analysis
Age: 20
if 20 >= 18:
→ True ✓
ELIGIBLE
Age: 18
if 18 >= 18:
→ True ✓
ELIGIBLE
Age: 17
if 17 >= 18:
→ False ✗
NOT ELIGIBLE

Interactive Test

Try the program with different ages to see how the if statement works:

Test Voting Eligibility

Enter an age to check voting eligibility:

Try ages like: 16, 18, 20, 25

Page 3 of 6
Page 3
Interactive Python Programming Textbook
Module 1: Python Programming
1.1.2: if...else Statement
Last Updated: December 2024 Author: Dr. Deepak D. Shudhalwar, Professor PSSCIVE

Working with Alternative Paths

The if...else statement allows writing two alternative paths. The control condition determines which path gets executed.

Syntax of if...else Statement
if test_expression:
    # Body of if (executes when True)
    statement1
    statement2
else:
    # Body of else (executes when False)
    statement3
    statement4
How it works: The if...else statement evaluates the test expression and executes the body of if only when the condition is True. If the condition is False, the body of else is executed.

Example 1.2: Modified Voting Eligibility

Let's modify Example 1.1 to illustrate the use of if...else structure:

Example 1.2: if...else Voting Check
Output will appear here after running the code...

if vs if...else Comparison

if Statement Only

if age >= 18:
    print("Eligible")

When False: Nothing happens

if...else Statement

if age >= 18:
    print("Eligible")
else:
    print("Not Eligible")

When False: else block executes

Practice if...else Statements

Write and test your own if...else code:

Your Practice Editor
Your output will appear here...
Page 4 of 6
Page 4
Interactive Python Programming Textbook
Module 1: Python Programming
1.1.3: if...elif...else Statement
Last Updated: December 2024 Author:Dr. Deepak D. Shudhalwar, Professor PSSCIVE

Handling Multiple Conditions

When you have multiple independent conditions to check, you can use elif statements (short for "else if") between the if and else control structures.

Syntax of if...elif...else
if condition1:
    # Executes when condition1 is True
    statement1
elif condition2:
    # Executes when condition1 is False and condition2 is True
    statement2
elif condition3:
    # Executes when both condition1 and condition2 are False
    # and condition3 is True
    statement3
else:
    # Executes when all conditions are False
    statement4
Important Notes:
  • Only one block executes (the first True condition)
  • Conditions are checked from top to bottom
  • Once a True condition is found, the rest are skipped
  • The else block is optional

Interactive Grade Calculator

A practical example of if...elif...else with a grade calculator:

Calculate Your Grade

Enter your score (0-100):

Try scores like: 95, 85, 75, 65, 55

Grade Calculator Code
Output will appear here after running the code...

Practice if...elif...else

Your Practice Editor
Your output will appear here...
Page 5 of 6
Page 5
Interactive Python Programming Textbook
Module 1: Python Programming
Advanced: Multiple elif Blocks
Last Updated: December 2024 Author:Dr. Deepak D. Shudhalwar, Professor PSSCIVE

Advanced if...elif...else Structure

You can have multiple elif blocks to handle many independent conditions. Among the several if...elif...else blocks, only one block is executed according to the condition.

Key Concepts

Only One Block Executes

First True condition wins, rest are skipped

Order Matters

Conditions checked from top to bottom

Multiple elif Blocks

You can have as many elif as needed

Single else Block

Only one else allowed per if statement

Example 1.3: Age Category Classifier

Let's create a comprehensive age classifier with multiple elif blocks:

Example 1.3: Age Category Classifier
Output will appear here after running the code...
Output Comparison
Age: 8
if age < 13:
→ True ✓
Child
Age: 16
elif age < 20:
→ True ✓
Teenager
Age: 45
elif age < 60:
→ True ✓
Middle-aged

Practice Multiple elif Blocks

Your Practice Editor
Your output will appear here...
Page 6 of 6
Page 6