Python Basics

Python Handling Conditional Operations

One of the powerful and of course a very important concept is conditional operations. For instance, lets consider the case of facebook login.

Can you guess, how it functions? (Avoid in depth analysis, just focus on over all function.)

Very simple.

If a user has logged in with valid user name and password, show him latest news feed.

Right?

Similarly Gmail login also follows same conditional operations

If user has logged in with valid email and password then show him his latest unread messages.

 

So the point here is, a programmer need to execute certain code only if a condition is met.

In one of the previous chapter I had discussed about modulus operator. With the help this operator of lets check if a number is even or not.

Example

num=100

if num%2==0:

print(“num is an even number”)

So the above code is going to print num is an even number

You must be thinking in the above code, I might have mistakenly indented the print line. No dear friend. I have not indented mistakenly but purposefully. When ever you use if condition, then the if condition must end with a colon and the corresponding block of code must follow minimum one space indent. If you forget to indent the corresponding block of code after if condition then it will throw an error.

In the above code print function going to print the message only if the condition is met. Here the conditions is meeting.

Now consider the below code:

num=109

if num%2==0:

print(“num is even”)

The above example is not going to print any thing. Because num is not divisible by 2 and hence the condition is not meeting here. So it wont execute the print code.

Now, in the above example there are two situation. One the provided number could be even or the provided number could be odd.

So we need to handle both situation. So how can we handle such conditional operation where there are two situation rather than one situation in the above.

With the help of if and else conditional statement, we can handle such situation.

Syntax

if condition 1:

do_task()

else:

do_another_task()

Note: if condition ends with colon (:) and else also ends with color (:) and its corresponding block of code must follow minimum one space indent.

Example

num=109

if num%2==0:

print(‘num is an even number’)

else:

print(‘num is an odd number’)

The above code of block is going to print – num is an odd number.

Now lets move one more step further. What if more than two situation rises? In the above example we have two sitautions: either the number is odd or the number is even. Lets see how can we handle more than two situation with the help of conditional logic.

Syntax

if condition 1:

do_task_one()

elif condition 2:

do _task_two()

elif condition 3:

do_task_three()

else:

do_task_four

Lets see the above syntax in an example context

num1=100

num2=200

if num1>num2:

print(‘num1 is greater than num2’)

elif num1<num2:

print(‘num1 is less than num2’)

else:

print(‘num1 and num2 both are same’)

The above code will print – num11 is less than num2

So this is how we use conditional operations in python programming.