Language

Control Flow · Lesson 5 of 56

Conditionalstatements

Source: 2-Control Flow/Conditionalstatements.ipynb

Start here — no coding background needed

What you will learn

Make the computer choose different actions based on a condition.

In simple words

`if` runs code only when a test is true. `else` is the backup when it is false. Indentation (spaces) shows what belongs inside.

Think of it like this

If rain → take umbrella, else → take sunglasses.

Words to know:

  • if / else — Two paths — one runs when test is true, other when false
  • Indent — Spaces at start of line — shows code inside if/else

Learn how programs make decisions and repeat tasks — like daily habits.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Conditional Statements (if, elif, else)

Video Outline:

  1. Introduction to Conditional Statements
  2. if Statement
  3. else Statement
  4. elif Statement
  5. Nested Conditional Statements
  6. Practical Examples
  7. Common Errors and Best Practices
Reference example
Python
Output
Expected (from notebook):
You are allowed to vote in the elections

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
True

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
You are a minor

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
You are a teenager

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Example HCL
HCL
## Nested Condiitonal Statements

# You can place one or more if, elif, or else statements inside another if, elif, or else statement to create nested conditional statements.

## number even ,odd,negative

num=int(input("Enter the number"))

if num>0:
    print("The number is positive")
    if num%2==0:
        print("The number is even")
    else:
        print("The number is odd")

else:
    print("The number is zero or negative")

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
## Practical Examples

## Determine if a year is a leap year using nested condition statement

year=int(input("Enter the year"))

if year%4==0:
    if year%100==0:
        if year%400==0:
            print(year,"is a leap year")
        else:
            print(year,"is not a leap year")
    else:
        print(year,"is a leap year")

else:
    print(year,"is not a leap year")


Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
## Assignment
## Simple Calculator program
# Take user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")

# Perform the requested operation
if operation == '+':
    result = num1 + num2
elif operation == '-':
    result = num1 - num2
elif operation == '*':
    result = num1 * num2
elif operation == '/':
    if num2 != 0:
        result = num1 / num2
    else:
        result = "Error! Division by zero."
else:
    result = "Invalid operation."

print("Result:", result)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
### Determine the ticket price based on age and whether the person is a student.
# Ticket pricing based on age and student status

# Take user input
age = int(input("Enter your age: "))
is_student = input("Are you a student? (yes/no): ").lower()

# Determine ticket price
if age < 5:
    price = "Free"
elif age <= 12:
    price = "$10"
elif age <= 17:
    if is_student == 'yes':
        price = "$12"
    else:
        price = "$15"
elif age <= 64:
    if is_student == 'yes':
        price = "$18"
    else:
        price = "$25"
else:
    price = "$20"

print("Ticket Price:", price)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Complex Example 3: Employee Bonus Calculation

Calculate an employee's bonus based on their performance rating and years of service.

Example HCL
HCL
# Employee bonus calculation

# Take user input
years_of_service = int(input("Enter years of service: "))
performance_rating = float(input("Enter performance rating (1.0 to 5.0): "))

# Determine bonus percentage
if performance_rating >= 4.5:
    if years_of_service > 10:
        bonus_percentage = 20
    elif years_of_service > 5:
        bonus_percentage = 15
    else:
        bonus_percentage = 10
elif performance_rating >= 3.5:
    if years_of_service > 10:
        bonus_percentage = 15
    elif years_of_service > 5:
        bonus_percentage = 10
    else:
        bonus_percentage = 5
else:
    bonus_percentage = 0

# Calculate bonus amount
salary = float(input("Enter current salary: "))
bonus_amount = salary * bonus_percentage / 100

print("Bonus Amount: ${:.2f}".format(bonus_amount))

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Complex Example 4: User Login System

A simple user login system that checks the username and password.

Example HCL
HCL
# User login system

# Predefined username and password
stored_username = "admin"
stored_password = "password123"

# Take user input
username = input("Enter username: ")
password = input("Enter password: ")

# Check login credentials
if username == stored_username:
    if password == stored_password:
        print("Login successful!")
    else:
        print("Incorrect password.")
else:
    print("Username not found.")

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

marks = 55. If marks >= 50 print Pass, else print Fail.

Python