What is a Python if else statement?
An if and else is a conditional statement that executes a block of code when certain conditions have been met.
Creating a Python if else statement
To create a Python if else statement we begin by writing the word if and then we give it a condition and if that condition is true it moves on to the next line and runs that piece of code.
So a basic if statement will have the word if followed by a condition and then a colon after which when we hit enter there will be an indentation. In this example, we will say that x = 20 and y =15 and then we will state the condition that says if x is greater than y, print x.
So what we’re saying here is that if the value for x is greater than the value for y, and these are both integers, it’s going to run the piece of code that prints out the number 20. As shown in the code below.
Suppose we change the value of y to 25 the code above will no longer run. This is because the condition above will no longer be met. However, if we change the condition to read if x was less than y we would then print x out again. As shown in the code below.
Using the else condition in Python if else statement
What if we want to do something else when the condition is not met. This is where the “else” comes in. If x is less than y, we are going to run a different piece of code. But if it is not met then we place another condition outside the indentation statement we are going to run this piece of code instead.
As you can see in the code above the condition x>y is not met therefore the program opts to run the piece of code after the else statement which then prints out the string “this is our else”.It is important to note that it is all about true and false in if statements.
We can also do the same with text or strings as well as shown in the example below.
If we change the name to ‘Fred’, then the statement is going to skip the first part and run the code after the else statement. As shown here.
Another useful thing that we can do is we can use an if statement to check if something is present in a list. This is quite a common concept and quite handy to know.
To begin this let’s create a list of names. Then write an if statement to check if any of the names are in the list or otherwise.
If we change the condition and check if Jessica is on the list we are going to get a different output as illustrated by the code below.
Always remember that you need the colon at the end and you need to indent the code after the colon and your else comes in the same line as your if.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.