This chapter will be a new milestone in learning about the programming world and you will be one step nearer to being called a programmer. You will learn about 'if' and 'else' in this section.
'if' and 'else' are used for making decisions. For example, you can think of - if your marks are more than 35% then you pass, else fail. If today is your birthday then it's a special day, else normal day.
One more example you can think of is that if you are making any website and want to allow some features to users with an account only, then you can think of - if a user has account, then this else not.
Now let's implement this in Python.
a = input() #taking input from user if a>10: print "your number is greater than 10" else: print "your number is smaller than 10"
your number is greater than 10
How to write?
In the first line, we are taking input.
if a>10: - Here we have used 'if'.
a>10 - This is the condition of 'if'. This condition is comparing a with 10 and if a is greater than 10, then it is True and if smaller then False.
So, the whole story is that if a>10 ( if a is greater than 10 ), then it will execute print "your number is greater than 10", otherwise, it will execute print "your number is smaller than 10"
See the flow given diagram below.
if condition: statement statement ... else: statement statement ...
We first write if condition: ( condition -> What has to be judged. In the first example, the condition was 'a>10' ). Statements under if will be executed only when this condition is true otherwise, the statements under else will be executed.
Then we write statements (in the previous example, it was print "your number is greater than 10" ).
Statements written inside if or else must be indented equally from the left margin. It means that all the statements written inside if must be equally spaced from the left. We have used indentation of 8 spaces. It means that before every statement inside if, there is a space left of 8 spaces.
Statements written after equal spaces inside if means that the written statements are inside if ( or body of if ).
Same goes with else as well.
Statements written after equal spaces inside else means that the statements are under else.
If the condition of if is true, then statements inside if ( body of if represented by equal indentation to the left margin ) will be executed and if it is false, then statements in else ( body of else i.e. represented by equal indentation to the left margin ) will be executed.
if condition: a b c d else: e f
Here, a, b, c and d are inside if, as these are written after one Tab space after if. Similarly, e and f are inside else, as they are written after one Tab space after else.
So till now, we have only learned to use if and else.
Before going further, let's learn about different operators.
Commonly used arithmetic operators are:
+ - addition
- - subtraction
* - multiplication
/ - division
% - modulus
** - exponentiation
// - integer division
Addition, multiplication and subtraction are simple mathematical operators with no change. Let's see an example over this.
a = 20 b = 10 print a+b print a-b print a*b
First see an example.
print 5/2 print 5.0/2 print 5.0/2.0 print 5/2.0
5/2 gave us 2 and not 2.5. And this is the thing that I was trying to show you. If both the operands of / are integer, then it will also give an integer rounded to the nearest lower integer. It means that 1/2 will give 0 ( as the nearest lower integer near to 0.5 is 0 ). To get float, at least anyone of the both operands must be a float.
% operator gives us the remainder after division of two numbers
** operator is the exponential operator. It will calculate the power of the first operand raised to second. Ex - 4**2 means 42
// is integer division operator. It means that it will give us a value that will be rounded off to the nearest lower integer after the division of two numbers, even if the numbers are float.
Let's see an example of these operators
a = 5 b = 2 print a%b print a**2 print 5.0//2
As we saw % calculated remainder, ** calculated 52 and // gave us 2.0 ( not 2.5 ) which is rounded to the nearest lower integer i.e. 2
In maths, you might have learned about BODMAS rule, but that rule is not applied here. If we have written more than one operation in one line, then which operation should be done first is governed by PEMDAS.
PEMDAS means :
Parentheses have the highest priority. It means brackets i.e. ( ).
Next is Exponential.
Then Multiplication and Division have the same precedence.
Then Addition and Subtraction have the same precedence.
Now a valid question comes in mind that which operator should be done first if they have the same precedence?
The answer is that if operators have the same precedence then they are evaluated from left to right. E.g. - 3-2+2 will be evaluated from left to right. It means that 3-2 will be evaluated first (as it is in left). And 3-2 is 1. So, the expression will become 1+2 and finally get evaluated as 3.
Solving, '*' and '/' first from left to right.
Now solving, '+' and '-' from left to right
So, the answer is -17
Now, you are ready to go for if and else.
Even or odd
a = input() if a%2 == 0: #checking divisiblity by 2 print "Your number is even" else: print "Your number is odd"
You number is odd
Above code is self-explanatory. In the above code, a%2 will give us the remainder by dividing 'a' with 2. And if the remainder is 0 ( perfectly divisible by 2 ), then the body of 'if' will be executed otherwise that of 'else' will be executed.
if under if
Yes, we can use one if inside another. This is also called nesting.
print "Enter a number" a = input() #taking input from user if a%2 == 0: #checking divisiblity by 2 if a>10: print "Your number is greater than 10 and even" else: print "Your number is even and smaller than or equal to 10" elif a%2!=0: print "Your number is odd"
Your number is greater than 10 and even
First see elif.
elif -> elif is 'else if'. I think you have already got the feel of elif. Next lines will make it clearer.
Firstly, the condition in 'if' will be checked.
If it is true, then only 'if' will be executed and if it is false, then the compiler will check below.
If the condition of 'if' is false, then condition in 'elif' will be checked. In the same way, if the condition of elif is true, then its body will be executed otherwise the compiler will come below.
If 'if' and 'elif' both are false then else will be executed.
Now, if the condition of first if is true, then there is another if inside its body. So, now this if will be checked. So, if a number is even ( a%2==0 is true ) then next if inside its body will be checked. So, it will check if the number is greater than 10 or not. And if this if is also true then print "Your number is greater than 10 and even" will be executed otherwise since there is no else statement for this if so program will stop.
Also, if 'else' is not required then we can write only 'if' (without 'else' or 'elif') and it will run smoothly. Try on your own.
See the flow chart below
if condition: statement statement ... elif condition: statement statement ... elif condition: statement statement ... else: statement statement ...
Another way to write the above code.
print "Enter a number" a = input() if a%2 == 0 and a>10: #using 'and' in condition print "Your number is greater than 10 and even" elif a%2 == 0 and a<10: #using 'and' to check if both conditions are true or not print "Your number is even and smaller than 10" else: print "Your number is odd"
Your number is even and smaller than 10
In the Boolean section, you had learned about 'and'.
a%2==0 and a>10 -> if 'and' is used, then it will be true if both are true, meaning if a%2 is equal to 0 and a is greater than 10.
Also, 'elif' will be true if both the conditions (a%2 == 0 and a<10) are true, meaning 'a' is divisible by two and a is less than 10.
You can understand the rest of the codes on your own.
See one more example to understand more clearly.
print "Enter your age." age = input() if age < 13: print "Hey! kid" elif age>13 and age < 20: print "So, you are enjoying your teenage." else: print "You are grown up."
Your are grown up.
Use of pass
'pass' is used to skip from 'if' or 'else'. If we don't have to give any argument in any 'if', 'else' or elif, we use pass. ( We will see later that pass is also used with methods and classes. ). See an example to make it clear
print "Enter your age." age = input() if age < 13: print "Hey! kid" elif age>13 and age < 20: pass else: print "You are grown up."
As we have seen, using 'pass' just skipped and did nothing in 'elif'.
You practice and you get better. It's very simple.