When you write code or programming in any language, mostly you have to use If Else statement with condition. So, Here SKOTechLearn describes the easy learning way about IF Else Statement in Python. And There are also description of Types of Operators in Python and How to use Operators in Python?
Basically, If Else statement work on true and false condition.
But, before we start , we understand, how this statement works in Python by given following flow chart.
Suppose, If we ask our friend with a cup of Tea, then how will this condition apply?
Now, IF Else statement require different types of Operators.
Set Tkinter Center Screen Window in Python and Size and position of Window in Python
Basically, in condition require “Comparison Operators” and “Logical Operators”.
How to Use Operators in Python?
Example of If Else Statement with Comparison Operators in Python:
When you compile or run this script, the following output will be display:
Simple way to use Tkinter label in Python
Example of IF Else Statement with Comparison Operators in Python:
The following output will be display, when you run this python code:
Output:
Output:
So, Here, SKOTechlearn describe the example of If Else Statement in Python with Logical operator and comparison operators in python.
Basically, If Else statement work on true and false condition.
But, before we start , we understand, how this statement works in Python by given following flow chart.
Suppose, If we ask our friend with a cup of Tea, then how will this condition apply?
If ( <condition> ) :
<Statement>
else:
<Statement>
<Statement>
else:
<Statement>
Now, IF Else statement require different types of Operators.
Set Tkinter Center Screen Window in Python and Size and position of Window in Python
Basically, in condition require “Comparison Operators” and “Logical Operators”.
Comparison Operators in Python : | |
Name | Details |
== | Comparing Equal condition |
!= | Comparing Not Equal |
> | Comparing Value is Greater Than |
< | Comparing Value is Less Than |
>= | Comparing Value is Greater or Equal to |
<= | Comparing Value is Less or Equal to |
<> | Comparing Not Equal |
Logical Operators in Python : | |
Name | Details |
and | Statement with Two and more condition, and if all condition are true |
or | Statement with Two or more condition, and if any one condition are true |
not | Reverse condition if true then false and if false then true |
How to Use Operators in Python?
Example of If Else Statement with Comparison Operators in Python:
MyFstVl1 = 135
MySecVl2 = 234
#Condition1
if ( MyFstVl1 == MySecVl2 ):
print "Condition1 : MyFstVl1 is Equal to MySecVl2"
else:
print "Condition1 : MyFstVl1 is Not Equal to MySecVl2"
#Condition2
if ( MyFstVl1 != MySecVl2 ):
print "Condition2 : MyFstVl1 is Not Equal to MySecVl2"
else:
print "Condition2 : MyFstVl1 is Equal to MySecVl2"
#Condition3
if ( MyFstVl1 <> MySecVl2 ):
print "Condition3 : MyFstVl1 is Not Equal to MySecVl2"
else:
print "Condition3 : MyFstVl1 is Equal to MySecVl2"
#Condition4
if ( MyFstVl1 < MySecVl2 ):
print "Condition4 : MyFstVl1 is Less than MySecVl2"
else:
print "Condition4 : MyFstVl1 is Greater than MySecVl2"
#Condition5
if ( MyFstVl1 > MySecVl2):
print "Condition5 : MyFstVl1 is Greater than MySecVl2"
else:
print "Condition5 : MyFstVl1 is Less than MySecVl2"
#Condition6
if ( MyFstVl1 <= MySecVl2):
print "Condition6 : MyFstVl1 is Less than or equal to MySecVl2"
else:
print "Condition6 : MyFstVl1 is Not Less than or Not equal to MySecVl2"
#Condition7
if ( MyFstVl1 >= MySecVl2):
print "Condition7 : MyFstVl1 is greater or equal to MySecVl2"
else:
print "Condition7 : MyFstVl1 is Not greater or Not equal to MySecVl2"
When you compile or run this script, the following output will be display:
Condition1 : MyFstVl1 is Not Equal to MySecVl2
Condition2 : MyFstVl1 is Not Equal to MySecVl2
Condition3 : MyFstVl1 is Not Equal to MySecVl2
Condition4 : MyFstVl1 is Less than MySecVl2
Condition5 : MyFstVl1 is Less than MySecVl2
Condition6 : MyFstVl1 is Less than or equal to MySecVl2
Condition7 : MyFstVl1 is Not greater or Not equal to MySecVl2
Condition2 : MyFstVl1 is Not Equal to MySecVl2
Condition3 : MyFstVl1 is Not Equal to MySecVl2
Condition4 : MyFstVl1 is Less than MySecVl2
Condition5 : MyFstVl1 is Less than MySecVl2
Condition6 : MyFstVl1 is Less than or equal to MySecVl2
Condition7 : MyFstVl1 is Not greater or Not equal to MySecVl2
IF Else Statement Example with Comparision Operator |
Simple way to use Tkinter label in Python
Example of IF Else Statement with Comparison Operators in Python:
MyStr1 = "python"
MyStr2 = "Learn"
MyStr3 = "Programming"
if ( MyStr1 == "python" and MyStr2 == "Learn" ):
print "'And' Operator Output: " + MyStr1 + MyStr2
else:
print "'And' Operator Output: String Not Match"
if ( MyStr1 == "python" or MyStr1 == "PYTHON" ):
print "'Or' Operator Output: " + MyStr1
else:
print "'Or' Operator Output: String Not Found"
if not ( MyStr3 == "python" or MyStr3 == "PYTHON" ):
print "'Not' Operator Output: " + MyStr3 + " is not equal to python"
else:
print "'Not' Operator Output: " + MyStr3 + " is equal to python"
The following output will be display, when you run this python code:
'And' Operator Output : pythonLearn
'Or' Operator Output : python
'Not' Operator Output : Programming is not equal to python
'Or' Operator Output : python
'Not' Operator Output : Programming is not equal to python
IF Else Statement Example with Logical Operator in Python |
Nested If Else in python :
When you want to check condition and want to check another condition after previous condition, then we have to use Nested If Else. Let’s check following example: sk = 102
if (sk < 500):
sk = sk % 2
print ( sk )
if (sk == 0):
print ( "sk is even" )
if (sk > 100):
print ( "sk Greater than 100" )
else:
print ( "sk Less than 100" )
Output:
0
sk is even
sk Less than 100
sk is even
sk Less than 100
If Else ladder in Python (if elif else):
There is following example of Ladder: Vl = 35
if ( Vl == 12 ):
print ( "Vl equal to 12" )
elif ( Vl == 18 ):
print ( "Vl equal to 18" )
elif ( Vl == 35 ):
print ( "Vl equal to 35" )
else:
print ( "Vl Not equal to any" )
Vl equal to 35
So, Here, SKOTechlearn describe the example of If Else Statement in Python with Logical operator and comparison operators in python.
0 comments: