Comparison Operators
Comparison Operators
=
- is used for assignment
==
- is used for comparison
print(10 == 10) # Checks if 10 = 10 --> will print True
# String comparison
print('hello' == 'Hello') # Checks if hello = Hello --> will print False
Comparison operators take into consideration types as well:
print(5 == '5') # Checks if 5 = '5' --> will print False
# There are also certain types that are 'truthy' (kinda true)
print(5 = 5.00) # Checks if 5 = 5.00 --> will print True
print(5 = 5.01) # Checks if 5 = 5.01 --> will print False
Less then, greater then, less then or equal to, grater then or equal to:
print(5 < 10) # Checks if 5 < 10 --> will print True
print(5 > 10) # Checks if 5 > 10 --> will print False
print(5 <= 5) # Checks if 5 less then or equal to 5 --> will print True
print(5 >= 10) # Checks if 5 greater then or equal to 10 --> will print False
The bang operator - (Negation operator):
print(5 != 6) # Checks is 5 is NOT equal to 6 --> will print True
The 'or' condition:
print('hello' == 'Hello' or 5 == 5) # Checks if one of the conditions is correct and if it is it will print True
The 'and' condition:
print('hello' == 'Hello' and 5 == 5) # Checks if both conditions are correct, if they are not it will print False
To write this cleaner it is better for each condition to be wrapped in it's own paranthesis:
print(('hello' == 'Hello') and (5 == 5))
Evaluation of statements:
True + True = True
True + False = False
False + False = False
The 'not' operator:
# Not will do the oposite:
print( not False ) # Prints True
print(not True) # Prints False
All comparison operators result in a bool
value:
bool
value:Either True
Either False
Last updated