Learning Python 01
Basics - Revision
What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its simple, readable syntax and powerful capabilities.
Python removes unnecessary complexity. Compare printing “Hello World” in different languages:
print('Hello World')#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}Python uses indentation (spaces) to define code blocks — no curly braces {} needed.
Python has thousands of ready-to-use libraries:
\(\begin{array}{|l|l|} \hline \textbf{Library} & \textbf{Purpose} \\ \hline \text{NumPy} & \text{Numerical computing} \\ \hline \text{Pandas} & \text{Data analysis} \\ \hline \text{TensorFlow} & \text{Machine learning} \\ \hline \text{Django} & \text{Web development} \\ \hline \text{Requests} & \text{HTTP calls} \\ \hline \end{array}\)How Python Code Actually Runs
This is important to understand — and very commonly asked in interviews.
You (Programmer)│
│
▼
Write Python code , this is called source code
Python Source Code (.py file) Example: print(”Hello World”)│
│
▼
Python compiler converts code into bytecode. Bytecode is stored in a pycache folder
Bytecode is a simple internal instruction format used by Python Bytecode is intermediate instructions Bytecode is not a machine code (0 's and 1's).It’s platform-independent. Byte Code is simple instructions which is done by python. It is just instructions for Python itself Example idea: LOAD_CONST “Hello World” CALL_FUNCTION print│
│
▼
Python Virtual Machine is the engine inside python ,reads and executes the bytecode
Python Virtual Machine (PVM) The PVM reads bytecode instructions and executes each bytecode instructions step-by-step. This is why Python is called “interpreted” — the PVM interprets and runs the bytecode at runtime, line by line.│
│
▼
PVM requests operations from the CPU
When the PVM needs to perform realwork,it asks the CPU (Central Processing Unit) to do it The CPU performs the actual work such as: • calculations • memory operations • displaying text The CPU already works using machine instructions (0s and 1s) internally The CPU executes these instructions and returns the result│
│
▼
Result is returned to the program
output : Hello World
Python Source Code (.py)
↓
Compiled into Bytecode (.pyc)
↓
Python Virtual Machine reads bytecode
↓
PVM requests operations from CPU
↓
CPU executes the operations
↓
Program Output
When a Python program runs, the source code is first compiled into bytecode.
The Python Virtual Machine then reads and executes the bytecode instructions, while the CPU performs the actual operations required to produce the output.
Interpreter vs Compiler — The Basics
Before we dive into Python, it’s important to understand *how* code actually runs. This is a very common interview question!
Compiler
A compiler translates your entire source code into machine code before running it. This means:
Errors are caught before the program runs
The output is a separate executable file
Examples: C, C++, Java (partially)
Interpreter
An interpreter translates and executes your code line by line, at runtime. This means:
Errors show up only when that specific line runs
No separate executable file is created
Examples: Python, JavaScript, Ruby
Where Python Fits
Python is often called an interpreted language, but technically it does both compilation and interpretation.
Execution process:
Python source code (.py)
↓
Compiled to bytecode
↓
Python Virtual Machine (PVM)
↓
Program outputInterview Answer tip:
Python is primarily an interpreted language. When you run a
.pyfile, Python first compiles it into bytecode, and then the Python Virtual Machine interprets and executes that bytecode.
Statically Typed vs Dynamically Typed Languages — The Basics
Before learning Python deeply, it’s important to understand how programming languages handle variable types.
This is also a very common interview question.
Statically Typed Languages
In statically typed languages, you must declare the data type of a variable when creating it.
Type checking happens at compile time.
This means:
The variable type must be explicitly defined
A variable cannot change its type
Errors related to type are caught before the program runs
Examples: Java, C++
Example (Java)
int number = 10;If you try to assign another type:
number = “Hello”; // ErrorThe program will not compile because the variable was declared as int.
Dynamically Typed Languages
In dynamically typed languages, you do not need to declare the variable type.
The programming language automatically determines the type at runtime.
This means:
No explicit type declaration is required
The variable can change its type
Type checking happens while the program is running
Examples: Python, JavaScript
Example (Python)
# Python is dynamically typed
x = 10 # x is an integer
x = “Hello” # now x becomes a string
x = 3.14 # now x becomes a floatPython automatically infers the data type from the assigned value.
Interview Answer Tip
Python is a dynamically typed language. This means developers do not need to explicitly declare variable types. The Python interpreter automatically determines the type at runtime based on the assigned value.
Strongly Typed vs Weakly Typed
This concept is different from dynamic/static typing.
Weakly Typed Languages
Weakly typed languages automatically convert types to make operations work.
Example (JavaScript):
console.log("5" + 3) // "53"
console.log("5" - 3) // 2Strongly Typed Languages
Strongly typed languages do not automatically convert incompatible types.
Example (Python):
print("5" + 3) # TypeError
print(int("5") + 3) # 8Python requires explicit type conversion.
Interview Answer
Python is strongly typed because it does not allow implicit conversion between incompatible types. Developers must convert types explicitly.
Note: Python is dynamically typed (no type declarations) but strongly typed (won’t silently convert types.
Python Implementations
Most people think Python is a single program, but there are multiple implementations.
Most developers use CPython.