Python Conditional Statements

Hello guys....,

In this session of tutorials we will see what are Python Conditional Statements and How to use them in programming...

First of all its important to know what conditional statements means.... I would like to explain this with an example:

Now, In everyday life we all need to take some decisions based on conditions, like " If movie review is good, then i'll watch a movie" or you can just say, " If lecture is scheduled today i'll take my Book" and may such if statements.

This all decision making statements which are based on some conditions are called conditional statements.

Same kind of conditional statements are also used in Python Programming Language, they are

Python Conditional Statements, Types of Python Conditional Statements
Python Conditional Statements

  1. IF statement
  2. IF ELSE statement
  3. IF ELIF (IF-ELSE-IF) statement 
  4. NESTED IF statement

We will be seeing all this statements and how to use them in programming in details.

IF statement

This statement is used to execute one or more statement depending on weather the given condition is True or False.

The syntax is given as:

if condition:
    statement (s)
    
The given condition is tested first, if the condition is True then the statement (s) after colon (:) is executed.

And if the condition is false then the statements are not executed.

Lets see this with an example:

When if condition is True:


Python Conditional Statement, Python if conditional statement
Python If statement

#Example when if condition is true

x = 5
y = 10

if (x<y):
    out = "x is less than y"

print(out)
  •  step 1: We have defined two variables x and y which stores integer value of 5 and 10 respectively.
  • Program is to find whether x is less than y or not.
  • step 2: Define if statement with a condition x<y, when this condition is True then only program will go to execute next statement.
  • step 3: The variable out is assigned a string "x is less than y". This will be saved only when if condition is true.
  • step 4: print statement is executed.

When if condition is False:

Python Conditional Statements, false if condition in python
False if condition
#Example when if condition is false

x = 10
y =  5

if (x<y):
    out = "x is less than y"

print(out)

  • Here we have used the same code as above but, only difference we did is, we changed the values of variable x and y.
  • Now, whenever Python runs if condition x<y, it will give False output.
  • As the condition is False, python will never go to next statement, and out variable will be declared.
  • So when python starts to execute print statement, it will not find out variable and it will give error as shown in figure.
To overcome this problem we use IF ELSE statement, so either one condition is true.

IF ELSE statement

else condition is always used when you have to test two conditions and they are interdependent, like IF condition is true the execute the statement below otherwise execute the statement under else condition.

The syntax of else statement is given as:


if condition:
    statement (s)

else:
    statement (s)

Example:

Taking same example as above we will see how to execute if else statement in python.


Python Conditional Statements, if else statement in python
if else statement in Python
#Example using if else condition 

x = 10
y =  5

if (x<y):
    out = "x is less than y"
else:
    out = "x is greater than y"

print(out)

  • Here when if condition x < y becomes false, python will not execute statement below it, but jumps to next line,i.e.; else statement
  • Else statement is executed whenever if condition is false. In this statement it will declare out variable and assign  a string value "x is greater than y".
  • Then print statement is executed and gives the proper output.


When does both condition does not work

Now, you may have understood the statement under if condition will not be executed when the condition is False. And when if condition is not satisfied else statements are always executed in any conditions, but some time it may happen even else statement may give wrong results. 

This happens only when logic written in condition place is wrong.

This can be properly explained with an example:


Python Conditional Statement, When does both condition does not work
When does both condition does not work

#Example when if else condition gives wrong answer

x = 10
y = 10

if (x<y):
    out = "x is less than y"
else:
    out = "x is greater than y"

print(out)

  • In both the above examples we have seen either x is greater than y or vise versa.
  • But if both x and y has same values, python will never recognize that, as it only checks if condition and if it is false, it always executes else condition.
  • So, to overcome this problem we need to use two conditional statements, that will be discused below.

IF ELIF statement

Sometimes you may have to test more than one or two conditions, in such cases the if...elif...else statement are used.

Even to over come problem in else statement discussed above can be overcome by this satement.

Syntax:


if condition:
    statement (s)
elif condition:
      statement (s)

else:
    statement (s)

Example:


Python Conditional Statements, if elif else statements
if--elif--else statements in Python

#Example using if--elif--else statements

x = 10
y = 10

if (x<y):
    out = "x is less than y"

elif (x==y):
      out = "x is equal to y"
else:
    out = "x is greater than y"
print(out)


  • we have defined two variables X and Y having same values.
  • The if statement checks whether X is less than Y and executes the statement under it.
  • When if condition is false Python goes to next line where a elif condition is given and checks whether the condition is true or false.
  • If the condition is true it will execute the statement otherwise it will go for else statement.

In this way the problem from above example can be solved.

Minimal code for conditional statement

Instead of writing separate code for each conditions, python provides minimal way to execute a code in singe line.

Syntax:

statement if condition else statement  

Now in this statement before if is executed only when condition after if is True, otherwise statement after else is executed.

This can be well understood with below example


Python Conditional Statements, Minimal code for conditional statement
Minimal code for conditional statement
#Minimal code for conditional statement

x = 5
y = 10

out = "x is less than y" if (x < y) else "x is greater than or equal to y"


print(out)

  • We have defined two variables X and Y having values of 5 and 10.
  • Variable out is set to "X is less than Y" if x<y or else "x is greater than or equal to y"
  • When the condition given after if is true then statement before if is executed otherwise statement after else is executed.
  • Based on the value of variable out the print statement gives the output in Python shell

NESTED IF statement

If you want to test condition under condition then you can use nested if statement in your code.

Syntax:


if condition:
    if condition:
        statement (s)
elif condition:
      statement (s)

else:
    statement (s)

Let's see an example

Python Conditional Statement, Nested if statement in Python
Nested if statement in Python
#nested if statement

x = 5
y = 10
z = 2

if(z==2):
    if(x<y):
        out="x is less than y"
     
elif(x==y):
      out="x is equal to y"
else:
    out="x is greater than y"


print(out)


  • we have defined three variables X Y and Z having three different values of 5,10 and 2 respectively.
  • We can see two if conditions is written below one another, if first if condition Z==2 is true then Python goes to next line to check next if condition.
  • If both conditions are true then only the statement under if condition is executed.
  • Both the if conditions are false Python checks next condition under elif statement.
  • Even if this condition is false Python will execute else statement and variable out is assign the value.
Make sure to practice below examples for more clearance of the topic.

Practice Examples


my_string = "hello there"
if "th" in my_string:
    print('yes')
else:
    print('no')

if "a" in "there" or 6 % 2:
    print('true')
else:
    print('false')

my_list = [ 'dog', 'cat', 'worm', 'cow']
if 'dog' not in my_list:
    print('true')
else:
    print('false')


Summary:

A Python conditional statement is executed by if statements and we saw various other types of conditional statements like if and else.
  • "if condition" – It is used when you need to print out the result when one of the conditions is true or false.
  • "else condition"- it is used when you want to print out the statement when your one condition fails to meet the requirement
  • "elif condition" – It is used when you have multiple possibility as the outcome. 
  • We can use minimal code to execute conditional statements by declaring all condition in single statement to run the code
  • If Statement can be nested
Hope this session was interesting....

Thank you...

Comments

Popular Post

Python Data Types

Python String

Python List