Python Core – Class 2 Notes
Topics Covered
Running Python files
What happens when we run a Python program
Functions and indentation
Importing modules
__pycache__ and .pyc files
Bytecode and the Python Virtual Machine (PVM)
Understanding Core Python
Q. Why is Python called a “straightforward and stable” language?
Ans:
Python behaves the same way in every environment (Windows, Linux, Mac).
It is maintained by the Python community at [Link].
After the migration from Python 2 to 3, it became very stable and backward-
compatible.
Q. Why is Python called an interpreted language?
Ans:
Python code (.py) is not converted directly into machine code.
Instead, it is first compiled into bytecode (a .py file becomes a .pyc file).
The Python Virtual Machine (PVM) then executes this bytecode line by line
(sequentially) at runtime.
This process of executing code line by line is why Python is called
an interpreted language.
It also makes Python easier to test and run without a separate compile step.
Q. What is the role of the Python interpreter?
Ans:
It reads and executes Python source code.
It compiles .py files into bytecode (.pyc files) and passes them to the PVM.
It handles errors, exceptions, and memory management during execution.
Q. What is meant by Python being dynamically typed?
Ans:
You don’t need to declare variable types explicitly.
The type is assigned automatically at runtime based on the value stored.
Example:
a = 10 # int
a = "Hi" # now string
Running a Python File
Q. What happens when we type python [Link]?
Ans:
Behind the scenes:
The terminal sends the command to the Operating System (OS).
The OS finds the location of [Link] using the PATH variable.
The Python interpreter starts and does the following:
Reads the source code from the file
Compiles it into bytecode (.pyc)
Sends the bytecode to the PVM (Python Virtual Machine)
The PVM executes the bytecode line by line.
The output appears in the terminal.
Q. Write a function greet that prints any value given to it.
def greet(n):
print(n)
Q. What is indentation in Python? Why is it important?
Ans:
Indentation means leaving spaces before a line of code to show its structure.
It defines where a block of code starts and ends (in other language like c,
c++, java etc. they use {}to define the code block but python
uses indentation).
If indentation is missing, Python gives an IndentationError.
Example:
def greet(n):
print(n) # This line is indented, so it's part of the
greet function
greet("Hello") # This line is not indented, so it's
outside the function
Q. Why does Python create a __pycache__ folder?
Ans:
It stores the compiled bytecode (.pyc) files.
This makes the next run faster because Python doesn’t need to recompile the
code every time.
If deleted, it will be created again automatically.
Q. Why does the folder have such a strange name (__pycache__)?
Ans:
The underscores (__) mean it’s a special system folder, not meant for direct
editing.
Python uses such naming to signal: “Don’t touch this manually.”
Q. When is a .pyc file created?
Ans:
A .pyc file is created and stored on disk when a module is imported.
When you directly run a top-level script, the bytecode is created in memory,
not saved to disk — so you won’t see a .pyc file in that case.
Q. What happens to the .pyc file if we change the code in a Python file?
Ans:
If the source code changes, Python automatically recompiles it and updates
the .pyc file inside __pycache__.
Q. What is PVM?
Ans:
PVM stands for Python Virtual Machine.
It is the runtime engine that executes Python bytecode line by line.
This is why Python is called an interpreted language.