Python if ,else and elif statement

In this post we will discuss the Python if,else and elif statements known as Python conditional statements.They are known as conditional statement since it allows decision making in our program.

The conditional statements are very useful in programming.For instance while making any game you will certainly allow the object to move either left or right of up or down.There is no rule that the object will always move forward.In such situation you can make use of the conditional statements to execute the instruction smoothly.Not only in game programming you can and will always make use of conditional statement in all aspect of programming.

If statement

With ‘if’ statement you can choose which code to be executed based on the result return by some expression.The expression is written just after the ‘if’ statement.And the expression must always return ‘True’ or ‘False’.The format of ‘if’ statement is:
if expression:
  code1 #Your codes

If the ‘expression’ return True the code1 is executed else nothing is performed.

>>> expression=True
>>> if expression:
	print( 'The expression is true' )

The expression is true

In the above code since the ‘expression’ is true so the code is executed.The ‘print()‘ is use to print out sentences in the screen.Some more example is given below.

>>> last=1
>>> if last>4 : #check if last is greater than 4
	print('last is smaller than 4')

>>> if last<2: #Check if last is smaller than 2
	print('last is smaller than 2')

last is smaller than 2

In the first code since ‘last’ is 1 so the expression ‘last>4’ returns False hence the ‘print’ statement is not executed.In the second code the expression ‘last<2’ returns True hence the output ‘last is smaller than 2’.

So you can use any expression for the ‘if’ statement but it must always return either True or False.Let us try using other data type with ‘if’ statement.

&amp;gt;&amp;gt;&amp;gt; #Using string as an expression
&amp;gt;&amp;gt;&amp;gt; txt1='New'
&amp;gt;&amp;gt;&amp;gt; if txt1:
	print( txt1 )

&amp;gt;&amp;gt;&amp;gt; #Using dictionary 
&amp;gt;&amp;gt;&amp;gt; dic={23:789 , 'New':'Old' , 'happy':'Sad'}
&amp;gt;&amp;gt;&amp;gt; if dic:
	print( dic )

{23: 789, 'New': 'Old', 'happy': 'Sad'}
&amp;gt;&amp;gt;&amp;gt; #Using list 
&amp;gt;&amp;gt;&amp;gt; lst=[23 , 56]
&amp;gt;&amp;gt;&amp;gt; if lst:
	print( lst)

[23, 56]
&amp;gt;&amp;gt;&amp;gt; #Using empty dictionary
&amp;gt;&amp;gt;&amp;gt; dic1={}
&amp;gt;&amp;gt;&amp;gt; if dic1:
	print( dic1 )

Almost all the data type seems to return True hence the code is executed,except for the empty dictionary ‘dic1’ which return False.

LInk :Dictionary data type
LInk :List data type
LInk :String data type

else statement

With ‘if’ statement we could execute a code only if the expression returns True,we can also allow execution of another code if the expression return False.We can do this by using the ‘else’ statement.The format of using ‘else’ is simple.
if expression:
  code1 #Your codes
else :
  code2 #Your codes

If the ‘expression’ returns true ‘code1’ is executed else ‘code2’ is executed.

&amp;gt;&amp;gt;&amp;gt; num=1
&amp;gt;&amp;gt;&amp;gt; if num&amp;gt;10:
	print('num =',num)
	print(num , ' is smaller than 10')

1  is smaller than 10

Since num is 1,which is smaller than 10 so the code under ‘else’ statement is executed.With ‘if and else’ statement we can allow two paths for the program to take,if we need more conditions or paths then we can use the ‘elif’ statement which is discussed below.

NotE:The way the sentence inside print() is written is different.In print(‘num =’,num) there is a text inside the quotation and then there is a coma and then the num name which is not written inside the quotation.So here what happen is the text inside the quotation will be written as it is when outputted to screen but the num not written inside the quotation is replaced by it’s value which is 1.Also in the print() under ‘else’ statement the num is replaced with the number of num which is 1 the but remaining text ‘ is smaller than 10’ is outputted as it is.

elif statement

With ‘if and else’ statement there are only two paths our program can take either the ‘if’ path or the ‘else’ path.What of we require more conditions,here we will use another statement known as elif.This statement allows you add as many conditions/path as you require.The format of using ‘elif’ is:
if expression1:
  code1 #Your codes
elif expression2 :
  code2 #Your codes
elif expression3 :
  code3 #Your codes
elif expression4 :
  code4 #Your codes
  codeLast #Your codes

The expression1 ,expression2 ,expression3 and expression4 are the conditions for executing their respective codes.With ‘elif’ we add it’s own condition and so we are not letting the expression of the ‘if’ statement to decide everything.

&amp;gt;&amp;gt;&amp;gt; num=100
&amp;gt;&amp;gt;&amp;gt; if num&amp;lt;10:
	print(num , &amp;quot; is smaller than 10&amp;quot;)
elif (num&amp;gt;10) &amp;amp; (num&amp;lt;50): 
	print( num , ' is greater than 10 but smaller than 50')
elif num &amp;gt;50:
	print( num ,' is greater than 50')
	print(&amp;quot;Number cannot be specify&amp;quot;)

100  is greater than 50

The expression of ‘if’ statement check if num is smaller than 10,then the first ‘elif’ statement expression check if the num is grater than 10 or smaller than 50 (the expression ‘(num>10) & (num<50)' does this ).Then the next 'elif' check if num is greater than 50,and the last 'else' statement is used if none of the 'if' and 'elif' condition is fulfilled.But here num is 100 and the condition of the second 'elf' (num>50) is meeted out so it’s code is executed and we get the ouput as “100 is greater than 50”.

Now what if more than one expression returns true meaning,the expression of ‘if’ returns true and other expressions of ‘elif’ also returns true.In such case the first expression that return true will have it’s code executed.

&amp;gt;&amp;gt;&amp;gt; num=1000
&amp;gt;&amp;gt;&amp;gt; if num&amp;gt;10:
	print(num,' is greater than 10')
elif num&amp;gt;200:
	print(num,' is greater than 200')
elif num &amp;gt;500:
	print(num,' is greater than 500')
elif num &amp;gt;=1000:
	print(num,' is greater than or equal to 1000')
	print(num,' is greater than everything')

1000  is greater than 10

Do you see that the condition of the ‘if’ statement and the three ‘elif’ that follows are all true but only the ‘if’ statement code is executed,the other three are not.So we can conclude that the first expression that fulfill the condition or return true will be executed and the other statement are neglected even if they return True.

We can also nest(meaning write) the ‘if’ statement inside another ‘if’ or inside the ‘elif’ statement,both the ways work fine.Consider the code below.

&amp;gt;&amp;gt;&amp;gt; num=500
&amp;gt;&amp;gt;&amp;gt; if (num&amp;gt;100) &amp;amp; (num&amp;lt;400):
	print(num,&amp;quot; is greater than 100 but smaller than 400&amp;quot; )
elif num&amp;gt;400:
	if (num&amp;gt;400) &amp;amp; (num&amp;lt;500): #nested if
		print(num,&amp;quot; is greater than 400 but smaller than 500&amp;quot; )
	elif (num==500) | (num &amp;gt;500): #nested elif
		print( num , ' is equal to 500 or greater than 500')
elif num &amp;gt;1000:
	print( num , ' is greater than 1000')
	print( 'num is unspecifiable')

500  is equal to 500 or greater than 500

Inside the first ‘elif’ there are ‘if’ and ‘elif’ nested and then next ‘elif’ is written outside.This program works fine because nesting if and elif are allowed.Such nestation allows programmer to express the complex idea more smoothly while coding and thus resulting to more efficient code and programs.

A more short cut method of writing if,else statement is known as ternary operator.