#Python (Basic) Logical Operators
------------------------------------------
OPERATOR | SYNTAX |
----------------------
and | a and b |
----------------------
#Example-----------------------------------
if condition1 and condition2 and condition3:
all_conditions_met == true
else:
all_conditions_met == false
------------------------------------------
OPERATOR | SYNTAX |
----------------------
or | a or b |
----------------------
#Example
if condition1 or condition2 or condition3:
any_condition_met == true
else:
any_condition_met == false
------------------------------------------
OPERATOR | SYNTAX |
----------------------
not | not a |
----------------------
if not equal_to_condition:
equal_to_condition == false
else:
equal_to_condition == true
>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}
>>> # OR, |
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1 # `s1` is unchanged
{'a', 'b', 'c'}
>>> # In-place OR, |=
>>> s1 |= s2
>>> s1 # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}
a = 3
b = 4
# Arithmetic Operators
print("The value of 3+4 is ", 3+4)
print("The value of 3-4 is ", 3-4)
print("The value of 3*4 is ", 3*4)
print("The value of 3/4 is ", 3/4)
# Assignment Operators
a = 34
a -= 12
a *= 12
a /= 12
print(a)
# Comparison Operators
# b = (14<=7)
# b = (14>=7)
# b = (14<7)
# b = (14>7)
# b = (14==7)
b = (14!=7)
print(b)
# Logical Operators
bool1 = True
bool2 = False
print("The value of bool1 and bool2 is", (bool1 and bool2))
print("The value of bool1 or bool2 is", (bool1 or bool2))
print("The value of not bool2 is", (not bool2))
children_above_five= True
have_good_health= True
if children_above_five and have_good_health:
print("Eligible to get admission in Primary school.")
children_above_five= True
have_good_health= False
if children_above_five or have_good_health:
print("Eligible to get admission in Primary school.")
children_above_five= True
have_good_health= False
if children_above_five and not have_good_health:
print("Eligible to get admission in Primary school.")
exactly on which containers can the in operator be applied?
Generally the rule of thumb is (not a fact but a doctrine):
if container is iterable => in can be applied
ex. of such containers- (str, list, tuple, set and dict)
#note1:
by iterable i mean we can iterate like this:
for x in list_object: or for y in dict_obj:
#note2:
not in operator just have opposite definition to that of in
but above rule of thumb is true for not in as well ofcourse.