In the enchanting realm of Python, conditionals are your magical wand for executing different computations or actions based on whether a particular boolean constraint evaluates to true or false.
These decision-making spectacles are choreographed by the charismatic trio of if, elif, and else keywords in Python.
The Marvelous ‘if’ Statement
The ‘if’ keyword takes center stage for decision-making purposes. The statements nestled inside the ‘if’ body pirouette into action only when the given condition sparkles with truth.
Flow Diagram
![Python Conditionals - Flow chart](https://datatuts.com/wp-content/uploads/2023/11/Conditional-Flow-Chart.png)
Syntax
![Python Conditionals - if syntax](https://datatuts.com/wp-content/uploads/2023/11/Syntax-If.png)
Examples
x = int(input())
if 1 <= x <=10:
print(x, "lies between 1 to 10")
x = int(input("Passing Percentage: "))
if x >= 85:
print("Congratulations")
print("You are qualified")
Python’s Rule of Indentation
While most programming languages use indentation for code organization, Python takes it to heart. Indentation is not just a suggestion; it’s the director’s cut, determining the scope of the code.
It’s how Python discerns which code belongs to which part of the program. Unlike other languages with curly brackets, Python relies on the art of indentation.
![Python Conditionals - Concept of Indentation](https://datatuts.com/wp-content/uploads/2023/11/Python-Indentation.png)
The Ever-Reliable ‘Else’ Statement
In the grand theater of conditionals, the ‘else’ keyword gracefully steps in when the ‘if’ condition bows out, proving false. The ‘else’ statement is the loyal companion of ‘if,’ completing the duo.
Syntax
![Python Conditionals - if-else syntax](https://datatuts.com/wp-content/uploads/2023/11/Syntax-If-Else.png)
Example
password = int(input())
if password == 78659:
print("Access granted")
print("How can i help you, Sir")
else:
print("Access denied")
print("2 more chances are left")
The Versatile ‘Elif’ Statement
The ‘elif’ keyword is Python’s way of saying, “If the previous conditions didn’t ring true, let’s try this one.” It’s a trusty companion to both ‘if’ and ‘if-else’ statements.
Syntax
![Python Conditionals - if-elif-else syntax](https://datatuts.com/wp-content/uploads/2023/11/Syntax-If-Elif-Else.png)
Example
marks = int(input())
if marks > 80:
print("First division")
elif 60 <= marks <= 80:
print("Second division")
elif 40 <= marks < 60:
print("Third division")
else:
print("You are not qualified")
Nesting Conditionals for Drama
Sometimes, the script calls for an additional condition after the main act. Enter the world of nested if constructs. It’s like having an if-elif-else play inside another if-elif-else production. The show goes on, and you can nest as many statements as you please.
Syntax
![Python Conditionals - Nested if-else syntax](https://datatuts.com/wp-content/uploads/2023/11/Syntax-Nested-If-Else.png)
Programs
n = int(input())
if n % 2 == 0:
print(n, "is an even no.")
else:
print(n, "is a odd no.")
# Square = length and breadth are the same
length = int(input())
breadth = int(input())
if length == breadth:
print("The given rectangle is also a square")
else:
print("It is not a square")
num = int(input())
# Enclosing the conditional statement 'num > 0' within parenthesis is not compulsory.
if (num > 0):
print("Positive")
elif (num < 0):
print("Negative")
else:
print("Zero")
'''
# The unicode / Ascii for the following:
digits -> 48 to 57
Alphabets
Uppercase -> 65 to 90
lowercase -> 97 to 122
'''
val = input("Enter a Charcater: ")
val_unicode = ord(val)
if 48 <= val_unicode <= 57:
print(val, "is a digit")
elif (65 <= val_unicode <= 90) or (97 <= val_unicode <= 122):
print(val, "is an alphabet")
else:
print(val, "is a special character")
Note:
ord(‘ch’):
- Returns the unicode from the given character.
- Accept a string containing a single character as the input inside it.
For example, ord('A')
will output 65, ord('4')
will result in 52.
num1 = int(input())
num2 = int(input())
num3 = int(input())
if num1 >= num2:
if num1 >= num3:
print(num1, "is largest")
else:
print(num3, "is largest")
elif num2 >= num3:
print(num2, "is largest")
else:
print(num3, "is largest")
'''
# Leap year
A leap year is a year that contains 366 days (29 days in the month of February) inside it. For a year to be a leap year, either one of the following conditions must be satisfied:
- Year is divisible by 400
- Year is divisible by 4 but not by 100
'''
year = int(input())
if year % 400 == 0:
print(year, "is a leap year")
elif year % 4 == 0 and year % 100 != 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Conditionals in Python are not just code structures; they are the plot twists that bring your programs to life. Embrace them, and may your code adventures be ever delightful!