Comparison operators: The following table shows the comparison operators used in Python to compare two values. Assume two variable x and y, then:
Operator | Name | Description | Example |
---|---|---|---|
> | Greater than | True if left operand is greater than the right | x > y |
< | Less than | True if left operand is less than the right | x < y |
== | Equal to | True if both operands are equal | x == y |
!= | Not Equal to | True if operands are not equal | x != y |
>= | Greater than or equal to | True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to | True if left operand is less than or equal to the right | x <= y |
Example -
a=5
b=2
print(a>b) # returns True because 5 is greater than 2
print(a<b) # returns False because 5 is not less than 2
print(a==b) # returns False because 5 is not equal to 2
print(a!=b) # returns True because 5 is not equal to 2
print(a>=b) # returns True because 5 is grater than, or equal to 2
print(a<=b) # returns False because 5 is not less than, or equal to 2
b=2
print(a>b) # returns True because 5 is greater than 2
print(a<b) # returns False because 5 is not less than 2
print(a==b) # returns False because 5 is not equal to 2
print(a!=b) # returns True because 5 is not equal to 2
print(a>=b) # returns True because 5 is grater than, or equal to 2
print(a<=b) # returns False because 5 is not less than, or equal to 2
True
False
False
True
True
False
False
False
True
True
False
Logical operators: The following table shows the logical operators used in Python to combine conditional statements. Assume x is a variable, then:
Operator | Description | Example |
---|---|---|
and | True if both statements are true | x > 5 and x < 8 |
or | True if one of the statements is true | x > 10 or x > 20 |
not | Reverse the result | not(x > 5 and x < 8) |
Example -
x=5
print(x>2 and x>1) # returns True because both conditions are true
print(x>4 or x<1) # returns True because one of the condition is true (x>4)
print(not(x>2 and x>1)) # returns False because not is used to reverse the result
print(x>2 and x>1) # returns True because both conditions are true
print(x>4 or x<1) # returns True because one of the condition is true (x>4)
print(not(x>2 and x>1)) # returns False because not is used to reverse the result
Output will be
True
True
False
True
False
Identity operators: Identity operators are used in Python to check if two variables (or objects) are identical or not. If identical, they are located on the same memory location. Assume two variable x and y, then:
Operator | Description | Example |
---|---|---|
is | True if both variables (or objects) are identical | x is y |
is not | True if the variables (or objects) are not identical | x is not y |
Example -
a=1
b=1
c="Neoogy"
print(a is b) # returns True because both variables are integers i.e identical and occupies same memory location
print(a is not c) # returns True because both variables are not identical and located in different memory location
b=1
c="Neoogy"
print(a is b) # returns True because both variables are integers i.e identical and occupies same memory location
print(a is not c) # returns True because both variables are not identical and located in different memory location
Output will be
True
True
True
« Previous Next »
0 Comments