Skip to main content
  1. Notes/
  2. Python ๐Ÿ/

๐Ÿงญ Conditional Statements in Python | Lesson 08

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 8: This Article

๐Ÿงญ Conditional Statements in Python
#

Conditional statements make your program decide what to do based on different situations. They let your code be smart and choose a path depending on conditionsโ€”like a traffic light for your programโ€™s flow! ๐Ÿšฆ


What Are Conditional Statements? ๐Ÿค”
#

They allow you to execute certain blocks of code only if a specific condition is true.


1. The if Statement โœ…
#

Basic way to run code when a condition is true.

age = 18
if age >= 18:
    print("You are an adult.")
  • The code inside the if block runs only if age >= 18 is true.

2. The if-else Statement ๐Ÿ”„
#

Choose between two paths.

age = 16
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

3. The if-elif-else Ladder ๐Ÿชœ
#

Check multiple conditions one by one.

marks = 75
if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 50:
    print("Grade: C")
else:
    print("Grade: F")

Important Tips for Conditionals
#

  • Conditions return True or False.
  • Indentation after if, elif, and else is mandatory.
  • Use comparison operators (==, !=, <, >, <=, >=) in conditions.

Practice Questions with Solutions ๐Ÿ†
#

  1. Practice: Write a program to check if a number is positive or negative.
    • Solution:
num = int(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")
  1. Practice: Check if a person is eligible to vote (age 18 or more).
    • Solution:
age = int(input("Enter your age: "))
if age >= 18:
    print("Eligible to vote!")
else:
    print("Not eligible yet.")

Step-by-Step Challenge: Simple Calculator ๐Ÿงฎ
#

Write a program that asks the user to enter two numbers and an operator (+, -, *, /) and then performs the operation.

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")

if op == "+":
    print(num1 + num2)
elif op == "-":
    print(num1 - num2)
elif op == "*":
    print(num1 * num2)
elif op == "/":
    if num2 != 0:
        print(num1 / num2)
    else:
        print("Error! Division by zero.")
else:
    print("Invalid operator.")

Checklist for This Chapter โœ…
#

  • Used if for simple decisions
  • Used if-else for two outcomes
  • Used if-elif-else for multiple choices
  • Handled conditions with comparison operators

Youโ€™re now ready to control the flow of your programs using decisions! Start building smart, interactive apps! ๐Ÿšฆ๐ŸŽ‰

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 8: This Article

Related

โฐ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
๐ŸŽฌ Strings in Python | Lesson 04
Python Progmramming Course
๐Ÿ—๏ธ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
๐Ÿ‘ถ Python Basics | Lesson 02
Python Progmramming Course
๐Ÿ“„ File Handling in Python | Lesson 14
Python Progmramming Course
๐Ÿ“‹ Lists in Python | Lesson 05
Python Progmramming Course