CSC 280 · Introduction to Computer Programming
Our notes
2 min readVariables
A variable is a named place in memory that holds a value that can change. Constants hold values that do not change (π = 3.14159).
Naming rules (most languages): start with a letter (or _), use only letters, digits and _, no spaces, not a keyword (if, while, print in some languages). Python is case-sensitive: Score and score are different. Use meaningful names: total_units, not tu.
Basic data types
| Type | Python | Example |
|---|---|---|
| Integer | int | 25, -3 |
| Real (floating point) | float | 3.14, 2.5e3 |
| Character/String | str | "A", "UNIPORT" |
| Boolean (logical) | bool | True, False |
Assignment
x = 5 means "store 5 in x" (the right side is worked out first, then stored). count = count + 1 adds one to count; it is not an equation.
Operators and precedence
| Operator | Meaning | Example (Python) | Result |
|---|---|---|---|
| + - × ÷ | add, subtract, multiply, divide | 7 / 2 | 3.5 |
| // | integer (floor) division | 7 // 2 | 3 |
| % | remainder (modulus) | 7 % 2 | 1 |
| pow(a, b) | power | pow(2, 3) | 8 |
Precedence: brackets first, then powers, then × ÷ // %, then + -; equal ones go left to right.
Worked example. 2 + 3 * 4 - 6 / 2 = 2 + 12 - 3.0 = 11.0. (10 - 4) / 2 * 3 = 3.0 * 3 = 9.0.
Relational operators: ==, !=, <, >, <=, >= give True/False. Logical: and, or, not.
Worked example. With a = 5, b = 10: (a > 3) and (b < 5) → True and False → False; not (a == 5) → False.
Input and output
In Python, input() always gives a string, so convert it to use it as a number:
length = float(input("Length in m: "))
width = float(input("Width in m: "))
area = length * width
print("Area is", area, "square metres")
Run with 4 and 2.5 → prints Area is 10.0 square metres.
- Formatting: print(f"{area:.2f}") prints 10.00 (two decimal places).
- Type conversion: int("12") → 12; float("3.5") → 3.5; str(40) → "40".
Mathematical formulas in code
Area of a circle: area = 3.14159 * r * r. Hypotenuse: c = math.sqrt(a * a + b * b) (after import math). Simple interest: I = P * R * T / 100.
Worked example. P = ₦50,000, R = 10, T = 2 years → I = 50000 × 10 × 2 / 100 = ₦10,000.
Exams usually ask: valid and invalid variable names; data types; evaluate expressions using precedence, // and %; write a program that reads values, computes a formula and prints the result.
Common mistake: adding two inputs without converting them: "3" + "4" gives "34", not 7.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Introduction to Python Programming, 1.3 Variables
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 1.3 Variables, CC BY-NC-SA 4.0 · licence
-
Introduction to Python Programming, 1.2 Input/output
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 1.2 Input/output, CC BY-NC-SA 4.0 · licence
-
Introduction to Python Programming, 1.5 Number basics
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 1.5 Number basics, CC BY-NC-SA 4.0 · licence
-
Introduction to Python Programming, 2.2 Type conversion
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 2.2 Type conversion, CC BY-NC-SA 4.0 · licence
-
3. An Informal Introduction to Python — Python 3.15.0 documentation
(opens docs.python.org in a new tab)
Watch
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.