  #### Top Courses #### Machine Learning with Python & Statistics 4 (4,001 Ratings) 220 Learners

#### Webinars

More webinars
##### What are Conditions in python? Sulochana Kamshetty

10 months 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.
Example
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".
2:- INDENTATION
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.
Example
``````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".
Example
``````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.
Example
``````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")
else:
&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:
Example
``````a =&nbsp;200
b =&nbsp;33
if&nbsp;b &gt; a:
&nbsp;&nbsp;print("b is greater than a")
else:
&nbsp;&nbsp;print("b is not greater than a")

``````
5.) SHORT HAND IF..
If you have only one statement to execute, you can put it on the same line as the if statement.
Example
One line if statement:
if a > b: print("a is greater than b")
6.)SHORT HAND IF…ELSE
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
Example
``````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:
Example
``````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")
``````
AND:-
The and keyword is a logical operator, and is used to combine conditional statements:
Example
``````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")

``````
OR:-
The or keyword is a logical operator, and is used to combine conditional statements:
Example
``````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")

``````
NESTED IF:-
You can have if statements inside if statements, this is called nested if statements.
Example
``````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;else:
&nbsp;&nbsp;&nbsp;&nbsp;print("but not above 20.")``````
THE PASS STATEMENT:-
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.
Example
``````a =&nbsp;33
b =&nbsp;200

if&nbsp;b &gt; a:
&nbsp;&nbsp;pass``````
# CONCLUSION:-  these are few different conditionals  where manager has applied and found the difference of sales applied on different situations

#### Submit Review BOT
Agent(Online)
We're Online!

Chat now for any query