|
| 1 | +# Python Booleans, Comparison and Logical Operators |
| 2 | + |
| 3 | +**Video link:** |
| 4 | + |
| 5 | +In this video, we learned about the boolean data type as well as comparison and logical operators in Python. |
| 6 | + |
| 7 | +**Programs in the Video** |
| 8 | + |
| 9 | +- [Boolean Data Type](#boolean-data-type) |
| 10 | +- [Comparison Operators](#comparison-operators) |
| 11 | +- [Logical Operators](#logical-operators) |
| 12 | +- [**Task**: Guess the Output](#programming-task) |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Boolean Data Type |
| 17 | +Boolean is a logical data type that represents one of two values: either `True` or `False`. |
| 18 | + |
| 19 | +```python |
| 20 | +result1 = True |
| 21 | +result2 = False |
| 22 | + |
| 23 | +print(result1) # True |
| 24 | +print(result2) # False |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Comparison Operators |
| 30 | +Python 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`. |
| 31 | + |
| 32 | +```python |
| 33 | +number = 5 |
| 34 | +print(number < 10) # True |
| 35 | + |
| 36 | +number = 15 |
| 37 | +print(number < 10) # False |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | +Here's a list of comparison operators: |
| 42 | + |
| 43 | +``` |
| 44 | +Comparison Operators: |
| 45 | +
|
| 46 | +< Less than |
| 47 | +> Greater than |
| 48 | +== Equal |
| 49 | +!= Not equal |
| 50 | +>= Greater than or equal to |
| 51 | +<= Less than or equal to |
| 52 | +``` |
| 53 | + |
| 54 | +## Examples: Comparison Operators |
| 55 | + |
| 56 | +```python |
| 57 | +# comparison operators in action |
| 58 | + |
| 59 | +number = 15 |
| 60 | +print(number > 10) # True |
| 61 | + |
| 62 | +number = 10 |
| 63 | +print(number > 10) # False |
| 64 | + |
| 65 | +number = 10 |
| 66 | +# equal to |
| 67 | +print(number == 10) # True |
| 68 | + |
| 69 | +number = 10.0 |
| 70 | +# comparing float and integer |
| 71 | +print(number == 10) # True |
| 72 | + |
| 73 | +number = '10' |
| 74 | +# comparing string and integer |
| 75 | +print(number == 10) # False |
| 76 | + |
| 77 | +number = '10' |
| 78 | +# not equal to |
| 79 | +print(number != 10) # True |
| 80 | + |
| 81 | +number = 10 |
| 82 | +# less than or equal to |
| 83 | +print(number <= 10) # True |
| 84 | + |
| 85 | +number = 10 |
| 86 | +# greater than or equal to |
| 87 | +print(number >= 10) # True |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | + |
| 93 | +## Logical Operators |
| 94 | +Python also has three logical operators that operate on the boolean values. Here's a list of the logical operators: |
| 95 | + |
| 96 | +``` |
| 97 | +Logical Operators |
| 98 | +
|
| 99 | +and True if both operands are True |
| 100 | +or True if either of the operands is True |
| 101 | +not True if the operand is False |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +### `and` Operator |
| 107 | + |
| 108 | +If both of the expressions are true, then the result should be true. |
| 109 | + |
| 110 | +```python |
| 111 | +age = 22 |
| 112 | +gpa = 3.8 |
| 113 | + |
| 114 | +result = age >= 18 and gpa > 3.6 |
| 115 | +print(result) # True |
| 116 | +``` |
| 117 | + |
| 118 | +However, if either of these expressions is false, |
| 119 | + |
| 120 | +```python |
| 121 | +age = 22 |
| 122 | +gpa = 3.8 |
| 123 | + |
| 124 | +print(age >= 18 and gpa > 3.9) # false |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### `or` Operator |
| 130 | +If either of the expression is true, then the result should be true. |
| 131 | +```python |
| 132 | +age = 22 |
| 133 | +gpa = 3.8 |
| 134 | + |
| 135 | +print(age >= 18 or gpa > 3.9) |
| 136 | +``` |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +### `not` Operator |
| 141 | +The not operator gives the complement of the result: |
| 142 | + |
| 143 | +* If `True`, result is `False`. |
| 144 | +* If `False`, result is `True`. |
| 145 | + |
| 146 | +```python |
| 147 | +result = True |
| 148 | +print(result) # True |
| 149 | + |
| 150 | +result = True |
| 151 | +print(not result) # False |
| 152 | +``` |
| 153 | +--- |
| 154 | + |
| 155 | +## Programming Task |
| 156 | + |
| 157 | +**Can you guess the output of this program?** |
| 158 | + |
| 159 | + |
| 160 | +```python |
| 161 | +language = "Python" |
| 162 | +print("1.", language == "python") |
| 163 | + |
| 164 | +age = 18 |
| 165 | +print("2.", age >= 18) |
| 166 | +print("3.", age > 18) |
| 167 | + |
| 168 | +print("4.", age >= 18 and language == "Java") |
| 169 | +``` |
| 170 | + |
| 171 | +**Output** |
| 172 | + |
| 173 | +``` |
| 174 | +1. False |
| 175 | +2. True |
| 176 | +3. False |
| 177 | +4. False |
| 178 | +``` |
0 commit comments