0% found this document useful (0 votes)
15 views13 pages

Day 1

The document outlines a Python Basics training session focused on memory management and variable scope, covering topics such as the id() function, memory allocation for mutable and immutable objects, and the LEGB rule for variable resolution. Participants will learn how Python manages memory, the significance of object identity, and the different scopes of variables. The session includes hands-on coding activities and visual examples to reinforce learning objectives.

Uploaded by

radhikagawali102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Day 1

The document outlines a Python Basics training session focused on memory management and variable scope, covering topics such as the id() function, memory allocation for mutable and immutable objects, and the LEGB rule for variable resolution. Participants will learn how Python manages memory, the significance of object identity, and the different scopes of variables. The session includes hands-on coding activities and visual examples to reinforce learning objectives.

Uploaded by

radhikagawali102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Basics

Python Memory Model


Agenda – Python Basics - Day 1
• Welcome & Introduction
• Overview of Python!!
• Understanding id() and object identity
• Memory allocation: mutable vs immutable objects
• Variable Scope and the LEGB Rule
• Local, Enclosing, Global, Built-in scopes
• Visual Walkthrough and Examples
• Hands-on Coding Activity
• Q&A and Wrap-up
Learning Objectives

By the end of this session, you will understand:


• How Python manages memory for variables
• What id() function reveals about object identity
• How the LEGB rule determines variable scope
The id() Function
What is id()?
• The id() function returns the memory address (identity) of an object
in Python. Think of it as the "home address" where your variable's
value lives in computer memory.

Key Technical Points:


• Unique Identity: Every object in memory has a unique id that
remains constant during its lifetime
• Memory Address: The id represents the actual location in RAM where
the object is stored
### id()

a = "Hello World"
print(id(a))

# Creating variables
a = "Hello World"
b= a
c = [1, 2, 3]
d= c
# Printing ID of variables
print(f"id(a) = {id(a)}")
print(f"id(b) = {id(b)}")
print(f"id(c) = {id(c)}")
print(f"id(d) = {id(d)}")

x = 42 # Memory allocated for integer 42


print(f"x points to memory: {id(x)}")

y=x # y points to SAME memory location as x


print(f"y points to memory: {id(y)}")
print(f"Same memory? {id(x) == id(y)}")

x = 100 # x now points to NEW memory location


print(f"x now points to: {id(x)}")
print(f"y still points to: {id(y)}")

### LEGB Rule | SCOPE Resolution in Python


# Local scope​
def local_function():
local_var = "I'm local!"
print(local_var)

local_function()

# Enclosing scope​

def outer_function():
enclosing_var= "I'm enclosing!"
def inner_function():
print(enclosing_var)
inner_function()

outer_function()

# Global scope​
global_var = "I'm global!" # Global scope​
def my_function():
print(global_var) # Accesses global scope

my_function()

# Built-in scope​

print(len([1, 2, 3])) # Both are built-in functions

# Enclosing scope​

def enclose_fun():
a = 1000
print(f"a is {a}")
def nested_fun():
b = 2000
print(f"a is {a} and b is {b}")
nested_fun()
enclose_fun()

### id()

a = "Hello World"
print(id(a))

# Creating variables
a = "Hello World"
b= a
c = [1, 2, 3]
d= c
# Printing ID of variables
print(f"id(a) = {id(a)}")
print(f"id(b) = {id(b)}")
print(f"id(c) = {id(c)}")
print(f"id(d) = {id(d)}")

x = 42 # Memory allocated for integer 42


print(f"x points to memory: {id(x)}")

y=x # y points to SAME memory location as x


print(f"y points to memory: {id(y)}")
print(f"Same memory? {id(x) == id(y)}")

x = 100 # x now points to NEW memory location


print(f"x now points to: {id(x)}")
print(f"y still points to: {id(y)}")

### LEGB Rule | SCOPE Resolution in Python


# Local scope​
def local_function():
local_var = "I'm local!"
print(local_var)

local_function()

# Enclosing scope​

def outer_function():
enclosing_var= "I'm enclosing!"
def inner_function():
print(enclosing_var)
inner_function()

outer_function()

# Global scope​
global_var = "I'm global!" # Global scope​
def my_function():
print(global_var) # Accesses global scope

my_function()

# Built-in scope​

print(len([1, 2, 3])) # Both are built-in functions

# Enclosing scope​

def enclose_fun():
a = 1000
print(f"a is {a}")
def nested_fun():
b = 2000
print(f"a is {a} and b is {b}")
nested_fun()
enclose_fun()

### id()

a = "Hello World"
print(id(a))

# Creating variables
a = "Hello World"
b= a
c = [1, 2, 3]
d= c
# Printing ID of variables
print(f"id(a) = {id(a)}")
print(f"id(b) = {id(b)}")
print(f"id(c) = {id(c)}")
print(f"id(d) = {id(d)}")

x = 42 # Memory allocated for integer 42


print(f"x points to memory: {id(x)}")

y=x # y points to SAME memory location as x


print(f"y points to memory: {id(y)}")
print(f"Same memory? {id(x) == id(y)}")

x = 100 # x now points to NEW memory location


print(f"x now points to: {id(x)}")
print(f"y still points to: {id(y)}")

### LEGB Rule | SCOPE Resolution in Python


# Local scope​
def local_function():
local_var = "I'm local!"
print(local_var)

local_function()

# Enclosing scope​

def outer_function():
enclosing_var= "I'm enclosing!"
def inner_function():
print(enclosing_var)
inner_function()

outer_function()

# Global scope​
global_var = "I'm global!" # Global scope​
def my_function():
print(global_var) # Accesses global scope

my_function()

# Built-in scope​

print(len([1, 2, 3])) # Both are built-in functions

# Enclosing scope​

def enclose_fun():
a = 1000
print(f"a is {a}")
def nested_fun():
b = 2000
print(f"a is {a} and b is {b}")
nested_fun()
enclose_fun()
# Creating variables
a = "Hello World"
b = "Hello World"
c = [1, 2, 3]
d=c
# Printing ID of variables
print(f"id(a) = {id(a)}")
print(f"id(b) = {id(b)}")
print(f"id(c) = {id(c)}")
print(f"id(d) = {id(d)}")
Memory Allocation in Python

How Python Manages Memory


• Python automatically handles memory allocation and deallocation
through a memory manager that works behind the scenes.
Key Technical Points:
• Reference Counting: Python tracks how many variables point to each
object in memory
• Garbage Collection: When reference count reaches zero, memory is
automatically freed
# Step-by-step memory allocation
x = 42 # Memory allocated for integer 42
print(f"x points to memory: {id(x)}")

y=x # y points to SAME memory location as x


print(f"y points to memory: {id(y)}")
print(f"Same memory? {id(x) == id(y)}")

x = 100 # x now points to NEW memory location


print(f"x now points to: {id(x)}")
print(f"y still points to: {id(y)}")
LEGB Rule - Variable Scope Resolution

What is LEGB?
• LEGB stands for Local → Enclosing → Global → Built-in - the
order Python searches for variables.
Key Technical Points:
• Scope Hierarchy: Python searches for variables in a specific order when
resolving names
• First Match Wins: Python stops searching once it finds the variable in any
scope level
The Four Scopes
L - Local Scope
Variables defined inside a function
E - Enclosing Scope
Variables in the enclosing function (for nested functions)
G - Global Scope
Variables defined at the module level
B - Built-in Scope
Pre-defined Python names (print, len, etc.)
# Global scope
# Local scope
global_var = "I'm global!" # Global scope
def my_function():
def my_function():
local_var = "I'm local!" print(global_var) # Accesses global
print(local_var) scope

# Built-in scope
# Enclosing scope
print(len([1, 2, 3])) # Both are built-in
def outer_function(): functions
enclosing_var = "I'm enclosing!"
def inner_function():
print(enclosing_var)
inner_function()
Q&A
Thank You

You might also like