Have you ever wanted to write a program that makes decisions based on certain conditions? if yes. This is where the if-else statements come into play in your program.
The if-else statement is important in Python programming, they allow you to build and execute specific blocks of codes in your programs based on certain conditions. If the condition is true, the program will execute one block of code, and if it is false, it will execute another block of code.
Whether you’re a beginner or an experienced programmer, mastering if-else statements is essential for writing effective and efficient code. in this blog article, we discuss five examples that would help you understand the if-else statements much better in Python.
Example 1. basic if-else statements
There are many basic if-else statement examples we could start with right now. but the most basic if-else statement I would love us to start with is checking if a number is positive or negative.
Here is an example below:
number = 4 if number >= 0: print("it's a Positive number") else: print("it's a Negative number")
In this example if the number variable is greater than or equal to zero, our program would print if statements block of code which is “it’s a positive number” but if the number variable is less than zero or is not equal to zero our program would print else statements block of code, which is “it’s a negative number”.
Example 2. Multiple if-else statements.
You can also use multiple if-else statements to check multiple conditions. For example, let’s say you want to print a message based on the grades students score in an exam.
Here’s an example below:
grade = 75 if grade >= 70: print('you got A') elif grade >= 60: print('you got B') elif grade >= 50: print('you got C') elif grade >= 40: print('you got D') else: print('you failed')
In the example above the program checks each condition of the if-else statements and prints a message based on the grade of the individual which it received.
Example 3: Nested if-else statements
Nested if-else statements are ways you could use to add additional conditions to your if-else statements. They allow you to test multiple criteria and increase the number of possible outcomes.
In a nested if statement, an if-else statement is placed inside another if-else statement. The nested if-else statement being the inner one is only executed if the conditions set on the outer if statement are true.
Here is an example in Python:
a = 20 b = 30 if a > b: print('a is greater than b') else: if a < b: print('a is less than b') else: print('a and b are equal numbers')
In the example above, the program first checks if the first if statement is true, if this is true, the block of code is been executed. if it’s false, the inner if-else statement is executed. This inner if-else statement checks if
x is less than
y. If it’s true, the code inside the if block is executed. If it’s false, the code inside the else block is executed.
In this example, the outer if statement checks if
x is greater than
y. If this is true, the code inside the if block is executed. If it’s false, the inner if-else statement is executed. This inner if-else statement checks if
x is less than
y. If it’s true, the code inside the if block is executed. If it’s false, the code inside the else block is executed. The else statement checks if a and b are equal numbers.
Example 4: Checking if a string is empty or not empty.
In Python, you can check if a string is empty or not by using the len( ) function, the len( ) function returns length of the string. If the length is 0, then the string is empty, if not then the string is not empty. Here’s an example of how to use an if-else statement to know if a string is empty or not empty.
string = "p" if len(string) == 0: print("String is empty") else: print("String is not empty")
In this example, we first set the variable to a string. Then, using the len( ) function. we check to know the length of the string is equal to zero or not. Then we set a condition, If the length is 0, then it prints “The string is empty.” Otherwise, it prints “The string is not empty.”
Example 5: Short if-else statement
In Python, they short if-else statement can be used, if you need to perform a single action based on a condition. It is a compact and easy-to-read way of writing if-else statements in Python programming. Here is an example:
num = 4 print("Positive number") if num >= 0 else print("Negative number")
In this example, in a single line of code, our program checks whether the number is positive or negative and executes the appropriate message. if it’s true, it prints its positive number, else it doesn’t.
Summary and Conclusion
If-else statements are one of the fundamental parts of Python programming, and there are several use cases in which the if-else statements are used. By practising the examples we’ve discussed, you can improve your understanding of how to use if-else statements much better and apply them to your own programming projects.
Praise Nnawuihe is working as a Python developer at Devtsoft. He is passionate about Python.