Skip to content

Python Programming! Conditional Statements Lectures 5

A cartoon character a kid or a robot

Lets Start Python Programming For Beginners to advanced. Conditional Statements, Lectures 5

๐Ÿ Lecture 5: Conditional Statements

๐ŸŽฏ Goal

By the end of this lecture, you will:

  • Understand how to use if, elif, and else statements.
  • Be able to make decisions in your code based on user input or values.
  • Build simple decision-based games or tools.
  • Learn how indentation works in Python.

๐Ÿง  What Are Conditional Statements?

Conditional statements are like asking a question in your code:

“If this is true, do that. Otherwise, do something else.”

They help your program make decisions โ€” just like you do every day!

[adrotate banner=”6″]

๐Ÿ” The if Statement โ€“ Making Choices

The simplest conditional statement is if.

Syntax:
if condition:
    # Do something if condition is True
Example:
age = 10

if age >= 13:
    print("You can join the club!")

๐Ÿ‘‰ Output:
Nothing happens because 10 is less than 13.

Letโ€™s try again with a higher age:

age = 14

if age >= 13:
    print("You can join the club!")

๐Ÿ‘‰ Output:

You can join the club!
โŒ The else Statement โ€“ If Not, Then…

If the condition is not true, we can use else to say what should happen.

age = 10

if age >= 13:
    print("You can join the club!")
else:
    print("Sorry, youโ€™re too young.")

๐Ÿ‘‰ Output:

Sorry, youโ€™re too young.
๐Ÿ”„ The elif Statement โ€“ More Choices

Use elif (short for โ€œelse ifโ€) to check more conditions.

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: D or lower")

๐Ÿ‘‰ Output:

Grade: B
๐Ÿ’ก Indentation Matters!

In Python, indentation (spaces at the start) tells Python which lines belong to the if, elif, or else.

โœ… Correct:

if age > 10:
    print("You are older than 10")
    print("Thatโ€™s cool!")

โŒ Wrong:

if age > 10:
print("You are older than 10")  # โŒ No indent!

Python will show an error here!

A cartoon character a kid or a robot
A cartoon character a kid or a robot

๐Ÿงช Try It Yourself!

Write a program that asks the user for their favorite animal and gives a fun response.

Example:

animal = input("What is your favorite animal? ")

if animal == "dog":
    print("Woof! Dogs are awesome friends.")
elif animal == "cat":
    print("Meow! Cats are super cute.")
elif animal == "dolphin":
    print("Splash! Dolphins are smart and playful.")
else:
    print("Cool choice! Thatโ€™s a unique animal.")

๐ŸŽฎ Mini Project: Rock, Paper, Scissors Game

[adrotate banner=”6″]

Letโ€™s build a simple version where the player vs computer chooses rock, paper, or scissors.

Weโ€™ll keep it simple for now โ€” full game next time!

player_choice = input("Choose rock, paper, or scissors: ")
computer_choice = "rock"  # For now, computer always picks rock

if player_choice == computer_choice:
    print("Itโ€™s a tie!")
elif player_choice == "paper":
    print("You win! Paper beats rock.")
else:
    print("Computer wins! Rock beats scissors.")

๐Ÿง’ Kids Corner

๐Ÿง  Imagine you’re choosing what to wear based on the weather:

  • If itโ€™s raining โ†’ Wear a raincoat.
  • Else if itโ€™s sunny โ†’ Wear shorts.
  • Else โ†’ Stay cozy inside.

Thatโ€™s exactly what if, elif, and else do in code โ€” help your program make choices!

[adrotate banner=”6″]

๐Ÿš€ Challenge (Optional)

Make a Magic Door Adventure game where the user picks a door (1โ€“3), and each door has a surprise.

Example:

door = int(input("Choose a door (1, 2, or 3): "))

if door == 1:
    print("A dragon appears! Run!")
elif door == 2:
    print("You found a treasure chest!")
elif door == 3:
    print("Oops! You fell into a trap.")
else:
    print("Invalid choice! Pick 1โ€“3.")

๐Ÿ“Œ Summary

  • Use if to check a condition.
  • Use else for what to do if the condition is false.
  • Use elif to check more conditions.
  • Indentation is very important in Python.
  • Conditional statements let your programs make decisions like humans.
๐Ÿ”น Call Of Action

If this article was helpful to you, please Share and leave your feedback in the comment section below.

Python Programming For Beginners To Advanced

Python Programming For Beginners! Lecture 1

Python Programming! Variables and Data Types | Lectures 2

Python Programming! Input And Output | Lectures 3

Python Programming! Operators in Python Lectures 4

Python Programming! Conditional Statements Lectures 5

[adrotate banner=”6″]

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

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