Topic 02 of 12 | π Lecture 2 β ICT582Topic02.pdf
Expressions, Variables & Types
Arithmetic operators, operator precedence, type conversion, and string expressions.
π― Learning Objectives
- Use arithmetic operators:
+,-,*,/,//,%,** - Understand operator precedence (order of operations)
- Convert between types using
int(),float(),str() - Perform string concatenation and repetition
- Use augmented assignment operators:
+=,-=, etc.
π Key Concepts
Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division (float) | 7 / 2 | 3.5 |
// | Floor division (int) | 7 // 2 | 3 |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Exponentiation | 2 ** 8 | 256 |
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
- Trace expressions manually β know that
7 // 2 = 3and7 % 2 = 1. - Know that
/gives float,//gives int β often tested. - Type conversion:
int(3.9)gives3, not4β it truncates, not rounds. - String + number causes a
TypeErrorβ you must usestr(n)first. - Know precedence:
**before*//before+/-.
β Common Mistakes
- Assuming
10 / 2 = 5(int) β in Python 3 it gives5.0(float). Use10 // 2for5. - Confusing
int(3.9) = 3(truncate) withround(3.9) = 4(round). - Trying
"Score: " + scorewithout converting β always usestr(score). - Missing parentheses:
a + b / cdivides first β use(a + b) / cif needed.
β‘ Quick Recap
/= float division,//= integer division,%= remainder.- Operator precedence:
**β* / // %β+ -. Use()to override. int()truncates (chops decimal),float()adds decimal,str()converts to text.- Strings use
+for joining and*for repeating. - int + float β float (automatic type promotion).