Browse All Articles > Conditional statements in Python

The purpose of this article is to demonstrate how we can use conditional statements using Python.

Conditional statements for testing using Python is similar to any other programming language in that we write **if** and **else** statements to get the desired results based on certain conditions before acting on any decision.

To show how to use conditional statements with example in Python I am using Mint operating system which has Python version 2.7.6 and also Python version 3.4.0 but the conditional statements are not operating system dependent , so we can use any OS. This conditional statements works on earlier Python versions too.

**if** statements by ending with another **if** like we do in other programming languages, so I have only written the below lines and after three dots used TAB for my right indentation. Hit enter to check conditional statements with** less than** operators:

** if** and **else** statements using operators.

To make the conditional statements I am going to use one mathematical game just to make use of conditional statements. To calculate simple interest we need to know the principal which is the starting amount that we are going to use, the interest rate for the amount, and the time. The formula to calculate is**i = prt** .

**The final script:**

Now let’s go ahead and test the conditional statements in Python. And the below test shows how we can use if and else statements in python to implement additional logics.

To show how to use conditional statements with example in Python I am using Mint operating system which has Python version 2.7.6 and also Python version 3.4.0 but the conditional statements are not operating system dependent , so we can use any OS. This conditional statements works on earlier Python versions too.

```
Using username "sloba".
sloba@***.***.*.*'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic x86_64)
Welcome to Linux Mint
* Documentation: http://www.linuxmint.com
Last login: Sun Oct 11 18:25:32 2015 from ***.***.*.*
sloba@sloba-VirtualBox ~ $
loba@sloba-VirtualBox ~ $ python2.7
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
sloba@sloba-VirtualBox ~ $ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
```

Normally we use some basic operators to check the conditional statements in Python such as “<”,”<=”,”>”,”>=”,”=”, “! =”,<> . Let’s check how we can use these operators starting with a very simple example. We are going to set some variables to check the min and max conditions.
```
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a =2
>>> b =5
>>>
```

We can assign the variables as above or we can also assign them just using a comma separator just like below:
```
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a =2
>>> b =5
>>>
>>> a,b = 2,5
>>>
>>> print a
2
>>> print b
5
>>>
```

Now you can see that the values assigned to the variables are as what we assigned earlier. So let us check if min is less than print the value assigned to min.
```
>>>
>>> if a < b:
... print a, " is cannot be more than ",b
...
2 is cannot be more than 5
>>>
```

In Python we don’t have to end the ```
>>> if a < b:
... print a, " is cannot be more than ",b
...
```

Let us try with a false statements using the same variables and values from the above test. We know the variable “a” is less than variable “b” and cannot be greater, so now we will just change the “<” and place “>” and some text changed:
```
>>> if a>b:
... print a," is greater than ",b
...
>>>
```

From the above condition we see that the false statement is false where we only message would have printed for the TRUE statements. To check the else statement we can use the same variables.
```
>>> if a>b:
... print a," is greater than ",b
... else:
... print a," is not greater than ",b
...
2 is not greater than 5
>>>
```

The above example is shown how to useTo make the conditional statements I am going to use one mathematical game just to make use of conditional statements. To calculate simple interest we need to know the principal which is the starting amount that we are going to use, the interest rate for the amount, and the time. The formula to calculate is

- I means interest earned
- P means principal amount
- R is the rate of interest
- T is time

The interest earned by Sloba is 1,600 in rupees and the total amount including the principal is 41,600.00.

Let’s make this in a script named "simple_interest.py".
```
#!/usr/bin/python
#Author: Swadhin
# Date: 11 - Oct - 2015
# Purpose: Simple interest game in Python
print "--------------Simple interest game--------------"
print "------------------------------------------------"
#Below are the variables used to calculate the formula
principle = 40000
# rate 4 % taken as per the question
rate = 4
# time in year
time = 1
name = raw_input("What is your name:")
print "Welcome ", name
print "Here is the question: "
print "Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded."
answer = input("So the question would be how much he will save in 1 years. ")
si = (principle * rate * time) / 100
if answer == si:
print name, "you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year "
else :
print name, "your answer is not correct"
```

Now if we execute this as like below:
```
sloba@sloba-VirtualBox ~/Desktop/myscripts $ ls
simple_interest.py
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1400
Ray your answer is not correct
sloba@sloba-VirtualBox ~/Desktop/myscripts $
```

When I enter 1400 is get the response back by saying “Ray your answer is not correct”. Now let’s try with the correct answer to the simple interest question.
```
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1600
Ray you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year
sloba@sloba-VirtualBox ~/Desktop/myscripts $
```

Let's add something more to the existing script by saying if the range of the input is closer to the actual answer. If the actual answer is between 1500 and 1599 then will the script will print that the answer is very close and if the range is beyond 1600 then it will print “you have exceeded the actual value of the correct answer."
```
#!/usr/bin/python
#Author: Swadhin
# Date: 11 - Oct - 2015
# Purpose: Simple interest game in Python
print "--------------Simple interest game--------------"
print "------------------------------------------------"
#Below are the variables used to calculate the formula
principle = 40000
#rate 4 % taken as per the question
rate = 4
# time in year
time = 1
name = raw_input("What is your name:")
print "Welcome ", name
print "Here is the question: "
print "Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded."
answer = input("So the question would be how much he will save in 1 years. ")
si = (principle * rate * time) / 100
if answer == si:
print name, "you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year "
elif answer >=1500 and answer <=1599:
print name, "your answer is very close, so try again"
elif answer >1600:
print name, "you have exceeded the actual value of the correct answer, so try again"
else :
print name, "your answer is not correct"
```

Now let’s go ahead and test the conditional statements in Python. And the below test shows how we can use if and else statements in python to implement additional logics.

```
sloba@sloba-VirtualBox ~/Desktop/myscripts $ clear
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned intere st of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 500
Ray your answer is not correct
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned intere st of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1599
Ray your answer is very close, so try again
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned intere st of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1650
Ray you have exceeded the actual value of the correct answer, so try again
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1600
Ray you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year
sloba@sloba-VirtualBox ~/Desktop/myscripts $
```

We can see one running example on how we can use conditional statements in Python. Thanks for reading this article.
## Comments (0)