COS 102 · Problem Solving
Our notes
2 min readWhy Python
Python is free, reads almost like pseudocode, and runs on phones (with apps such as Pydroid) as well as laptops. CCMAS allows C, Python or similar; the ideas carry over.
Variables and assignment
A variable is a named place that holds a value. The = sign means "store the value on the right in the name on the left" (assignment), not "is equal to".
score = 65
name = "Ada"
score = score + 5
After the third line, score holds 70.
Data types
| Type | Example | Meaning |
|---|---|---|
| int | 42 | whole number |
| float | 3.75 | number with a decimal point |
| str | "UNIPORT" | text (a string) |
| bool | True, False | yes/no value |
Arithmetic
+ add, − subtract (typed as -), * multiply, / divide (always gives a float), // whole-number division, % remainder.
17 // 5 gives 3 and 17 % 5 gives 2.
Input and output
input() reads text typed by the user; convert it with int() or float() before doing sums. print() shows results.
Worked example: area of a rectangle
length = float(input("Length in m: "))
width = float(input("Width in m: "))
area = length * width
print("Area =", area, "square metres")
With 4.5 and 3 the program prints: Area = 13.5 square metres.
Worked example: naira change
A program that breaks an amount into ₦1,000 and ₦500 notes and the rest:
amount = int(input("Amount: "))
thousands = amount // 1000
rest = amount % 1000
fives = rest // 500
left = rest % 500
print(thousands, "x 1000,", fives, "x 500, and", left, "left")
For 3,700 it prints: 3 x 1000, 1 x 500, and 200 left. Check: 3,000 + 500 + 200 = 3,700 ✓.
Comments and names
A line starting with # is a comment, ignored by Python; use comments to explain why. Choose clear names (total_units, not tu). Names are case-sensitive: Score and score are different.
Errors you will meet
- Syntax error: the code breaks Python's rules (a missing bracket or colon). Python will not run it.
- Runtime error: it crashes while running (dividing by zero; int("abc")).
- Logic error: it runs but gives the wrong answer (using + where * was meant). Only testing finds these.
Exams usually ask
- Write a short program that reads inputs, computes and prints a result.
- State the output of a given fragment.
Common mistake
Forgetting that input() gives text: "2" + "3" is "23", not 5.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick 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.3 Variables
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 1.3 Variables, CC BY-NC-SA 4.0 · licence
-
Python (programming language)
(opens en.wikipedia.org in a new tab)
Wikipedia, Python (programming language), CC BY-SA 4.0 · licence
-
Variable (high-level programming language)
(opens en.wikipedia.org in a new tab)
Wikipedia, Variable (high-level programming language), CC BY-SA 4.0 · licence
-
OpenStax, Introduction to Python Programming (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Python Programming (whole free textbook), CC BY-NC-SA 4.0 · licence
-
NPTEL: The Joy of Computing using Python (IIT Ropar, whole course)
Short lessons for beginners: first programs, input, if conditions, loops and lists, through small games and puzzles. Video lectures, many hours in all: watch on Wi-Fi where you can.
(opens nptel.ac.in in a new tab)
NPTEL, NPTEL: The Joy of Computing using Python (IIT Ropar, whole course), CC BY-NC-SA 4.0 · licence
Watch
-
Python variables for beginners ❎
14 min · ≈ 63 MB · about ₦35
(opens youtube.com in a new tab)
-
Python Tutorial for Absolute Beginners #1 - What Are Variables?
25 min · ≈ 113 MB · about ₦63
(opens youtube.com in a new tab)
-
Python Input Function: Visually Explained | #Python Course 6
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
Python Variables - Python Tutorial for Beginners with Examples | Mosh
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
Lecture 2: Strings, Input/Output, and Branching
1 h 19 min · ≈ 356 MB · about ₦200
(opens youtube.com in a new tab)
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.