Introduction to Programming in Python final exam review
The topics, worked on a live board with an AI tutor.
Units
Values, types and expressions
Practice
What are the value and the type of "4" + "5", and of 4 + 5?Answer
With a = 6 and b = 2, then a = a * b and b = a - b, what does print(a, b) show?Answer
Booleans and conditionals
What exams ask
Every decision a program makes, whether to log a user in, stop a loop or charge a fee, rests on a condition, and a condition with the wrong operator or the wrong grouping gives the wrong answer for just the unusual inputs that tests tend to miss.
Practice
For year = 1600, what do year % 4 == 0, year % 100 == 0 and year % 400 == 0 give?Answer
What does the program print for score = 79?Answer
Loops
Practice
For n = 2 and count = 0, with while n < 50: n = n * 2 and count = count + 1, what does print(count, n) show?Answer
For k in range(4, 20, 5) with the same block, what is the last value of k?Answer
Functions
What exams ask
Programs beyond a screenful are only manageable as small functions with clear jobs; it is how every project, lab and exam question with "write a program that" is best approached, and testing pieces separately finds bugs where they are, not three calls away.
- Defining and calling functions
- Scope and the call stack
- Default and keyword arguments and docstrings
- Designing with functions
Practice
With def double(n): return n * 2, what does print(double(7) + 1) show?Answer
With def sq(x): return x * x and def f(a): return sq(a) + 1, how many frames are there at the deepest point of print(f(3)), and what does it print?Answer
3 frames, global, f and sq; it prints 10
Hint: The deepest point is while sq runs
Scope and the call stackStrings
What exams ask
Text is everywhere in programs, from names and dates to file lines and DNA, and pulling out parts of it by position is the first string skill; the off-by-one at a slice's stop is a classic exam trap, and the same indexing works on lists next unit.
Many text tasks have no ready-made method, such as checking a password's rules, cleaning names or validating codes, and the loop over characters with an accumulator is the pattern behind them; reversing and palindromes are classic first exam and interview questions.
Practice
name = "Grace". What are name[0], name[-1] and name[2]?Answer
s = "Python"; s.lower(); print(s); What is printed, and how do you fix it to print python?Answer
Lists and tuples
What exams ask
Almost every program keeps a collection of values, test scores, names, readings, and a list is Python's everyday way to hold one; most list bugs are an index one past the end, or a method whose result was used as if it were the list.
- Lists: indexing and mutation
- Traversing lists
- Aliasing and mutability
- Tuples and unpacking
- Nested lists and grids
- List comprehensions
Practice
For t = [10, 20, 30, 40, 50], what are t[-2] and t[len(t) - 1]?Answer
What does this print: total = 0, for n in [2, 4, 6], total = total + n * n, print(total)?Answer
Dictionaries and sets
Practice
With scores = {"Ana": 8, "Raj": 6}, what is scores["Raj"] + scores.get("Mei", 1)?Answer
What does len(set([4, 4, 7, 7, 7, 2])) give?Answer
Files and exceptions
What exams ask
Real programs meet typing mistakes, missing files and impossible values; catching the right exception keeps them running with a helpful message, and raising one makes a function refuse bad input clearly; reading and writing try and except is a standard exam question.
CSV is the plain format that spreadsheets, sensors and websites export, and a first real data task is almost always read rows, convert fields, and summarise by group; this read, convert, accumulate pattern is a standard exam and project question.
Practice
What are the lines of a file after write("x"), write("y\n") and write("z")?Answer
Write safe_float(text) that returns float(text), or 0.0 when the text is not a number.Answer
Try: return float(text), except ValueError: return 0.0; safe_float("2.5") gives 2.5 and safe_float("abc") gives 0.0
Hint: Float raises ValueError too
ExceptionsClasses and objects
What exams ask
Real programs share code between kinds of things, such as a savings account that is a bank account with extras, and the questions that matter are which method Python runs for a given object and what breaks when the parent's setup is skipped; both are standard exam traces.
Practice
After p = BankAccount("Dee", 5) and p.balance = p.balance + 10, what does print(p.owner, p.balance) show?Answer
With c = BankAccount("Cy", 10), then c.deposit(5) and c.deposit(5), what does c.balance show?Answer
Recursion
What exams ask
Recursion is how many problems are most naturally written, folders inside folders, trees, divide and conquer searches and sorts, and tracing a recursive call on the stack is a standard exam question in a first programming course.
Splitting a sequence into its first item and the rest is the pattern behind recursive code on lists, strings, files and trees, and writing reverse, sum or count this way is a standard exam question in a first programming course.
Practice
Using factorial, what is factorial(6)?Answer
Trace reverse("dog") and give each returned value.Answer
Searching and sorting
What exams ask
Searching is one of the most common things programs do, and binary search is the first algorithm where a smart idea turns a million steps into twenty; tracing low, mid and high and counting comparisons are standard exam questions.
Sorting is everywhere, and these two simple sorts are where students first trace an algorithm pass by pass and count its work; showing the list after each pass and counting comparisons are standard exam questions, and the quadratic count explains why faster sorts exist.
Practice
How many comparisons does linear search make for 91, and for 4?Answer
Give the list after each pass of selection sort on [5, 1, 4, 2].Answer
1, 5, 4, 2; then 1, 2, 4, 5; then 1, 2, 4, 5 unchanged
Hint: The second pass finds 2
Selection sort and insertion sortTesting and debugging
What exams ask
Code that passes a few happy examples often fails at a boundary or on empty input; writing tests for ordinary, edge and error cases is how programmers catch that before users do, and choosing such cases is a common exam and interview question.
Every programmer spends much of their time debugging, and a calm method, read the error, find the line, trace the values, fix one thing and retest, is what separates guessing from fixing; reading a traceback and tracing a loop are standard exam questions.
Practice
Write two ordinary tests for a function double(x) that returns 2 * x.Answer
For example assert double(3) == 6 and assert double(-4) == -8
Hint: Include a negative number
Testing with assert and test casesA traceback ends with IndexError, list index out of range, at return xs[3] where xs = [7, 8, 9]. What is wrong?Answer
Questions
What does the Introduction to Programming in Python review cover?
Every unit of the course, 12 in all. Pick one and Helio teaches it on the board, then asks you questions. Values, types and expressions; Booleans and conditionals; Loops; Functions; Strings; Lists and tuples; Dictionaries and sets; Files and exceptions; Classes and objects; Recursion; Searching and sorting; Testing and debugging.
Can it help the week before my final?
Yes. Pick a topic, and Helio explains it, checks you and goes over what you miss.
Is it the same as my class?
It follows the usual topics of Introduction to Programming in Python. For anything else on your syllabus, type the topic and Helio teaches that too.
How much is free?
30 minutes a month for one learner, with no card. Starter is free for 7 days with a card: cancel in time and pay nothing.



