Python Control Statements

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

It is very common for programs to execute statements based on some conditions. In this section we will learn about python if .. else ...  statement.

But before we need to learn about relational operators. Relational operators allows us to compare two objects.

SYMBOL DESCRIPTION
<=  smaller than or equal to
smaller than
greater than
>=  greater than or equal to
==  equal to
!=  not equal to


The result of comparision will always be a boolean value i.e True or  False . RememberTrue  and False  are python keyword for denoting boolean values.

 

Let take some examples:

 

1

2

3

4

5

6

7

8

>>> 3 == 4

False

>>> 12 > 3

True

>>> 12 == 12

True

>>> 44 != 12

True

Now you are ready to tackle if statements.The syntax of If statement  is:

 

1

2

3

4

if boolean-expression:

   #statements

else:

   #statements

Note: Each statements in the if block must be indented using the same number of spaces, otherwise it will lead to syntax error. This is very different from many other languages like Java, C, C# where curly braces (  {} ) is used.

Now let’s see an example

 

1

2

3

4

5

6

i = 10

 

if i % 2 == 0:

   print("Number is even")

else:

   print("Number is odd")

 

here you can see that if number is even then "Number is even"  is printed otherwise"Number is odd"  is printed.

Note: else clause is optional you can use only if clause if you want, like this

 

1

2

if today == "party":

    print("thumbs up!")

here when value of today  is "party"  then thumbs up!  will get printed, otherwise nothing will print.

If your programs needs to check long list of conditions then you need to use if-elif-else statements.

 

1

2

3

4

5

6

7

8

9

10

if boolean-expression:

   #statements

elif boolean-expression:

   #statements

elif boolean-expression:

   #statements

elif boolean-expression:

   #statements

else:

   #statements

You can add as many elif condition as programs demands.

here is an example to illustrate if-elif-else  statement.

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

today = "monday"

 

if today == "monday":

   print("this is monday")

elif today == "tuesday":

   print("this is tuesday")

elif today == "wednesday":

   print("this is wednesday")

elif today == "thursday":

   print("this is thursday")

elif today == "friday":

   print("this is friday")

elif today == "saturday":

   print("this is saturday")

elif today == "sunday":

   print("this is sunday")

else:

   print("something else")

 

Nested if statements:

You can nest if statements  inside another if statements  as follows

 

1

2

3

4

5

6

7

8

9

today = "holiday"

bank_balance = 25000

if today == "holiday":

   if bank_balance > 20000:

      print("Go for shopping")

   else:

      print("Watch TV")

else:

   print("normal working day")

 

In the next post we will learn about Python Functions.