👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial Series – Lesson 9: Conditional Statements in Python

Python Tutorial for Beginners (Lesson 9): Learn conditional statements in Python (if, elif, else), comparison operators, logical operators, and real-world examples with exercises.

Python Tutorial Series – Lesson 9: Conditional Statements in Python

Welcome back to the Python Tutorial Series for Beginners! 🎉

In Lesson 8, we learned about sets – collections of unique items and how to perform set operations.

Now in Lesson 9, we’ll explore conditional statements – the way Python makes decisions.

By the end of this lesson, you’ll understand:

  • Why we need conditions in programming
  • if, elif, and else statements
  • Comparison and logical operators
  • Nested conditions
  • Real-world use cases
  • Beginner exercises

🔹 What are Conditional Statements?

In real life, we often make decisions:

👉 “If it’s raining, I’ll take an umbrella. Otherwise, I won’t.”

In Python, we use conditional statements to let our programs decide what to do depending on certain conditions.


🔹 1. The if Statement

 
age = 18

if age >= 18:
   print("You are eligible to vote")

👉 If the condition is True, the block of code runs.


🔹 2. The if...else Statement

 
age = 16

if age >= 18:
   print("You are eligible to vote")
else:
   print("You are not eligible to vote")

🔹 3. The if...elif...else Statement

👉 Use when you have multiple conditions:

 
marks = 75

if marks >= 80:
   print("Grade: A")
elif marks >= 60:
   print("Grade: B")
elif marks >= 40:
   print("Grade: C")
else:
   print("Grade: F")

🔹 4. Comparison Operators

Conditions often use comparison operators:

OperatorMeaningExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater or equalx >= y
<=Less or equalx <= y

🔹 5. Logical Operators

Combine multiple conditions using logical operators:

 
age = 20
country = "Nigeria"

if age >= 18 and country == "Nigeria":
   print("You can vote in Nigeria")
   
if age < 18 or country != "Nigeria":
   print("You cannot vote here")
   
if not age < 18:
   print("You are an adult")

🔹 6. Nested Conditions

You can place one if inside another:

 
age = 25
citizen = True

if age >= 18:
   if citizen:
       print("You are eligible to vote")
   else:
       print("You must be a citizen to vote")
else:
   print("You are not eligible")

🔹 7. Real-World Use Cases

Login system check

 
username = "admin"
password = "1234"

if username == "admin" and password == "1234":
   print("Login successful")
else:
   print("Invalid credentials")

Discount eligibility

 
amount = 120

if amount >= 100:
   print("You get a discount!")
else:
   print("No discount available")

🔹 Exercises for Beginners

Try these in a new file (lesson9.py):

  1. Write a program that checks if a number is positive, negative, or zero.
  2. Write a program that asks for your age and prints whether you are a child, teenager, adult, or senior.
  3. Write a program to check if a student passed or failed (pass mark = 50).
  4. Write a program to check if a given year is a leap year.
  5. Challenge: Write a login system where the correct username is "user" and password is "pass123".

🎯 Recap

In this lesson, you learned:
✅ Conditional statements help programs make decisions
if, elif, and else control the flow of logic
✅ Comparison and logical operators make conditions powerful
✅ Nested conditions let you check multiple layers of logic
✅ Real-world examples include logins, discounts, and validations

Next up: Lesson 10 – Loops in Python 🔄 (where we repeat tasks efficiently).

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

Python, Python tutorials, Python Beginners series, Python Step-byStep
3 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy