Member-only story
Hi everyone, welcome back. In this tutorial, I will show how to use if else statements in Python with examples. If else statements are used to handle conditions or choices within a program. If a certain condition is true, then a block of code will execute, if that condition is false, then separate block of code will execute.
In order to create these decision statements, we need to first understand relational operators:
- > Greater than
- < Less than
- >= Greater than or equal to
- <=Less than or equal to
- == Equal to
- != Not equal to
These relational operators allow us to compare one value with another value to determine if a condition has been met.
Let’s see an example of how an if statement works:
x = 1
y = 0
if x > y:
print("x is greater than y")Output
x is greater than y
As you can see, I am setting the ‘x’ variable to 1 and the ‘y’ variable to 0. In my if statement, if x is greater than y, which resolves as true since 1 is greater than 0, execute the following code. Notice how there is an indent after the if statement. This indent tells Python which block of code to execute after…