Video link: https://youtu.be/mrryXQnlYN8
In this video, we learned about the boolean data type as well as comparison and logical operators in Python.
Programs in the Video
Boolean is a logical data type that represents one of two values: either True or False.
result1 = True
result2 = False
print(result1) # True
print(result2) # FalsePython has a set of comparison operators that allow us to compare two values. If the comparison is right, we get True and if the comparison is wrong, we get False.
number = 5
print(number < 10) # True
number = 15
print(number < 10) # FalseHere's a list of comparison operators:
Comparison Operators:
< Less than
> Greater than
== Equal
!= Not equal
>= Greater than or equal to
<= Less than or equal to
# comparison operators in action
number = 15
print(number > 10) # True
number = 10
print(number > 10) # False
number = 10
# equal to
print(number == 10) # True
number = 10.0
# comparing float and integer
print(number == 10) # True
number = '10'
# comparing string and integer
print(number == 10) # False
number = '10'
# not equal to
print(number != 10) # True
number = 10
# less than or equal to
print(number <= 10) # True
number = 10
# greater than or equal to
print(number >= 10) # TruePython also has three logical operators that operate on the boolean values. Here's a list of the logical operators:
Logical Operators
and True if both operands are True
or True if either of the operands is True
not True if the operand is False
If both of the expressions are True, then the result is True.
age = 22
gpa = 3.8
result = age >= 18 and gpa > 3.6
print(result) # TrueHowever, if either of these expressions is False, the result is False.
age = 22
gpa = 3.8
print(age >= 18 and gpa > 3.9) # falseIf either of the expression is True, then the result is True. If both expressions are False, only then the result is False.
age = 22
gpa = 3.8
print(age >= 18 or gpa > 3.9)The not operator gives the complement of the result:
- If
True, result isFalse. - If
False, result isTrue.
result = True
print(result) # True
result = True
print(not result) # FalseCan you guess the output of this program?
language = "Python"
print("1.", language == "python")
age = 18
print("2.", age >= 18)
print("3.", age > 18)
print("4.", age >= 18 and language == "Java")Output
1. False
2. True
3. False
4. False