All Courses

What are Conditions in Python?

Sulochana Kamshetty

a year ago

          Let’s understand this concept with the help of small story telling…..
There was a showroom manager who wanted to check all the sales during festivals and also wanted to know what will be the sales of the festivals with given conditions to check his financial conditions. So, let's help this manager, what all he can solve the issues with the help of conditions to come across with his solution.
Before that let's see why conditions are used, and what are different types of conditions are there in python program.
Conditions apply in real-time with examples
Definition:-  Python uses Boolean variables to evaluate conditions. The Boolean values true and false are returned when an expression is compared or evaluated.
Types of conditions:-
1.       Python if else condition
2.       Indentation
3.       Elif
4.       Else
5.       Short hand if
6.       Short hand if else
7.       And
8.       Or
9.       Nested if
10.   Pass statement
Now let’s check each and every type of conditions and will even check how the manager has used it…
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and "loops".
An "if statement" is written by using the if keyword.
If statement:
Sales =&nbsp;66%
off sales =&nbsp;200%
if&nbsp;b &gt; a:
&nbsp;&nbsp;print("off sales&nbsp; is greater
than&nbsp; sales")

Output :- &nbsp;off
sales&nbsp; is greater than&nbsp; sales
In this example, we use two variables, sales and off sales, which are used as part of the  if statement to test whether off sales is greater than sales. As sales are 66%, and sales are 200%, we know that 200% is greater than 66%, and so we print to screen that "off sales is greater than sales".
Python relies on indentation (whitespace at the beginning of a line or extra space mentioned after the code) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
If statement, without
indentation (will raise an error):

Sales &nbsp;=&nbsp;33%
offsales =&nbsp;200%
if&nbsp;b &gt; a:
print("off sales is greater than sales") &nbsp;# you will get an error
Since that we have used extra spaces in our code the python couldn’t gave the proper output with..
Let’s use sales as  A, and replace with off sales with b for our easy convenience.. 
3. ELIF:-
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
a = 33
b = 33
if b &gt; a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
Output:- In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".
4) ELSE:-
The else keyword catches anything which isn't caught by the preceding conditions.
a =&nbsp;200%
b =&nbsp;33%
if&nbsp;b &gt; a:
&nbsp;&nbsp;print("b is greater than a")
elif&nbsp;a == b:
&nbsp;&nbsp;print("a and b are equal")
&nbsp;&nbsp;print("a is greater than b")

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".
You can also have an else without the elif:
a =&nbsp;200
b =&nbsp;33
if&nbsp;b &gt; a:
&nbsp;&nbsp;print("b is greater than a")
&nbsp;&nbsp;print("b is not greater than a")

If you have only one statement to execute, you can put it on the same line as the if statement.
One line if statement:
if a > b: print("a is greater than b")
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
One line if else statement:

a =&nbsp;2
b =&nbsp;330
print("A")&nbsp;if&nbsp;a &gt; b&nbsp;else&nbsp;print("B")
This technique is known as Ternary Operators, or Conditional Expressions.
You can also have multiple else statements on the same line:
One line if else statement, with 3 conditions:

a =&nbsp;330
b =&nbsp;330
print("A")&nbsp;if&nbsp;a &gt; b&nbsp;else&nbsp;print("=")&nbsp;if&nbsp;a
== b&nbsp;else&nbsp;print("B")
The and keyword is a logical operator, and is used to combine conditional statements:
Test if&nbsp;a&nbsp;is greater than&nbsp;b, AND
if&nbsp;c&nbsp;is greater than&nbsp;a:

a =&nbsp;200
b =&nbsp;33
c =&nbsp;500
if&nbsp;a &gt; b and c &gt; a:
&nbsp;&nbsp;print("Both conditions are true")

The or keyword is a logical operator, and is used to combine conditional statements:
Test if&nbsp;a&nbsp;is greater than&nbsp;b, OR
if&nbsp;a&nbsp;is greater than&nbsp;c:

a =&nbsp;200
b =&nbsp;33
c =&nbsp;500
if&nbsp;a &gt; b or a &gt; c:
&nbsp;&nbsp;print("At least one of the conditions is true")

You can have if statements inside if statements, this is called nested if statements.
x =&nbsp;41

if&nbsp;x &gt;&nbsp;10:
&nbsp;&nbsp;print("Above ten,")
&nbsp;&nbsp;if&nbsp;x &gt;&nbsp;20:
&nbsp;&nbsp;&nbsp;&nbsp;print("and also above 20!")
&nbsp;&nbsp;&nbsp;&nbsp;print("but not above 20.")
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
a =&nbsp;33
b =&nbsp;200

if&nbsp;b &gt; a:
# CONCLUSION:-  these are few different conditionals  where manager has applied and found the difference of sales applied on different situations.
Like the Blog, then Share it with your friends and colleagues to make this AI community stronger. 
To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our insideAIML blog page.
Keep Learning. Keep Growing.

Submit Review