In python one of the most used statements is Python If Else or Python Else If statements. With these statements we check special conditions and according to this condition, we do something. The usage of this statements can be differently like below.
We can only use if statement;
We can use if and else statements together;
We can use one or more elif with if and else statements;
Now, let’s do Python Else If examples to understand these statements better.
The output of this python if else example will be:
Now, let’s use if, elif and else statements together.
The output will be:
Python If Else or Python Else If statements are often used with python for loops. To show the usage of these two statements together, let’s do an example.
The output of this example will be:
Now, let’s find the numbers that can be divided by five in the given number tuple. Here, we will check the numbers in the tuple if they can be divided by five or not. If yes, then we will print, if not, the code will g oto else statement. In the body of the else statement, there is “continue” statement. So, we will not do anything here, instead, we will g oto the other for loop step.
For the above python code, the output will be:
In the below example, we will check a number list for a number. If the number in the list smaller than the number that we are searching for, then we will print that it is smaller. If it is not, then we will check if it is higher number or not. If it is higher, then we will print that it is a higher number. And at the last statement, at else statement, we will that it is equal to this number.
The output of this python code will be like below:
30 is greater than 21.21 is equal to 21.11 is smaller than 21.16 is smaller than 21.82 is greater than 21.47 is greater than 21.28 is greater than 21.79 is greater than 21.
Sometimes we need to check one more condition in the if statements. There are two operators that we us efor this purposes. One of them is “and”, the other one is “or”.
With “and” operator, we check the given conditions and if both the conditions are provided, then the body of the if statement runs.
With “or” operator, we check the given conditions and if one of them is provided, then the body of the if statament runs.
Now, let’s show how to use and operator with python if else statement with an example.
The output of this code will be only:
We will do the same example with “or” operator this time.
Nested If Statements are the if statemetns used in another if statements. We can use multiple if statements inside another if statements.
In the below examples, we will shows a nested if statement that includes two if statements. In this example, we will check if the numbers in the tuple can be divided by both 3 and 9.
As the output, we will get:
As you can see here, firstly, the numbers that can be divided by 3 are checked. And between these numbers, the numbers that can be divided by 9 has printed.
Now, we will use range method, and operator and nested if statements together. We will print the even numbers between 8 and 18.
The output of this code will be:
The similar code can be written by the help of range function and for loop. Because, range function has three parameters: starting item, ending item and step.
The below code, will give the same output like the above example.
In this lesson, we have learned how to use Python If Else, Python Else If statements with different examples. You can increase these examples by yourself. And with more if examples, you can understand this lesson better. So, you can think about different Python If Else scenarios and try to write it with python code. Condition statements are very important for python coding. So, if you learn this lesson better, in the following lessons, you will be more comfortable.