In Python programming, if else statements are used to execute different code blocks based on certain conditions. These statements are essential for controlling the flow of a program and making decisions based on certain criteria.
Syntax of if else Statements
The syntax of if else statements in Python is straightforward:
if condition:
# code block to execute if condition is True
else:
# code block to execute if condition is False
The if
keyword is followed by the condition to be evaluated. If the condition is true, the code block under the if
statement will be executed. If the condition is false, the code block under the else
statement will be executed.
Conditions can include:
- Comparing values using comparison operators like
<
,>
,<=
,>=
,==
, and!=
. - Combining conditions using logical operators like
and
,or
, andnot
. - Checking if an item is present in a sequence (like a list, tuple, or string) with the
in
keyword.
Syntax of if elif else Statements
Python also provides elif
(short for “else if”) to test multiple conditions:
if condition1:
# code block to execute if condition1 is True
elif condition2:
# code block to execute if condition2 is True
else:
# code block to execute if none of the conditions are True
Examples of if else Statements
Here are some examples that illustrate the use of if else statements, including if elif else
statements in Python:
Example 1: Checking if a number is positive or negative
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
else:
print("Negative number")
In this example, the user is prompted to enter a number. The if
statement checks if the number is greater than zero. If it is, the program will print “Positive number”. If it is not, the program will print “Negative number”.
Example 2: Checking if a number is even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
This example prompts the user to enter a number and checks if it is even or odd using the modulus operator (%). If the number is even, the program will print “Even number”. If it is odd, the program will print “Odd number”.
Example 3: Checking if a number is between two values
num = int(input("Enter a number: "))
if num >= 10 and num <= 20:
print("Number is between 10 and 20")
else:
print("Number is not between 10 and 20")
This example checks if a number entered by the user is between 10 and 20. If the number is between 10 and 20, the program will print “Number is between 10 and 20”. If it is not, the program will print “Number is not between 10 and 20”.
Example 4: Checking if a string contains a specific character
string = input("Enter a string: ")
if "a" in string:
print("String contains the letter 'a'")
else:
print("String does not contain the letter 'a'")
This example checks if a string entered by the user contains the letter “a”. If the string contains “a”, the program will print “String contains the letter ‘a'”. If it does not, the program will print “String does not contain the letter ‘a'”.
Example 5: Checking if two values are equal
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
if num1 == num2:
print("The two numbers are equal")
else:
print("The two numbers are not equal")
This example prompts the user to enter two numbers and checks if they are equal. If the numbers are equal, the program will print “The two numbers are equal”. If they are not equal, the program will print “The two numbers are not equal”.
Conclusion
If else statements are an essential part of programming in Python. They allow you to make decisions based on certain criteria and control the flow of your program. By using if else statements, including if elif else
statements, you can create more complex programs that can handle different scenarios and situations.