Swipe left or tap Next ▶ to turn the page.
Select any text to highlight it or add a note. Tap a diagram to enlarge it.
Python Core: Types, Strings and Collections
- ✔ Explain names, objects, types and mutability the way interviewers expect
- ✔ Predict the output of string, slicing and operator questions without running code
- ✔ Choose between list, tuple, dict and set and use their key methods correctly
- ✔ Write clean list, dict and set comprehensions and spot the classic copy and aliasing traps
How Python runs your code
Almost every Python technical round starts with "what happens when I run this file?", so a clear picture here makes every later answer easier.
When you type python3 app.py, Python does not turn your code straight into machine code the way a C compiler does. It works in two steps. First, the compiler inside Python reads your source file and turns it into bytecode, a compact list of simple instructions. Second, the Python Virtual Machine (PVM) reads that bytecode one instruction at a time and carries it out. The standard Python you download from python.org is called CPython, because it is written in C.
The .pyc files in a __pycache__ folder are saved bytecode; Python reuses them for imported modules whose source has not changed. This is why people say Python is "interpreted", even though there is a small compile step hidden inside.
flowchart LR A[app.py source] --> B[Compiler] B --> C[Bytecode .pyc] C --> D[Python Virtual Machine] D --> E[Output on screen]
What this means for you in an interview
- Python is dynamically typed: a name has no fixed type, the object it points to has a type.
- Python is strongly typed: it will not quietly add a number to a string.
"5" + 5raises aTypeError. - Errors such as a misspelt name appear only when that line actually runs, because names are looked up at run time.
The file has
print("start")on line 1 andprint(totl)on line 2, wheretotlis a typo.The compile step only checks grammar (syntax). A misspelt name is still valid grammar.
Line 1 runs and prints
start.Line 2 runs, Python looks up
totl, cannot find it, and raisesNameError.Answer:
startis printed, then a NameError.
A file has a missing closing bracket on line 10 and a
printon line 1.A missing bracket is a syntax problem, so the compiler stops before any bytecode exists.
Nothing runs, not even line 1.
Answer: a SyntaxError is shown and nothing is printed.
Names, objects and references
Most "tricky output" questions are really questions about names pointing at objects, so learn this picture well.
In Python, everything is an object: numbers, strings, lists, functions, even classes. Every object has three things:
| Property | Meaning | How to see it |
|---|---|---|
| Identity | Where the object lives in memory | id(x) |
| Type | What kind of object it is | type(x) |
| Value | The data it holds | print(x) |
A variable is just a name stuck onto an object, like a name tag. The statement a = 10 means "create (or find) the object 10 and tie the name a to it". It does not mean "put 10 inside a box called a". When you then write b = a, no copy is made. You simply tie a second name tag onto the same object.
a = [1, 2, 3]
b = a
b.append(4)
print(a)
print(a is b)
print(type(a).__name__)
[1, 2, 3, 4]
True
list
Because a and b point to the same list, changing the list through b is visible through a. The is operator checks identity: are these two names tied to the very same object?
x = 5tiesxto the object5.y = xtiesyto the same5.x = x + 1creates a new object6and moves the tagxto it.ywas never touched, so it still points to5.Answer:
print(x, y)shows6 5.
p = [10]andq = p.p = p + [20]builds a new list[10, 20]and movespto it.qstill points to the old[10].Answer:
print(p, q)shows[10, 20] [10].
After m = [1, 2], n = m, n += [3], what does print(m) show?
For lists, += changes the list in place (like extend), and m and n share that list, so m also shows [1, 2, 3].
Numbers and operators
Operator questions are quick marks in screening tests, but only if you know Python's own rules, which differ from C and Java.
Python has three main number types: int (whole numbers of any size), float (decimal numbers, stored in binary) and complex (like 3+4j). bool is a small sub-type of int, where True behaves like 1 and False like 0.
| Operator | Meaning | Example | Result |
|---|---|---|---|
/ |
True division, always float | 7 / 2 |
3.5 |
// |
Floor division | 7 // 2 |
3 |
% |
Remainder (modulo) | 7 % 3 |
1 |
** |
Power | 2 ** 10 |
1024 |
Python's int has no fixed size limit, so 2 ** 100 works perfectly. Floats, however, are stored in binary, so some decimals cannot be stored exactly.
print(7 / 2, 7 // 2, 7 % 2)
print(-7 // 2, -7 % 2)
print(2 ** 3 ** 2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2) == 0.3)
print(True + True, 10 * False)
3.5 3 1
-4 1
512
False
True
2 0
First find the floor:
-7 // 2 = -4(−3.5 rounded down).Use the rule: remainder = a − (a // b) × b.
−7 − (−4 × 2) = −7 + 8 = 1.
Answer: 1.
2 ** 3 ** 2groups from the right:2 ** (3 ** 2).3 ** 2 = 9.2 ** 9 = 512.Answer: 512, not 64.
Evaluate
5 + 3 * 2 ** 2.Power first:
2 ** 2 = 4.Then multiply:
3 * 4 = 12.Then add:
5 + 12.Answer: 17.
Keep going with the full course
Day 1 has 23 pages. You have read the first 3.
See what's inside →