ICT582 Python Revision
🎯 Learning Objectives
πŸ“š Key Concepts

Arithmetic Operators

OperatorMeaningExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division (float)7 / 23.5
//Floor division (int)7 // 23
%Modulo (remainder)7 % 21
**Exponentiation2 ** 8256

Operator Precedence (PEMDAS)

Highest β†’ Lowest: ** β†’ * / // % β†’ + -

Same-level operators evaluate left to right. Use parentheses () to control order.

2 + 3 * 4      # = 14, NOT 20
(2 + 3) * 4    # = 20
2 ** 3 ** 2    # = 512 (right to left for **)

Integer vs Float Division

/ always returns a float. // returns an integer (floors the result).

10 / 3    # 3.3333... (float)
10 // 3   # 3 (int β€” drops decimal)
10 % 3    # 1 (remainder after dividing)

Trick: Use % to check if a number is even: n % 2 == 0

Type Conversion

Explicitly convert between types with built-in functions:

int("42")        # β†’ 42
int(3.9)         # β†’ 3 (truncates, does NOT round)
float("3.14")    # β†’ 3.14
float(5)         # β†’ 5.0
str(42)          # β†’ "42"
bool(0)          # β†’ False (0, "", None, [] are falsy)
bool(1)          # β†’ True (everything else is truthy)

Augmented Assignment

x = 10
x += 3   # same as x = x + 3  β†’ 13
x -= 2   # same as x = x - 2  β†’ 11
x *= 4   # same as x = x * 4  β†’ 44
x //= 5  # same as x = x // 5 β†’ 8

String Operations

Strings support concatenation (+) and repetition (*). You cannot add a string and a number directly.

"Hello" + " " + "World"   # "Hello World"
"Ha" * 3                   # "HaHaHa"
"Age: " + str(25)          # "Age: 25"  (must convert number)

# This causes a TypeError:
"Age: " + 25               # ERROR!

Mixed-Type Arithmetic

When you mix int and float in an expression, Python automatically promotes the result to float.

5 + 2.0   # 7.0 (float, not int)
5 * 2.0   # 10.0
πŸ’» Code Examples

Example 1 β€” Basic arithmetic

a = 17
b = 5
print(a + b)   # 22
print(a - b)   # 12
print(a * b)   # 85
print(a / b)   # 3.4
print(a // b)  # 3
print(a % b)   # 2
print(a ** b)  # 1419857

Example 2 β€” Temperature converter

celsius    = float(input("Enter Celsius: "))
fahrenheit = celsius * 9 / 5 + 32
print("Fahrenheit:", fahrenheit)

Example 3 β€” Even/odd check with modulo

n = int(input("Enter a number: "))
remainder = n % 2
print(remainder)   # 0 if even, 1 if odd
⚠️ Exam Focus
  1. Trace expressions manually β€” know that 7 // 2 = 3 and 7 % 2 = 1.
  2. Know that / gives float, // gives int β€” often tested.
  3. Type conversion: int(3.9) gives 3, not 4 β€” it truncates, not rounds.
  4. String + number causes a TypeError β€” you must use str(n) first.
  5. Know precedence: ** before *// before +/-.
❌ Common Mistakes
⚑ Quick Recap
← Topic 01: Introduction Topic 03: Conditions β†’