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+


ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a technical core team member at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel