Python Else If, Python If Else

python-else-if-condition-5

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;

 

if (condition):
   body

 

We can use if and else statements together;

 

if (condition):
   body
else:
   body

 

We can use one or more elif with if and else statements;

 

if (condition)
   body
elif (condition):
   body
else:
   body

 

Now, let’s do Python Else If examples to understand these statements better.

 

x = 5
if (x % 2 == 0):
  print("x is an even number")
else:
  print("x is an odd number")

 

The output of this python if else example will be:

 

x is an odd number

 

python-if-else-condition-1

 

Now, let’s use if, elif and else statements together.

 

x = 10
if x > 10:
  print("greater")
elif x < 10: 
  print("smaller")
else:
  print("equal")

 

The output will be:

 

10

 


 

Python For Loop With If Statement

 

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.

 

 

characters=["Arwen", "Legolas", "Aragorn", "Elrond" , "Galadriel"]
for x in characters:
 if x == "Aragorn":
  print("{} is an Human.".format(x))
 else :
   print("{} is an Elf.".format(x))

 

The output of this example will be:

 

Arwen is an Elf.
Legolas is an Elf.
Aragorn is an Human.
Elrond is an Elf.
Galadriel is an Elf.

 

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.

 

numbers=(5,12,25,33,48,50,61)
for x in numbers:
 if x % 5 ==0:
  print("{} divided by five.".format(x))
 else:
  continue

 

For the above python code, the output will be:

 

5 divided by five.
25 divided by five.
50 divided by five.

 

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.

 

list1=[30, 21, 11, 16, 82, 47, 28, 79]
for x in list1:
 if x < 21:
  print("{} is smaller than 21.".format(x))
  elif x > 21:
  print("{} is greater than 21.".format(x))
  else :
   print("{} is equal to 21.".format(x))

 

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.

 


 

Using And / Or Operators with Python If Else 

 

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.

 

numbers=(5,12,25,33,48,50,61)
for x in numbers:
 if x % 5 == 0 and x % 10 == 0:
  print(x)
 else:
  continue

 

The output of this code will be only:

 

50

 

python-if-else-statement-3

 

We will do the same example with “or” operator this time.

 

numbers=(5,12,25,33,48,50,61)
for x in numbers:
 if x % 5 == 0 or x % 10 == 0:
  print(x)
 else:
  continue

 

5
25
50

 

python-if-else-statement-2.

 


 

Nested If Statements

 

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.

 

numbers=(30,44,54,63,78,96)
for x in numbers:
 if x % 3 == 0:
  if x % 9 == 0:
               print ("{} can be divided both 3 and 9.".format(x))

 

As the output, we will get:

 

54 can be divided both 3 and 9.
63 can be divided both 3 and 9.

python-else-if-condition-5

 

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.

 

for x in range(20):
 if x < 18 and x > 8:
   if x % 2 == 0:
    print(x)

 

The output of this code will be:

 

10
12
14
16

 

python-else-if-condition-6

 

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.

 

for x in range(10,18,2):
 print(x)

 

10
12
14
16

 

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.

 

Back to: Python Programming Course > Python Conditions

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k