Python is a simple and powerful programming language for building great projects. In almost every project, we develop a situation where we need to make decisions on some conditions. For example, imagine that we were building a simple web application like isitchristmas using Django or Flask.
This application checks the date and tells us if it is Christmas or not. In python, we can implement this kind of problem by simply using the DateTime library to check the date and then use the if/else condition to check if the date is 25 December. So if/else conditioned are very useful for such situations where we want to make decisions based on some conditions.
In this tutorial, we will learn how to use the python if/else statements, which will help us deal with decision making in our python program. We will also look at some practical examples to see how it works. To follow this tutorial, you need to have python installed in your system; if you don’t have python installed, you can follow our guide on installing python on Linux.
Logical operators in python
Python has support for some logical operators that can be used with the if/else statements to make it more powerful. The below list shows some of the useful python logical operators.
- a==b: a is equal to b.
- a!=b: a is not equal to b.
- a<b: a is less than b.
- a>b: a is greater than b.
- a<=b: a is less than or equal to b.
- a>=b: a is greater than or equal to b.
The if statement
The If statement is the simplest decision-making statement. It is present in almost every programming language, including C, C++, PHP, Javascript, etc,. Instead, it is also present in the Linux bash scripting. The if statement is used to check if an expression is true, and if it is true, then the block of code that is indented in it will be run. The syntax of the if statement in python is something different from C, C++, or javascript. It depends on the indentation instead of curly brackets. Following is the syntax of the if statement in python.
if expression_true: statement(s)
In the above syntax, the expression_true is where we need to give the expression we need to check if true or false. In the statement(s) we will have the code block, which will be run if the expression will be true. The below code shows a practical example.
# creating a variable with an integer value var = 10 # checking if the integer is greater than 0 if var>0: print("The number is greater than 0")
In the above code, we have created an integer variable containing the value 10. Then we use the python if statement with the condition var>0 to check if the integer is greater than 0. The if statement checks if the condition is true or false, and if it is true( which is in our case ), then the code present in the if block (which is a print() statement in the above code block) will be run.
Output:
In python, indentation is significant since python does not use curly brackets for if/else statements or loops, so the main syntax depends upon the indentation. For example, if we run the following code, we will get an error.
# creating a variable with an integer value var = 10 # checking if the integer is greater than 0 if var>0: print("The number is greater than 0") # unindented code
In the above code block, we have written the code correctly, but at the end, we have not indented the print() statement, which treats the print() statement to be outside of the if statement and assumes that the if statement is empty.
Output:
Python elif Keyword
Python if/else decision making also has a keyword elif stands for else if, which means that if the previous conditions were not True, then check this condition. For example, see the below code.
# creating a variable with an integer value var1 = 10 var2 = -1 # checking if the integer is greater than 0 or less than 0 if var1>0: print(f"{var1} is positive") elif var1<0: print(f"{var1} is negative") if var2>0: print(f"{var2} is positive") elif var2<0: print(f"{var2} is negative")
In the above code, we have created two variables with the values 10 and -1. Then we use the if and elif statements to check if the integer is positive or negative.
Output:
Python else Keyword
The final keyword in python if/else statement is the else keyword. Unlike the elif keyword, the else keyword is present in the if/else statements in almost every programming language. The block of code present in the else keyword will be run if all the above expressions will be False. The following code shows a practical example of the else keyword in the if/else statement.
# creating a python list python_list = [-1, 0, 1, -2, 2,-5] # Using a for loop to access each element of the list for integer in python_list: if integer>0: # checking if the integer is greater than 0 print("The number is greater than Zero") elif integer==0: # checking if the element is equal to 0 print("The integer is equal to zero") else: # checking if the element is less than 0 print("The integer is less than zero")
In the above code, we first created a python list containing some integers. We use the python for loop to get each integer and then use the if/elif/else statement inside for loop to check if the integer is positive, negative, or zero.
Output:
Short-hand if
Whenever in a python code, we need to run only one statement, if a condition returns true, we can use the shorthand if statement. The shorthand if statement is a short version of the if statement with a simple syntax. The syntax of the short-hand if statement is shown below.
if condition: statement
Let us look at an example code to see how to use the short-hand if statement in python. See the below code.
# creating a python list containing integers integers = [ 1, -2, 0, 999, -34] # using for loop to access each item of the list for integer in integers: # check if the list item is positive if integer>0: print(f"{integer} is positive")
In the above code, we have not indented the if statement but given both expression and statement in the same line of code. On running the above code, the if statement will check which integers are positive in the list and then print them.
Output:
Short-hand if else
Like the short-hand, if statement, there is also a short-hand if/else statement in python. The syntax of the shorthand, if-else, is shown below.
statement1 if condition else statement2
In the above syntax, statement1 is the statement that will be executed when the condition is true, and statement2 is the statement that will execute when the condition is false. The below code shows a practical example of the shorthand if/else statement.
# creating a python list containing integers integers = [ 1, -2, 0, 999, -34] # Using the python for loop to get each item of the list for integer in integers: # checking if the number in the list is positive or negative print(f"{integer} is positive") if integer>0 else print(f"{integer} is negative or zero")
In the above code, we write both the else if statements in a single line of code.
Output:
Nested if statements
The if statement which is inside another if statement is called a nested if statement. Python allows us to nest an if statement into another if statement. The syntax of the nested if statement is-
if expression1: statements(s) if expression2: statement(s) else: statement(s) else: statement(s)
In the above syntax, the code will first check the expressions. If it is true, the if statement nested inside it will be executed. It checks for expression2 and runs some statements. If the expression1 is false, the else statement present at the bottom will be run.
The below code block shows a practical example of how to use nested if else statements.
# creating a python list containing integers integers = [ 1, -2, 0, 999, -34] # accessing each element of the list for integer in integers: # checking if the list is positive or negative if integer > 0: print(f"[+] {integer} is positive") # checking if the list is odd or even if integer%2 == 0: print(f"[+] {integer} is a positive even integer") else: print(f"[+] {integer} is a positive odd integer") else: print(f"[+] {integer} is negative") if integer %2 == 0: print(f"[+] {integer} is a negative even integer") else: print(f"[+] {integer} is a negative odd integer")
In the above code, we use a nested if statement. The parent if/else statement will check if the number is positive or negative, and the nested if/else statement will check if the number is odd or even.
Output:
Conclusion
In this post, we have learned everything about the if/else statements in python. We have seen the basic syntax of the if/else statement and also see short-hand if/else notation and nested if/else. You may also refer to our guide on working with CSV files in python.