If you're new to programming or brushing up for technical assessments, mastering if-else statements is a must. That's why Uncodemy’s Python programming course prioritizes practical exercises and real-world projects centered on Python's conditional logic. In this comprehensive and beginner-friendly article, you'll not only understand the syntax and philosophy behind if-else but also practice with a range of clear code samples and explanations. Each example is designed to improve your logical thinking, boost interview readiness, and set a solid programming foundation.


Conditional statements form the bedrock of every meaningful program. They dictate logic, create branches in your code, and empower your software to make decisions. In Python, the if-else construct is the primary tool for implementing conditional logic, testing scenarios, and controlling the flow of execution.
Imagine you’re instructing a robot to serve coffee. You might say: “If the cup is empty, pour more coffee. Otherwise, do nothing.” This “if-else” thinking is at the core of how computers mimic human decision-making.
Uncodemy’s Python programming course is built around such real scenarios, demonstrating that conditional logic isn’t abstract—it’s a powerful, everyday skill.
Python’s if-else syntax is designed for simplicity and readability:
python
if condition:
# code block for True
else:
# code block for False
Python requires indentation to mark the body of each block—typically four spaces or one tab.
python
temperature = 25
if temperature > 20:
print("It's warm today.")
Output:
text
It's warm today.
Here, since 25 > 20, the message prints. If the temperature were 18, nothing would be output.
python
age = 16
if age >= 18:
print("Eligible to vote.")
else:
print("Not eligible to vote.")
Output:
text
Not eligible to vote.
If the condition in the if is false, Python runs the else block.
Sometimes, you want more than a simple if-or-else. Enter elif (short for "else if"):
python
score = 78
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Grade D")
Output:
text
Grade B
Python checks each condition in order and executes the first true block.
You can place an if (or else) block inside another for more complex checks.
python
num = 7
if num > 0:
if num % 2 == 0:
print("Positive even number")
else:
print("Positive odd number")
else:
print("Negative number")
Output:
text
Positive odd number
This is useful for multi-level decision-making, but for readability, it’s often better to use elif or logical operators when possible.
Combine multiple conditions using and, or, and not:
python
year = 2024
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Output:
text
Leap year
The logical operators allow for testing compound scenarios and are frequently covered in Uncodemy’s Python programming course projects and assignments.
python
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
Sample Output:
text
Enter a number: 5
Odd
python
value = float(input("Number: "))
if value > 0:
print("Positive")
elif value == 0:
print("Zero")
else:
print("Negative")
Sample Output:
text
Number: -3
Negative
python
password = input("Password: ")
if password == "Python123":
print("Access granted")
else:
print("Access denied")
Sample Output:
text
Password: Hello
Access denied
python
n = int(input("Enter an integer: "))
if n < 0:
abs_val = -n
else:
abs_val = n
print("Absolute value:", abs_val)
Sample Output:
text
Enter an integer: -10
Absolute value: 10
You can use if-else to test:
String Example:
python
city = "Mumbai"
if city.lower() == "delhi":
print("Capital of India")
else:
print("Other city")
Output:
text
Other city
Python offers a concise one-line version of if-else for simple value selection.
python
result = "Even" if number % 2 == 0 else "Odd"
print(result)
This style is examined in intermediate challenges and interviews covered in Uncodemy’s course.
python
password = input("Enter password: ")
if len(password) < 8:
print("Weak password")
else:
print("Strong password")
python
total = int(input("Purchase amount: "))
if total >= 1000:
print("You get a 10% discount!")
else:
print("No discount available")
python
age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif age < 18:
print("Teenager")
elif age <= 60: print("adult") else: print("senior citizen")
=>The if-else construct in Python is much more than a beginner's milestone—it's the logic engine behind every decision a program makes. Mastering it lays the groundwork for solving bigger, more complex problems, and helps you think algorithmically “like a computer.” Uncodemy’s Python programming course weaves if-else mastery into every topic: from user interfaces, web apps, and APIs to data science and process automation.
Practice often, imagine everyday logic as if-else flows, and seek out small challenges to apply your new knowledge. Over time, you’ll find that conditionals are second nature, powering elegant, bug-free programs and readying you for technical interviews and advanced projects alike.
Q1: Is “elif” required, or can I just use multiple if-else statements?
Elif keeps code cleaner and more efficient; otherwise, multiple if-else can create bugs and redundant checks.
Q2: Does if-else work with all data types?
Yes, as long as the condition resolves to a Boolean (True/False).
Q3: What happens if no else or elif is provided?
If the if condition is not met and there’s no else, the program simply continues—no code in that block runs.
Q4: Is indentation optional?
No. Python’s syntax depends on indentation to determine which statements belong inside each block.
Q5: Will I use if-else a lot in real programming?
Absolutely. Conditionals are present in almost every real application—from automation scripts to data analytics and web apps.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR