Wednesday, 8 April 2026

April Python Bootcamp Day 5

 



Day 5: Conditional Statements in Python

Making Decisions in Your Code 


 Introduction

In real life, we make decisions all the time:

  • If it rains → take an umbrella
  • If marks ≥ 90 → Grade A
  • If balance is low → show warning

Similarly, in programming, we use conditional statements to control the flow of execution.


 What are Conditional Statements?

Conditional statements allow a program to make decisions based on conditions.

They help programs:

  • Execute different blocks of code
  • Respond dynamically to input
  • Implement logic like real-world systems

 Core Idea

# if condition is True -> run code
# else -> skip or run something else

 1. if Statement

 Syntax

if condition:
# code block

 Example

age = 18

if age >= 18:
print("You can vote")

 Runs only when condition is True


 2. if-else Statement

Syntax

if condition:
# True block
else:
# False block

 Example

num = 5

if num % 2 == 0:
print("Even")
else:
print("Odd")

 3. if-elif-else Statement

 Used when multiple conditions exist

 Syntax

if condition1:
# block1
elif condition2:
# block2
else:
# default block

 Example

marks = 94

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

 Important Rule

 Only ONE block executes
 First True condition wins


 4. Nested Conditions

 Condition inside another condition

 Example

age = 20
has_id = True

if age >= 18:
if has_id:
print("Entry Allowed")
else:
print("ID required")
else:
print("Underage")

 Important Concepts (Must Understand)

 Truthy & Falsy Values

if 0:
print("Hello")
else:
print("World")

 Output: World


 Truth Table

  • 0, None, False, "" → Falsy
  • Everything else → Truthy

 Order Matters

marks = 90

if marks >= 50:
print("Pass")
elif marks >= 90:
print("Topper")

 Output: Pass (because first condition matched)



 Practice Problems

 Basic Level
  1. Check whether a number is positive or negative.
  1. Check whether a number is even or odd.
  1. Find the greater number between two numbers.

 Intermediate Level
  1. Find the greatest among three numbers.
  1. Check whether a given year is a leap year.
  1. Create a grade system based on marks:
    • 90 and above → Grade A
    • 75 to 89 → Grade B
    • 50 to 74 → Grade C
    • Below 50 → Fail

 Advanced Level
  1. Check whether a given number is a palindrome.
  1. Build logic for an ATM withdrawal system:
    • Check if balance is sufficient
    • Check if amount is a multiple of 100
  1. Create a login system:
    • Validate username and password
    • Show success or error message

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (237) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (3) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (21) data management (15) Data Science (339) Data Strucures (16) Deep Learning (144) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (277) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1286) Python Coding Challenge (1124) Python Mistakes (50) Python Quiz (465) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)