10.3Operators and Precedence
Covers IGNOU MCS-201 §10.6
Operators perform computations on operands. Python’s operators are close to C’s, with a few important differences: true division, a power operator, words instead of symbols for logic, and logical operators that return operands rather than only True or False.
Arithmetic operators
With n1 = 5 and n2 = 6:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | n1 + n2 | 11 |
- | Subtraction | n1 - n2 | -1 |
* | Multiplication | n1 * n2 | 30 |
/ | True division (always gives a float) | n1 / n2 | 0.8333333333333334 |
// | Floor division (rounds down) | n1 // n2 | 0 |
% | Remainder (modulus) | n1 % n2 | 5 |
** | Power | n1 ** n2 | 15625 |
// rounds towards minus infinity, so 7 // -2 is −4 (C’s integer division would give −3), and % takes the sign of the divisor, so -7 % 3 is 2.
Assignment operators
= assigns. Each arithmetic and bitwise operator has an augmented form: +=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, >>=. m += n1 means m = m + n1. Python has no ++ or -- operators.
Comparison (relational) operators
==, !=, >, <, >=, <= compare values and give True or False. Strings are compared character by character using Unicode code points ('A' is 65, 'Z' 90, 'a' 97), so uppercase letters come before lowercase. Comparisons can be chained: 0 < x < 10 means 0 < x and x < 10.
Python also has identity operators is, is not (are they the same object?) and membership operators in, not in (is the item in the collection?).
Logical operators
and, or, not (lowercase words, not &&, ||, !).
a | b | a and b | a or b | not a |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
Any value can be used as a condition. Falsy values are False, None, 0, 0.0, and empty collections ("", [], (), {}, set()); everything else is truthy. and and or short-circuit and return one of their operands: x and y returns x if x is falsy, otherwise y; x or y returns x if x is truthy, otherwise y. So 100 and 200 gives 200, and 0 or "default" gives "default".
Bitwise operators
They work on the binary form of integers. With n1 = 60 (0011 1100) and n2 = 13 (0000 1101):
| Operator | Name | Example | Result |
|---|---|---|---|
& | AND | n1 & n2 | 12 (0000 1100) |
| | OR | n1 | n2 | 61 (0011 1101) |
^ | XOR | n1 ^ n2 | 49 (0011 0001) |
~ | NOT (complement) | ~n1 | −61, because ~x equals -(x + 1) |
<< | Left shift | n1 << 2 | 240 (multiplies by 2²) |
>> | Right shift | n1 >> 2 | 15 (floor-divides by 2²) |
Precedence
From highest to lowest:
| Level | Operators |
|---|---|
| 1 | ( ) parentheses |
| 2 | ** (right-associative: 2 ** 3 ** 2 is 2 ** 9 = 512) |
| 3 | Unary +x, -x, ~x |
| 4 | *, /, //, % |
| 5 | +, - |
| 6 | <<, >> |
| 7 | & |
| 8 | ^ |
| 9 | | |
| 10 | Comparisons, in, not in, is, is not |
| 11 | not |
| 12 | and |
| 13 | or |
Because ** binds tighter than a unary minus on its left, -2 ** 2 is −4.
Key takeaways
- Arithmetic:
/true division,//floor division,%remainder,**power. - Comparisons return booleans and can be chained; strings compare by Unicode code points.
and/orshort-circuit and return an operand; falsy values are 0, empty andNone.- Bitwise operators act on binary digits;
~xis-(x + 1).
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain, with an example, why "apple" < "Banana" is False in Python.
Show worked answer
Strings are compared character by character using Unicode code points. The first characters are 'a' (97) and 'B' (66). Since 97 > 66, "apple" is greater than "Banana", so "apple" < "Banana" is False. To compare ignoring case, convert both first: "apple".lower() < "banana".lower() is True.