Python if...elif...else statements tutorial with easy,productive,high quality examples for beginners

What is if...else statement in Python? ... Decision making is required when we want to execute a code only if a certain condition is satisfied. The if

 



Do you know what is if-else statements in python?

The if…Elif…else statement is used in Python for decision-making. An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if. You can also use elif to avoiding else-if again and again. Let's see an example:
if expression: statement(a) else: statement(a)

This program evaluates the test expression and will execute statement(s) only if the test expression is True. If the test expression is False, the statement(s) is not executed. In Python, the body of the if statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end. Python interprets non-zero values as True. None and 0 are interpreted as False.


No need to keep it on your mind just see. Now we are talking about some basic logical conditions from mathematics:
1. Equals: a == b
2. Not Equals: a != b
3. Less than a < b
4. Less than or equal to a <= b
5. Greater than a > b
6. Greater than or equal to a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops. An "if statement" is written by using the if keyword.
For Example:

a = 300
b = 500
if b > a:
  print("b is greater than a")

In this example, we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 300, and b is 500, we know that 500 is greater than 300, and so we print to screen that "b is greater than a".

Now we are showing you a flowchart:

Python flowchart

Python Example:


You will get this output:
3 is a positive number
This is always printed
This is also always printed.

Indentation

Python relies on indentation (whitespace at the beginning of a line) to define the scope in the code. Other programming languages often use curly brackets for this purpose.

a = 33
b = 200
if b > a:
print("b is greater than a"
You will get an error. So, make sure you write your code the correct way.

Python if...else statement



The if..else statement evaluates test expression and will execute the body of if only when the test condition is True.
If the condition is False, the body of else is executed. Indentation is used to separate the blocks.

The elif Statement

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.

Now explain what we write in this code. 

Here we use the input function to get a value from users. Don't think about the input function. In the upcoming day, we will explain the input function. After getting a value from users python will check the following condition. First, it will check if statement then goes to the next step elif then go to the else statement and program will give you an output according to your condition.

Flowchart of if...elif...else statement:

Python Nested if statements

We can write an if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.

Maybe you have a question what is if nested elements?

You can have if statements inside if statements, this is called nested if statements. Hope you get your answer.

Short Hand If

If you have only one or multiple statements to execute, you can put it on the same line as the if statement and it will decrease your program line:

if a > b: print("a is greater than b")

Short Hand If ... Else

You already know what is shorthand if. Now introduce with shorthand if...else. If you have only one or multiple statements to execute, one for if, and one for else, you can put it all on the same line:
a = 2
b = 330
print("A"if a > b else print("B")
Now let's see a multi statements single line example:
a = 300
b = 300
print("A"if a > b else print("="if a == b else print("B")

And

The and keyword is a logical operator and is used to combine conditional statements. Now we are giving an example:
a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

Or

The or keyword is a logical operator, and is used to combine conditional statements. Don't worry we are giving a simple example:
a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

The pass Statement

if statements cannot be empty or you forget to complete your if condition but if you for some reason have an if statement with no content, you will show an error. If you want to avoid this error you can use pass statement. Here an example for you:
a = 330
b = 200

if b > a:
  pass
We are creating a full python tutorial. If you see any problem with our content please comment or contact us.