Simple Python If-else statements
At times, we might need to do something, on the basis of some condition. Let’s consider a situation, where we are writing a game, and when the user presses the spacebar, then we want the character to jump. So, this is like if the user presses the spacebar, then makes the character jump. So here, we are doing something, on the basis of some conditions.
Let’s consider another simple situation, where we want to print that the number is even, only when the number is even. So, we would do something like – if the number is even, print that the number is even. Now, in the previous line, I want you to concentrate on that if. We have this if as a keyword in Python, which we are going to use soon.
So, we can just make the use of if else statements, when we need to do something, on the basis of some condition. So, we will just write the if clause, and then we would specify the expression, and then if the expression evaluates to True, then the if block would execute, and if the expression evaluates to False, then the else executes.
Let’s have an example syntax here, which would give us an idea about how we can write the if else statements.
In the above sample code, you can simply see that we have written the if keyword, and after that, we would have some expression (the word expression in the code is just a placeholder, and we would write some expression soon). But the if block executes when the expression evaluates to True, and the else block executes if the expression evaluates to False. This is the basic idea, and we are soon going to work on it.
Let’s have a look at a very simple and basic program, where we would simply check if the number entered by the user, is even or odd. For that, first of all, we would have the number as user input, then we would check that if the number is divisible by 2, and if it is True, then we would print that the number is even, and else we would print that the number is odd. Here is the program for the same.
As you can see in the above program, we are taking some number as a user input, and then we are checking that if the number is divisible by 2. if this is True, we print that the number is even, and else we print that the number is odd.
With this example, you can get an idea about the expression that we can have here. The expressions are going to evaluate to True or False. Here, you can try executing the program for some even and odd inputs.
While we would move ahead and use the conditional statements in our programs often, you would get familiar with them, and would be comfortable in using them in the programs.
But the thing to be kept in mind here, is the indentation levels. The indentation here needs to be followed, after the if statement, we have certain indentation, which helps specify that this code is a part of the if block. You can understand this thing from the below program.
As you can see in the above program, we have a print statement, which has a different indentation. Basically, we need to focus on the indentations, since they have a great importance in our programs.
But there is no need to worry, since when you would go through some different programs, you would get familiar with those indentations as well.
One more thing to mention here is that we have written the if statement, and there we have written some expression as well.
Then we have some code, which would execute if the expression evaluates to True. Then we have the else block, which executes if the expression evaluates to False. The thing is that we can write some if block, without the corresponding else block. You can simply try writing only the if and not the else. Let’s have a look at a simple program, where we have the if block and not the else block.
As you can see in the program, we are taking a number as user input, and then we have the if statement, and the expression to check if the entered value is greater than 10. If the value is greater than 10, then we are going to print that the number is greater than 10. Here, we do not have the else written. The else block executes if the given expression evaluates to False, and since we have not written the else block, then we are not doing anything if the expression evaluates to True. But we cannot write an else without an if. You can simply try doing so and observe the output on trying to execute the program.