Skip to content

Latest commit

 

History

History
217 lines (156 loc) · 4.07 KB

File metadata and controls

217 lines (156 loc) · 4.07 KB

<< Day 2 | Day 4 >>

📘 Day 3: If-Else and Loops in Python

Welcome to Day 3 of the 30 Days of Data Science series! Today, we will cover essential programming constructs—If-Else Statements and Loops—which are fundamental for controlling the flow of your Python programs. Let’s dive in!

Table of Contents

1️⃣ If-Else Statements 🧠

Syntax

if condition:
    # Code block executed if the condition is True
else:
    # Code block executed if the condition is False

Example: Simple If-Else Statement

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

Output:

You are an adult!

Example: Nested If-Else

age = 16
if age >= 18:
    print("You can vote!")
else:
    if age >= 16:
        print("You are a teenager!")
    else:
        print("You are a child!")

Output:

You are a teenager!

Example: If-Elif-Else

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

Output:

Grade: B

2️⃣ Loops 🔁

Loops allow repetitive tasks to be performed efficiently.

For Loop

The for loop iterates over a sequence (like a list, tuple, or string).

Syntax

for item in sequence:
    # Code block to execute for each item

Example: Using a For Loop

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)

Output:

1
2
3
4
5

While Loop

The while loop executes a block of code as long as a condition is True.

Syntax

while condition:
    # Code block to execute

Example: Using a While Loop

count = 0
while count < 5:
    print(count)
    count += 1

Output:

0
1
2
3
4

Break and Continue

  • Break: Terminates the loop prematurely.
  • Continue: Skips the current iteration and moves to the next.

Example: Break and Continue

for num in range(1, 6):
    if num == 3:
        break  # Exit loop when num is 3
    print(num)

Output:

1
2
for num in range(1, 6):
    if num == 3:
        continue  # Skip iteration when num is 3
    print(num)

Output:

1
2
4
5

🧠 Practice Exercises

If-Else Statements

  1. Write a program that checks if a number is positive, negative, or zero.
  2. Create a grade classifier using the if-elif-else structure.

Loops

  1. Write a program that prints all even numbers from 1 to 50 using a for loop.
  2. Create a program that sums the numbers from 1 to 100 using a while loop.
  3. Use break and continue in a loop to demonstrate their functionality.

🌟 Summary

  • If-Else Statements allow you to make decisions in your code.
  • Loops enable you to automate repetitive tasks efficiently.
  • Break and Continue give more control over loop execution.