In Python programming, conditionals play a crucial role in decision-making processes. The most commonly used conditional structure is the “if else” statement. Let’s delve into some examples to comprehend how conditionals are implemented.
Understanding Even and Odd Numbers
Let’s start by creating a simple conditional to determine whether a given number is even or odd.
i = 5
Regular if else
if i % 2 == 0:
print(“even”)
else:
print(“odd”)Ternary Operator (one liner)
print(“even” if i % 2 == 0 else “odd”)
Here, we’re checking whether the number i
is even or odd using both the traditional if-else
approach and the concise ternary operator.
Handling Special Cases
Next, let’s refine this condition to include scenarios where the number could be 0.
i = int(input("Enter an integer: "))
if i == 0:
print(f"i is 0")
elif i % 2 == 0:
print(f"{i} is even")
else:
print(f"{i} is odd")
In this case, we’re checking if the number is zero, even, or odd, and printing the appropriate message accordingly.
Dealing with Special Objects
It’s important to be able to handle special objects like None
.
n = None
print(‘Not None’) if n else print(n)
In this snippet, we’re demonstrating a simple conditional check against the None
object.
Categorizing Age Groups
Let’s explore a more practical example by categorizing age groups based on months.
age = int(input('Enter age in months: '))
if age <= 6:
print(‘New Born or Infant’)
elif 7 <= age <= 18:
print(‘Toddler’)
elif 19 <= age <= 144:
print(‘Grown up’)
elif 145 <= age <= 216:
print(‘Youth’)
else:
print(‘Adult’)
Here, we’re determining the appropriate age category based on the input provided.
Additional Tasks
We can further extend conditionals to check for specific conditions like evenness and divisibility.
# Check if the number is even or divisible by 3 n = int(input('Enter integer: ')) if n % 2 == 0 or n % 3 == 0: print(f'Number {n} is even or divisible by 3')
Check if the number is even and divisible by 3
n = int(input(‘Enter integer: ‘))
if n % 2 == 0 and n % 3 == 0:
print(f’Number {n} is even and divisible by 3’)
elif n % 3 == 0:
print(f’Number {n} is divisible by 3 but not even’)
elif n % 2 == 0:
print(f’Number {n} is even but not divisible by 3’)
By combining logical operators like boolean or and boolean and, we can create powerful conditional statements to suit various needs.
Conclusion
Conditionals are fundamental to programming logic, enabling us to make decisions based on different scenarios. By mastering conditional statements in Python, you gain the ability to create dynamic and responsive code that handles a wide range of situations efficiently. Remember, proper usage of conditionals can greatly enhance the functionality and readability of your code. Experiment with different scenarios and leverage conditional statements to write more robust programs. Happy coding!
Looking to improve your Python skills further? Explore our hands-on labs for more practical experience.
Don’t miss out on the opportunity to advance your data engineering skills with labs.itversity.com.
Join our community today and unlock a world of possibilities in the realm of Big Data. Start creating
tables and mastering SQL with us!