MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 3 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python9 min · Section 3 of 12

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:

OperatorNameExampleResult
+Additionn1 + n211
-Subtractionn1 - n2-1
*Multiplicationn1 * n230
/True division (always gives a float)n1 / n20.8333333333333334
//Floor division (rounds down)n1 // n20
%Remainder (modulus)n1 % n25
**Powern1 ** n215625

// 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 &&, ||, !).

aba and ba or bnot a
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

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):

OperatorNameExampleResult
&ANDn1 & n212 (0000 1100)
|ORn1 | n261 (0011 1101)
^XORn1 ^ n249 (0011 0001)
~NOT (complement)~n1−61, because ~x equals -(x + 1)
<<Left shiftn1 << 2240 (multiplies by 2²)
>>Right shiftn1 >> 215 (floor-divides by 2²)

Precedence

From highest to lowest:

LevelOperators
1( ) parentheses
2** (right-associative: 2 ** 3 ** 2 is 2 ** 9 = 512)
3Unary +x, -x, ~x
4*, /, //, %
5+, -
6<<, >>
7&
8^
9|
10Comparisons, in, not in, is, is not
11not
12and
13or

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/or short-circuit and return an operand; falsy values are 0, empty and None.
  • Bitwise operators act on binary digits; ~x is -(x + 1).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the value of 17 // 5 + 17 % 5?

Show answer

Answer: B. 5

17 // 5 is 3 and 17 % 5 is 2; their sum is 5.

Question 2 · multiple choice

What is 2 ** 3 ** 2?

Show answer

Answer: C. 512

** is right-associative: 3 ** 2 = 9 first, then 2 ** 9 = 512. Left-to-right would give 64.

Question 3 · multiple choice

What does this program print?

Python · quiz_logic.py
name = ""
print(name or "Guest", 5 and 0)
Show answer

Answer: A. Guest 0

The empty string is falsy, so or returns its second operand “Guest”; 5 is truthy, so and returns its second operand 0.

Question 4 · multiple choice

Which operator has the lowest precedence?

Show answer

Answer: D. or

The logical operators come last, in the order not, and, or, so or is evaluated last.

Question 5 · multiple choice

What is 12 & 10?

Show answer

Answer: C. 8

12 is 1100 and 10 is 1010; only the bit worth 8 is 1 in both. 12 | 10 would be 14 and 12 ^ 10 would be 6.

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.

↑ ↓ move↵ openTab switch type