Is 10 / 3 the same as 10 // 3 in Python? No: one gives 3.3333333333333335 and the other gives 3. Small symbols make big differences, and Python follows strict rules about which operation happens first. Section 2.3 of the book covers all of it.
Chapter 2 of the PECTAA Computer Science 11 book is Python Programming. After variables, data types and input/output (section 2.2), section 2.3 covers operators and expressions: arithmetic, comparison, assignment and logical operators, expressions, and operator precedence. This note is for FSc Part 1 (1st year, Class 11) students in Lahore, Faisalabad, Sargodha, Rawalpindi, Multan, Gujranwala and the other Punjab boards. Gujrat district comes under the Gujranwala board. PECTAA writes the books. Your own board sets your paper.
What to know
Arithmetic operators (section 2.3.1). For a = 10 and b = 3:
| Operator | Meaning | Result |
|---|---|---|
| + | addition | 13 |
| â | subtraction | 7 |
| * | multiplication | 30 |
| / | division (always a float) | 3.3333333333333335 |
| // | floor division | 3 |
| % | modulus (remainder) | 1 |
| ** | exponentiation | 1000 |
Comparison operators (section 2.3.2). They compare two values and return True or False. For x, y = 10, 5: x > y is True, x < y is False, x == y is False, x != y is True, x >= y is True, x <= y is False. Note that == compares, while = assigns.
Assignment operators (section 2.3.3). = assigns the value on the right to the variable on the left. Compound operators combine arithmetic with assignment: a += b means a = a + b, and likewise -=, *=, /=, %= and **=.
Logical operators (section 2.3.4). and, or and not combine conditions and return Boolean values. With x = True and y = False: x and y is False, x or y is True, not x is False.
Expressions (section 2.3.5). An expression is a combination of variables, operators and values that produces a result. 3 + 4 gives 7; (3 + 4) * 2 gives 14, because parentheses control the order.
Operator precedence (section 2.3.6). Python, like mathematics, evaluates some operators before others:
- Parentheses () first: (3 + 2) * 4 = 20.
- Exponentiation ** next: 2 ** 3 = 8.
- *, /, % (and //) next: 4 * 3 = 12, 10 / 2 = 5.0, 11 % 3 = 2.
- + and â last: 5 + 2 = 7, 10 â 4 = 6.
The book's tip: parentheses make complex expressions clearer and force the order you want.
Key points
- / always gives a float; // drops the fractional part.
- Comparison and logical operators give True or False.
- a op= b is short for a = a op b, and it changes a each time.
- Precedence: parentheses â ** â * / // % â + â.
Worked examples
Example 1 (book Class Activity). Evaluate 10 + 3 * 2 ** 2 â 5 / 5.
- 2 ** 2 = 4.
- 3 * 4 = 12 and 5 / 5 = 1.0.
- 10 + 12 â 1.0 = 21.0 (a float, because / was used).
Example 2 (book Class Activity). Evaluate (10 + 3) * (2 ** (2 â 1)) / 5.
- Inner brackets: 10 + 3 = 13 and 2 â 1 = 1, so 2 ** 1 = 2.
- 13 * 2 = 26, then 26 / 5 = 5.2.
Example 3 (book assignment code, run in order). Start with a = 10, b = 5, and run each line one after another:
- a += b â 15
- a -= b â 10
- a *= b â 50
- a /= b â 10.0
- a %= b â 0.0
- a **= b â 0.0
The book's comments print 5, 2.0, 2 and 100000 for steps 2, 4, 5 and 6. Those values do not follow from running the lines in sequence; the list above is what Python actually prints.
Example 4 (comparison with logic). A student scores marks = 72. Evaluate marks >= 50 and marks < 80.
- marks >= 50 is True.
- marks < 80 is True.
- True and True gives True, so the student is in the 50 to 79 band.
- If marks were 85, the second test would be False, and the whole expression would be False.
Common mistakes in the exam
- Writing two assignments with a comma.
a= 10, b=3is a SyntaxError. Usea = 10andb = 3on separate lines, ora, b = 10, 3. - Confusing = with ==. = assigns; == compares.
- Expecting 10 / 2 to give 5. It gives 5.0.
- Ignoring precedence. 2 + 3 * 4 is 14, not 20.
- Using an undefined name. In the book's logical NOT code, the result is stored as
logical_not_xbut printed aslogical_not, which raises a NameError; printlogical_not_x.
Exam technique
- For output questions, trace each line and keep track of every variable's current value.
- Show precedence steps one operator at a time.
- Write True and False with capital letters and no quotes.
- Check your board's notice for the sections in your paper.
Practice questions
- What is 17 // 5 and 17 % 5? Answer: 3 and 2.
- What does
print(4 == 4.0)show? Answer: True. - What is
not (5 > 3)? Answer: False. - Evaluate 2 + 3 * 4 ** 2. Answer: 4 ** 2 = 16, 3 * 16 = 48, 2 + 48 = 50.
- x = 7; x *= 2; x -= 4. What is x? Answer: 14, then 10.
Ready to practise this? Continue in 1st Year Computer Science.