[go: up one dir, main page]

0% found this document useful (0 votes)
18 views7 pages

24 Conditional Statements

The document explains conditional statements in Python, including 'if', 'elif', and 'else', with examples demonstrating their usage for decision-making in programming. It covers basic syntax, real-time use cases like login systems, nested conditions, and best practices for writing clear and logical conditions. Additionally, it introduces practical scenarios such as exam eligibility and promotional offers using logical operators.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

24 Conditional Statements

The document explains conditional statements in Python, including 'if', 'elif', and 'else', with examples demonstrating their usage for decision-making in programming. It covers basic syntax, real-time use cases like login systems, nested conditions, and best practices for writing clear and logical conditions. Additionally, it introduces practical scenarios such as exam eligibility and promotional offers using logical operators.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Gowtham SB

www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

🎬 Topic: Conditional Statements (if, elif, else)

✅ What Are Conditional Statements?

They help your program make decisions based on certain conditions.

💡 Like real life:


"If it's raining, take an umbrella. Else, wear sunglasses."

✅ Syntax in Python:

if condition:

# code if condition is True

elif another_condition:

# code if elif is True

else:

# code if none of the above is True

🔹 1. Basic if Statement

age = 20

if age >= 18:


Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

print("You are eligible to vote")

✅ it checks the condition and runs the code inside only if it's True.

🔹 2. if with else

age = 16

if age >= 18:

print("You are eligible to vote")

else:

print("You are not eligible to vote")

✅ else runs when the condition is False.

🔹 3. if, elif, else (Multiple Conditions)

marks = 75

if marks >= 90:

print("Grade A")

elif marks >= 70:

print("Grade B")
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

elif marks >= 50:

print("Grade C")

else:

print("Fail")

✅ Explain:

● Only one block is executed (the first True one).

● Once a condition matches, it skips the rest.

🔹 4. Real-Time Use Case: Login System

username = input("Enter username: ")

password = input("Enter password: ")

if username == "admin" and password == "1234":

print("Login successful!")

else:

print("Invalid credentials")

✅ Combine comparison and logical operators here.

🔹 5. Nested Conditions

age = 25
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

has_license = True

if age >= 18:


if has_license:
print("You can drive")
else:
print("You need a license to drive")
else:
print("You are too young to drive")

✅ Mention: Good for real-world validations, but avoid deep nesting.

✅ 1. Exam Eligibility Example (using and)


📘 Logic:

If marks >= 50 and attendance >= 75, student is eligible for the exam.

Code:

marks = 55
attendance = 80

if marks >= 50 and attendance >= 75:


print("Eligible for exam")
else:
print("Not eligible")

✅ 2. Mobile Recharge Offer (using or)


📘 Logic:
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

If recharge amount >= 399 or data balance >= 1GB, user gets bonus.

Code:

recharge_amount = 200
data_balance = 1.5 # in GB

if recharge_amount >= 399 or data_balance >= 1:


print("You are eligible for bonus data")
else:
print("No bonus data")

✅ 3. Restaurant Offer (using and + or)


📘 Logic:

If the bill is above ₹1000 and it's a weekend,


or the user is a Gold Member, they get 20% off.

Code:

bill = 1200
day = "Saturday"
membership = "Gold"

if (bill > 1000 and day in ["Saturday", "Sunday"]) or membership


== "Gold":
print("You get 20% discount")
else:
print("No discount")

🧠 Best Practices:
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

● Use elif instead of multiple if checks.

● Keep conditions readable and logical.

● Indentation matters! (Each block inside if/else should be indented)

✅ Quick Recap Table:


Stateme Description
nt

if Checks a condition

elif Checks another condition if the first is


False

else Runs if no condition is True

About the Author


Gowtham SB is a Data Engineering expert, educator, and content creator with a
passion for big data technologies, as well as cloud and Gen AI . With years of
experience in the field, he has worked extensively with cloud platforms, distributed
systems, and data pipelines, helping professionals and aspiring engineers master the
art of data engineering.

Beyond his technical expertise, Gowtham is a renowned mentor and speaker, sharing
his insights through engaging content on YouTube and LinkedIn. He has built one of
the largest Tamil Data Engineering communities, guiding thousands of learners to
excel in their careers.
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

Through his deep industry knowledge and hands-on approach, Gowtham continues to
bridge the gap between learning and real-world implementation, empowering
individuals to build scalable, high-performance data solutions.

𝐒𝐨𝐜𝐢𝐚𝐥𝐬

𝐘𝐨𝐮𝐓𝐮𝐛𝐞 - https://www.youtube.com/@dataengineeringvideos

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/dataengineeringtamil

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/thedatatech.in

𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐟𝐨𝐫 𝟏:𝟏 - https://topmate.io/dataengineering/

𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧 - https://www.linkedin.com/in/sbgowtham/

𝐖𝐞𝐛𝐬𝐢𝐭𝐞 - https://codewithgowtham.blogspot.com

𝐆𝐢𝐭𝐇𝐮𝐛 - http://github.com/Gowthamdataengineer

𝐖𝐡𝐚𝐭𝐬 𝐀𝐩𝐩 - https://lnkd.in/g5JrHw8q

𝐄𝐦𝐚𝐢𝐥 - atozknowledge.com@gmail.com

𝐀𝐥𝐥 𝐌𝐲 𝐒𝐨𝐜𝐢𝐚𝐥𝐬 - https://lnkd.in/gf8k3aCH

You might also like