Logical Operators:

  • These operators are used to combine multiple conditions.
OperatorMeaningExampleResult
andLogical AND(5 > 3) and (2 < 4)True
orLogical OR(5 < 3) or (2 < 4)True
notLogical NOTnot (5 > 3)False

Comparison Operators:

  • These operators compare values and return True or False.
OperatorMeaningExampleResult
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal to5 >= 5True
<=Less than or equal to5 <= 3False
==Equal to5 == 5True
!=Not equal to5 != 3True

  • Example: Both comparisons need to be True for the if statement to be True.
python
1temperature = 75
2forecast = "rain"
3
4if temperature < 80 and forecast != "rain":
5    print("Go outside!")
6else:
7    print ("Stay inside!")
  • Output:
1Stay inside!