← Python Developer
Day 1 · Python Core: Types, Strings and Collections
Python Developer · Crash Course
Day 1

Python Core: Types, Strings and Collections

⏱ About 90 minutes · 23 pages
By the end of today you will be able to
  • ✔ 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
  1. How Python runs your code
  2. Names, objects and references
  3. Numbers and operators

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" + 5 raises a TypeError.
  • Errors such as a misspelt name appear only when that line actually runs, because names are looked up at run time.
✍️ Example 1 — Why does this program print before it fails?
  1. The file has print("start") on line 1 and print(totl) on line 2, where totl is a typo.

  2. The compile step only checks grammar (syntax). A misspelt name is still valid grammar.

  3. Line 1 runs and prints start.

  4. Line 2 runs, Python looks up totl, cannot find it, and raises NameError.

  5. Answer: start is printed, then a NameError.

✍️ Example 2 — Which error comes first?
  1. A file has a missing closing bracket on line 10 and a print on line 1.

  2. A missing bracket is a syntax problem, so the compiler stops before any bytecode exists.

  3. Nothing runs, not even line 1.

  4. Answer: a SyntaxError is shown and nothing is printed.

Python Developer · Day 12 / 5

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 b list object [1, 2, 3] b = a → two names, one 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?

✍️ Example 3 — Re-binding is not changing
  1. x = 5 ties x to the object 5.

  2. y = x ties y to the same 5.

  3. x = x + 1 creates a new object 6 and moves the tag x to it.

  4. y was never touched, so it still points to 5.

  5. Answer: print(x, y) shows 6 5.

✍️ Example 4 — Same idea with a list
  1. p = [10] and q = p.

  2. p = p + [20] builds a new list [10, 20] and moves p to it.

  3. q still points to the old [10].

  4. Answer: print(p, q) shows [10, 20] [10].

🙋 Try it now

After m = [1, 2], n = m, n += [3], what does print(m) show?

Python Developer · Day 13 / 5

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
✍️ Example 5 — Work out -7 % 2
  1. First find the floor: -7 // 2 = -4 (−3.5 rounded down).

  2. Use the rule: remainder = a − (a // b) × b.

  3. −7 − (−4 × 2) = −7 + 8 = 1.

  4. Answer: 1.

✍️ Example 6 — Power is right-to-left
  1. 2 ** 3 ** 2 groups from the right: 2 ** (3 ** 2).

  2. 3 ** 2 = 9.

  3. 2 ** 9 = 512.

  4. Answer: 512, not 64.

✍️ Example 7 — Precedence check
  1. Evaluate 5 + 3 * 2 ** 2.

  2. Power first: 2 ** 2 = 4.

  3. Then multiply: 3 * 4 = 12.

  4. Then add: 5 + 12.

  5. Answer: 17.

Python Developer · Day 14 / 5
Free preview ends here

Keep going with the full course

Day 1 has 23 pages. You have read the first 3.

See what's inside →