• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

Conditional structures of a programming language perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. In Python, conditional statements are handled by if statements.








In this tutorial, we will see how to apply conditional statements in Python.

How to use "if condition" in conditional structures

When you want to justify one condition while the other condition is not true, then you use "if statement".

Syntax:

If expression
     Statement
else: 
     Statement

Let see an example-

  • Code Line 5: We define two variables x, y = 2, 8
  • Code Line 7: The if Statement checks for condition x<y which is True in this case
  • Code Line 8: The variable st is set to "x is less than y."
  • Code Line 9: The line print st will output the value of variable st which is "x is less than y",

Python Conditional Statements: IF, ELIF, ELSE Nested IF

What happen when "if condition" does not meet

In this step, we will see what happens when your "if condition" does not meet.

  • Code Line 5: We define two variables x, y = 8, 4
  • Code Line 7: The if Statement checks for condition x<y which is False in this case
  • Code Line 8: The variable st is NOT set to "x is less than y."
  • Code Line 9: The line print st - is trying to print the value of a variable that was never declared. Hence, we get an error.

Python Conditional Statements: IF, ELIF, ELSE Nested IF

How to use "else condition"

The "else condition" is usually used when you have to judge one statement on the basis of other. If one condition goes wrong, then there should be another condition that should justify the statement or logic.

Example:

  • Code Line 5: We define two variables x, y = 8, 4
  • Code Line 7: The if Statement checks for condition x<y which is False in this case
  • Code Line 9: The flow of program control goes to else condition
  • Code Line 10: The variable st is set to "x is greater than y."
  • Code Line 11: The line print st will output the value of variable st which is "x is greater than y",

Python Conditional Statements: IF, ELIF, ELSE Nested IF

When "else condition" does not work

There might be many instances when your "else condition" won't give you the desired result. It will print out the wrong result as there is a mistake in program logic. In most cases, this happens when you have to justify more than two statement or condition in a program.

An example will better help you to understand this concept.

Here both the variables are same (8,8) and the program output is "x is greater than y", which is WRONG. This is because it checks the first condition (if condition), and if it fails, then it prints out the second condition (else condition) as default. In next step, we will see how we can correct this error.

Python Conditional Statements: IF, ELIF, ELSE Nested IF

How to use "elif" condition

To correct the previous error made by "else condition", we can use "elif" statement. By using "elif" condition, you are telling the program to print out the third condition or possibility when the other condition goes wrong or incorrect.

Example

  • Code Line 5: We define two variables x, y = 8, 8
  • Code Line 7: The if Statement checks for condition x<y which is False in this case
  • Code Line 10: The flow of program control goes to the elseif condition. It checks whether x==y which is true
  • Code Line 11: The variable st is set to "x is same as y."
  • Code Line 15: The flow of program control exits the if Statement (it will not get to the else Statement). And print the variable st. The output is "x is same as y" which is correct

Python Conditional Statements: IF, ELIF, ELSE Nested IF

How to execute conditional statement with minimal code

In this step, we will see how we can condense out the conditional statement. Instead of executing code for each condition separately, we can use them with a single code.

Syntax

	A If B else C

Example:

  • Code Line 2: We define two variables x, y = 10, 8
  • Code Line 3: Variable st is set to "x is less than y "if x<y or else it is set to "x is greater than or equal to y". In this x>y variable st is set to "x is greater than or equal to y."
  • Code Line 4: Prints the value of st and gives the correct output

Python Conditional Statements: IF, ELIF, ELSE Nested IF

  • Instead of writing long code for conditional statements, Python gives you the freedom to write code in a short and concise way.

Nested IF Statement

Following example demonstrates nested if Statement

total = 100
#country = "US"
country = "AU"
if country == "US":
    if total <= 50:
        print "Shipping Cost is  $50"
elif total <= 100:
        print "Shipping Cost is $25"
elif total <= 150:
	    print "Shipping Costs $5"
else:
        print "FREE"
if country == "AU": 
	  if total <= 50:
	    print "Shipping Cost is  $100"
else:
	    print "FREE"

Uncomment Line 2 in above code and comment Line 3 and run the code again

Summary:

A conditional statement in Python is handled by if statements and we saw various other ways we can use conditional statements like if and else over here.

  • "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 third possibility as the outcome. You can use multiple elif conditions to check for 4th,5th,6th possibilities in your code
  • 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

 

YOU MIGHT LIKE: