Conditional Statements in Python

In this tutorial, we will study about conditional statements (if, if else and elif statements). These statements are used to make decisions with respect to some condition. Before starting this tutorial, I will suggest you to install IDE for python like Aptana Studio or Komodo IDE. Both of these IDEs are free. It will save your time as you do not have to open your file in terminal again and again. You can compile and run code directly from IDE.

Conditional statements check conditions and take decision what to to.

If Statements:

Lets take an example first:

We have a variable, say name, and we want to print 'name is harish' if name is equal to 'harish'. We will write code like this:

  1. name = 'harish'
  2. if name == 'harish':
  3. print('name is harish')

Output:

  1. name is harish

The if loop will run if the condition is true or non-zero or non-empty. Here is three words:

  1. true: meaning of this word is clear itself. As in example, we have used comparison operator '==' which will check name is equal to 'harish' or not. If yes then it will return true otherwise false. It means condition is true or false.
  2. non-zero: We can also experiment by putting 0 and non-zero numbers at the place of condition. In fact, 0 is considered as false and other non-zero letters are considered as true.
  3. non-empty: All empty or null variables are considered as false and other variables / values are considered as true. Please not that empty doesn't mean that you leave condition blank. Empty mean null or undefined.

For better understanding, please try all of the three types discussed above. Note that there is some space before print() function. In python there is no braces like C, C++, Java or PHP. If you have leave some space means statement is inside the above statement. Look at these two examples:

  1. name = 'harish'
  2. if name == 'harish':
  3. print('name is harish')
  4. print('this statement is also in the if section')
  5. print('this statement is outside the if section')

Now if name is equal to 'harish' then you will get output:

  1. name is harish
  2. this statement is also in the if section
  3. this statement is outside the if section

otherwise:

  1. this statement is outside the if section

It is because first two print statements are inside if statement. Note if the space before both statement is not equal then you will get error.

if else statement:

This statement is selected when we have to choose one thing out of two things. For example, we want that if name is 'harish' then print 'name is harish' otherwise print 'name is not harish'.

  1. name = 'harish'
  2. if name == 'harish':
  3. print('name is harish')
  4. else:
  5. print('name is not harish')

Now note that I give different space before print statements. You will not get error this time because you can give any amount of space for each new section. Section of 'if' is different from section of 'else'. But you have to give same space to all statements in same section. It is always good practice to give same space in all sections because it will make code more readable.

  1. name = 'harish'
  2. if name == 'harish':
  3. print('name is harish')
  4. else:
  5. print('name is not harish')

nested statement:

We can use new statement inside if or else statement. For example: we want to print 'name is harish' if name is 'harish' otherwise print 'name is sachin' if name is 'sachin' and if both conditions are false then print 'security alert! unknown member found'. We can do it like this:

  1. name = 'harish'
  2. if name == 'harish':
  3. print('name is harish')
  4. else:
  5. if name == 'sachin':
  6. print('name is sachin')
  7. else:
  8. print('security alert! unknown member found')

elif statements:

If we have more than two choices then we can use elif statement. It is similar to nested statement. We will rewrite above example:

  1. name = 'harish'
  2. if name == 'harish':
  3. print('name is harish')
  4. elif name == 'sachin':
  5. print('name is sachin')
  6. else:
  7. print('security alert! unknown member found')

First program will check first statement if it is false then second and if second statement is also false then it will execute code of else statement. Here I have used only one elif statement, you can use elif any number of time between if and else.

Using more than one condition:

We can use more than one condition. For example, we want to print 'valid member' if name is 'harish' or 'sachin' otherwise print 'security alert! unknown member found.

  1. name = 'harish'
  2. if name == 'harish' or name == 'sachin':
  3. print('valid member')
  4. else:
  5. print('security alert! unknown member found')

'or' keyword is same as OR gate. It returns true if either statement is true. If you want that both / all conditions must be true then use 'and' ( same as AND gate) instead of 'or'.

  1. name = 'harish'
  2. age = 20
  3. country = 'India'
  4. if name == 'harish' and age == 20 and country == 'India':
  5. print('valid member')
  6. else:
  7. print('security alert! unknown member found')

If you want to use both 'or' and 'and' in same condition then use braces '()':

  1. name = 'harish'
  2. age = 20
  3. country = 'India'
  4. if (name == 'harish' and age == 21) or country == 'India':
  5. print('valid member')
  6. else:
  7. print('security alert! unknown member found')

Exercise:

Take a variable as mark and print the grade according to this table.

Min marksMax marksGrade
030F
3140E
4150D
5160C
6170C+
7180B+
8190A
91100A+


About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this tutorial, we will write our first python program. We will start from python interpreter and also learn how to...
First Python Program
Python is a general purpose, high-level, interpreted, interactive and object-oriented language. Easy to learn, read ...
Introduction to Python
Python has five standard data types: Numbers, Strings, Lists, Tuple and Dictionary.Every data type has its own proper...
Variable and Data Types in Python

Login or Sign up to leave comment.