Home » Scripting and Automation » Python » Using conditional statements, if and if-else in Python

Using conditional statements, if and if-else in Python

[addthis tool="addthis_inline_share_toolbox"]

When you are writing python program, sometimes you need to make certain checks on variables and based on which you need to take some decisions.. this same is called as Decision making statements in C programming Language.

In this post, we will show you how you can use simple conditional statements “if” and “if-else”

As we see in C, python also supports following conditional operators

  • x == y means “returns true if x is equals to y” otherwise “return false”
  • x != y means “returns true if x is not equals to y” otherwise “return false”
  • x < y means “returns true if x is less than y” otherwise “return false”
  • x > y means “returns true if x is greater than y” otherwise “return false”
  • x <= y means “returns true if x is less than or equals to y” otherwise “return false”
  • x >= y means “returns true if x is greater than or equals to y” otherwise “return false”

Now, lets use above conditional operators to show how you can use if and if-else to execute different code based on this conditions.

Using IF

$ vim if.py 
#!/usr/bin/env python

x = 10
y = 22

if x <= y :
    print ("x is less than or equal to y")

Notice in above code, how we have written the “if” statement.. in C we need to write the same statement as,

if (x <= y) 

Now, compared with C,

  • we don’t need to add open and close bracket () in python
  • another difference is we need to add colon ” : ” at the end of statement in python.
  • While in C program we can start the next line after “if” at any place but in python the next line has to be indented one more place after indentation of “if” statement.

If indentation is missing for the line after “if” we will get an error as below, when we try to run this code,

  File "if.py", line 7
    print ("x is less than or equal to y")
        ^
IndentationError: expected an indented block

if we execute this python code, now on console.. we can see that it will print the string “x is less than or equal to y” as x is set to 10 and y is set to 22.

$ python if.py 
x is less than or equal to y

Using IF-ELSE

$ vim ifelse.py 
#!/usr/bin/env python

x = 10
y = 22

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

As we have checked in “if’ when x = 10 and y = 22 i.e. 10 < 22, the code’s if statement “x <= y” will become true and “x is less than or equal to y” message will get printed.

Now, lets change values of x = 22 and y = 10 as below,

#!/usr/bin/env python

x = 22
y = 10

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

and now if we run this code, since 22 > 10 i.e. x > y , the first “if x <= y” returns false, hence code execution will go to “else”
and if will print “x is greater than y”

$ python ifelse.py 
x is greater than y

Leave a Comment