Python provides many ways to make your code clean, concise, and more readable. One of the most powerful techniques is using the short-hand if-else statement, also known as the ternary conditional operator. In this article, we will dive into several Python short-hand if-else exercises with complete examples and explanations. This guide is designed for beginners and intermediate programmers who want to write more compact code while maintaining clarity.
What is Python Short-Hand If-Else?
The short-hand if-else allows you to write conditional statements in a single line. Instead of writing multiple lines of code, you can use the short-hand form to make decisions quickly.
The syntax is simple:
value_if_true if condition else value_if_false
It returns value_if_true when the condition is true, otherwise it returns value_if_false.
Why Use Short-Hand If-Else in Python?
- Cleaner code: Reduces the number of lines in your program.
- Better readability: Easier to follow logic when used properly.
- Efficient for inline operations: Perfect for variable assignments and simple decisions.
Python Short-Hand If-Else Exercises
Exercise 1: Check if a number is positive or negative
num = -5 result = "Positive" if num > 0 else "Negative" print(result)
Output: Negative
Here, the condition num > 0
is checked. Since -5 is not greater than 0, it returns “Negative”.
Exercise 2: Determine if a number is even or odd
num = 12 result = "Even" if num % 2 == 0 else "Odd" print(result)
Output: Even
Exercise 3: Find the maximum of two numbers
a, b = 10, 20 maximum = a if a > b else b print("Maximum:", maximum)
Output: Maximum: 20
Exercise 4: Check eligibility to vote
age = 16 status = "Eligible" if age >= 18 else "Not Eligible" print("Voting Status:", status)
Output: Voting Status: Not Eligible
Exercise 5: Assign grade based on score
score = 85 grade = "Pass" if score >= 50 else "Fail" print("Result:", grade)
Output: Result: Pass
Exercise 6: Short-hand if without else
x = 10 if x > 5: print("x is greater than 5")
Output: x is greater than 5
Exercise 7: Multiple conditions with short-hand if-else
marks = 78 result = "Excellent" if marks > 80 else "Good" if marks > 60 else "Needs Improvement" print("Performance:", result)
Output: Performance: Good
Exercise 8: Short-hand If-Else inside a function
def check_temperature(temp): return "Hot" if temp > 30 else "Cold" print(check_temperature(35)) print(check_temperature(20))
Output:
Hot
Cold
Exercise 9: Using short-hand if-else in list comprehension
numbers = [1, 2, 3, 4, 5] result = ["Even" if num % 2 == 0 else "Odd" for num in numbers] print(result)
Output: [‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’]
Exercise 10: Short-hand If-Else with strings
username = "" greeting = f"Hello, {username}" if username else "Hello, Guest" print(greeting)
Output: Hello, Guest
Table: Short-Hand If-Else vs Traditional If-Else
Traditional If-Else | Short-Hand If-Else |
---|---|
if num > 0: result = "Positive" else: result = "Negative" |
result = "Positive" if num > 0 else "Negative" |
Best Practices for Using Short-Hand If-Else
- Use it only for simple conditions. For complex logic, stick with traditional if-else for better readability.
- Ensure that the condition is clear and unambiguous.
- Do not overuse it in deeply nested conditions.
Conclusion
The Python short-hand if-else is a simple yet powerful feature to make your code concise. By practicing the exercises above, you will become more comfortable using this syntax in real-world projects. Always balance between compact code and readability to write clean, maintainable Python programs.
For more details, check out the official Python Documentation.