Learn How to Code with Python: Beginner to Advanced Guide
Comparison operators allow programs to evaluate relationships between values. In Python, these operators return a Boolean result: `True` or `False`. They are fundamental to decision-making structures like `if`, `while`, and conditional expressions. The equality operator `==` checks whether two values are the same. It does not assign a value; it compares them. For example, `5 == 5` returns `True`, while `5 == 3` returns `False`. The inequality operator `!=` checks whether two values are different. For example, `5 != 3` returns `True`, while `5 != 5` returns `False`. These operators form the foundation of logical reasoning in code. 0 True False None What does `10 == 10` return? Inequality operators compare the relative size of values. The operator `>` checks whether the value on the left is greater than the value on the right. For example, `7 > 3` evaluates to `True`. The operator `<` checks whether the value on the left is smaller than the value on the right. For instance, `2 < 5` evaluates to `True`, while `5 < 2` evaluates to `False`. These operators are essential in controlling program flow. They allow a program to make decisions based on numerical thresholds. 1 True False 4 What does `4 > 9` return? Python also supports inclusive comparison operators. The operator `>=` checks whether the left value is greater than or equal to the right value. For example, `5 >= 5` returns `True`. Similarly, the operator `<=` checks whether the left value is less than or equal to the right value. For example, `3 <= 7` returns `True`, and `7 <= 7` also returns `True`. Inclusive comparisons are useful when boundary conditions matter, such as checking minimum or maximum limits. 1 False True 6 What does `6 <= 6` return? Comparison operators work with numbers, but they also work with strings. When comparing strings using `==`, Python checks whether the sequences of characters are identical. For example, `"apple" == "apple"` returns `True`, while `"apple" == "Apple"` returns `False` because string comparison is case-sensitive. Case sensitivity means uppercase and lowercase letters are treated as different characters. 1 True False Error What does `"cat" == "Cat"` return? Inequality operators also work with strings. Python compares strings lexicographically, meaning it compares them character by character based on their Unicode values. For example, `"apple" < "banana"` returns `True` because "a" comes before "b" in character order. Understanding lexicographic comparison helps when sorting or filtering textual data. 1 False True Error What does `"dog" > "cat"` return? Comparison operators can be used inside conditional statements. An `if` statement executes a block of code only if its condition evaluates to `True`. For example: `if 5 > 3:` The code inside the block runs because the condition is `True`. If the comparison evaluates to `False`, the block is skipped. 1 True False 8 If `x = 8`, what does `x != 8` evaluate to? Python allows chained comparisons. This means you can combine comparisons in a single expression, such as `1 < x < 5`. This expression checks whether `x` is greater than 1 and less than 5 simultaneously. It is equivalent to writing `1 < x and x < 5`. Chained comparisons make code more readable and concise. 0 True False Error If `x = 3`, what does `1 < x < 5` return? Comparison operators can also be used with variables. When comparing two variables, Python evaluates their current values. For example: `a = 10` `b = 20` `a < b` evaluates to `True`. The result depends entirely on the stored values at the time of comparison. 0 True False None If `a = 15` and `b = 10`, what does `a >= b` return? Be careful not to confuse `=` with `==`. The single equals sign assigns a value to a variable, while the double equals sign compares two values. For example: `x = 5` assigns 5 to x. `x == 5` checks whether x is equal to 5. Using the wrong operator can lead to errors or unexpected behavior. 1 Assign a value Compare two values Print a value What is the purpose of `==` in Python? Comparison operators always return Boolean values. The result of any comparison will be either `True` or `False`. These Boolean values can be stored in variables. For example: `result = (4 < 9)` Now `result` holds `True`. Understanding that comparisons produce Boolean results is key to mastering control flow and logical reasoning in programming. 2 Integer String Boolean What type of value does a comparison operator return?