GK SOLUTIONS
AI • IoT • Arduino • Projects & Tutorials
DEFEAT THE FEAR
← Back to All Lessons

🔄 Lesson 5: Loops (Repetition)

⏱️ Estimated time: 25 minutes | Difficulty: Beginner

Automation

Loops allow you to run the same code multiple times, which is essential for processing lists of data or performing repetitive calculations.

What is an If Statement?

An if statement lets your program make decisions. It runs different code based on conditions.

Real-World Analogy:

🌧️ If it's raining: Take an umbrella
☀️ Else (if it's not raining): Don't take an umbrella

Basic If Statement

age = 18

if age >= 18:
    print("You can vote!")
    print("Welcome to voting!")

Output:

You can vote!
Welcome to voting!

Comparison Operators

These are used in if statements to compare values:

== Equal

if name == "Alice":
    print("Hi Alice!")

!= Not Equal

if name != "Bob":
    print("You're not Bob!")

> Greater

if score > 50:
    print("You passed!")

< Less

if age < 13:
    print("You're a kid!")

>= Greater or Equal

if score >= 90:
    print("Grade: A")

<= Less or Equal

if age <= 12:
    print("Child price")

If/Else Statement

age = 15

if age >= 18:
    print("You can vote!")
else:
    print("You can't vote yet.")

Output:

You can't vote yet.

If/Elif/Else (Multiple Conditions)

score = 85

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

Output:

Grade: B

Combining Conditions with AND/OR

age = 25
has_license = True

# AND: Both must be true
if age >= 18 and has_license:
    print("You can drive!")

# OR: At least one must be true
if age < 13 or age > 65:
    print("Special pricing available")

✅ Quick Quiz

❓ What will this print?
x = 10
if x > 5:
print("Big")

❓ What operator checks "not equal"?

❓ When do you use "elif"?

🎯 Challenge

Write a program that:

  1. Asks the user for their age
  2. Uses if/elif/else to print:
    - "Too young" if under 13
    - "Teenager" if 13-19
    - "Adult" if 20 or older

Hint: Use input() to get the user's age

← Previous: Math & Operations Next: Loops →