Behind the Scenes of Python: A Beginner's Guide

Introduction

Have you ever wondered what happens when you run a Python script? I recently started learning Python from the Chai aur Code YouTube channel by Hitesh Sir. He explained the internal workings of Python very clearly. This guide will help you understand Python's inner workings. We'll explore concepts such as the interpreter, compilation, memory management, mutable and immutable objects

Interpreting and Compiling

  • Python is an interpreted language, which means your code is executed line by line by the Python interpreter.

  • Before execution, Python compiles your source code (.py file) into byte code (.pyc file). Byte code is a lower-level, platform-independent representation of your source code.

  • The byte code is then executed by the Python Virtual Machine (PVM), which interprets the byte code and interacts with the underlying hardware.

Memory Management

Now that we have bytecode, the computer needs a space to store and process information. This is where memory comes in.

  • Values assigned to variables are stored in memory as objects

  • Variables in Python are essentially references to objects in memory. When you assign a value to a variable, you are creating a reference to an object.

  • In Python, the data type is associated with the object, not the variable. This means a variable can hold objects of different types during its lifetime.

# Assigning a list to a variable
a = [1, 2, 3]
# Assigning the same list to another variable
b = a

print("Original list a:", a)
print("Original list b (reference to a):", b)

# Modifying the list using one variable
a.append(4)

print("Modified list a:", a)
print("Modified list b (reference to a):", b)

In this example, a and b are references to the same list object in memory. When you modify a, b also reflects the change because they both point to the same list.

Mutable v/s Immutable Objects

there are basically 2 types of objects, mutable and immutable

immutable objects - These are objects whose state cannot be modified after they are created.

mutable objects - These are objects that can be changed after they are created.

# Immutable object: string
str1 = "hello"
str2 = str1

print("Before modification:")
print("str1:", str1)
print("str2:", str2)

# Modify str1
str1 = "world"

print("\nAfter modifying str1:")
print("str1:", str1)
print("str2:", str2)

Strings are immutable objects in Python. When str1 is modified, it actually creates a new string object and str2 remains unchanged, still referencing the original string.

# Mutable object: list
list1 = [10, 20, 30]
list2 = list1

print("Before modification:")
print("list1:", list1)
print("list2:", list2)

# Modify list1
list1[0] = 100

print("\nAfter modifying list1:")
print("list1:", list1)
print("list2:", list2)

Lists are mutable objects in Python. When list1 is modified, list2 also shows the modification because it references the same list.

Understanding references and mutability is crucial for efficient Python programming:

  • Reference Behavior : When you assign one variable to another, both variables point to the same object. Changes to the object through one variable will reflect when accessed through the other.

  • Copying Objects : To avoid unintentional changes to mutable objects, you may need to create a copy. This can be done using the copy module for shallow and deep copies.

Conclusion

Understanding the inner workings of Python helps you write more efficient and effective code. From how your script is executed, to memory management, what are mutable and immutable objects, knowing these details will grow your programming skills and deepen your Knowledge for the Python language.

Always Check out Chai aur code playlist on python for beginner to understand this concepts more clearly