Python Basics and How It Runs: Interview Questions
45 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)"Is Python compiled or interpreted?" opens many Python rounds. A short, exact answer shows you know the real flow, not just the slogan. This lesson revises the basics with answers you can say in half a minute.
Recap: compiled or interpreted?
Bytecode is the compact instruction list that CPython builds from your source file. The Python virtual machine then runs it one instruction at a time.
Interviewers ask this to see if you know what really happens. Python first compiles source to bytecode, then a virtual machine runs that bytecode.
source = "price * 2"code = compile(source, "<demo>", "eval")print(type(code).__name__)price = 25print(eval(code))So the honest answer is both: compiled to bytecode, then interpreted.
Example 1 · See the two steps: compile, then run
code = compile("4 * 25", "<demo>", "eval")print(code.co_consts)print(eval(code))print(type(code).__name__)(100,) 100 code
What this code does
compileproduces a code object. Python folds4 * 25into the constant100at compile time, so the code object holds it.evalis the second step: the virtual machine runs the code object and gives100.
compile turns text into a code object (bytecode plus data). eval runs it. Imported modules are cached as .pyc files in __pycache__, so the compile step is skipped next time.
Recap: dynamic but strong typing
Dynamic typing means a name has no fixed type. The type belongs to the object, and Python checks it only when the line runs.
A name can point to an int now and a string later. But Python never silently mixes unrelated types.
amount = 100print(type(amount).__name__)amount = "hundred"print(type(amount).__name__)try: print(100 + "5")except TypeError as err: print(err)That is why Python is dynamically and strongly typed.
Example 2 · Dynamic and strong typing side by side
balance = 500print(type(balance).__name__)balance = "500"print(type(balance).__name__)print(balance * 2)try: print(balance + 2)except TypeError as err: print(err)int str 500500 can only concatenate str (not "int") to str
What this code does
- Rebinding
balancefrom int to str is fine: dynamic typing. "500" * 2repeats the text, but"500" + 2is refused. Python will not guess a conversion: strong typing.
Rebinding amount is allowed. Adding an int to a str is refused with a TypeError. Type hints such as amount: int document intent but are not enforced when the program runs.
Recap: PEP 8 and the entry point
PEP 8 is the official Python style guide. It sets snake_case names, four-space indents and a 79-character line as the shared habit of the community.
Teams follow one style so any file reads like the rest. Functions and variables use snake_case, classes use CapWords, constants use UPPER_CASE.
MAX_ORDERS = 3
def order_total(price, qty): return price * qty
if __name__ == "__main__": print(order_total(250, MAX_ORDERS))A file can also be run directly or imported, and __name__ tells which.
Example 3 · int never overflows, / always gives a float
big = 2 ** 100print(big)print(big + 1 - big)print(7 / 2, 7 // 2, 7 % 2)print(type(6 / 3).__name__)1267650600228229401496703205376 1 3.5 3 1 float
What this code does
- A Python
intgrows as large as memory allows, so there is no overflow. /always returns a float, even for6 / 3. Use//for floor division and%for the remainder.
Run directly, __name__ is "__main__", so the total prints. If another file imports this one, the block is skipped and only the definitions load.
Next step
Check your understanding
12 quick questions