python42
python42
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 1/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Cheat Sheet
Object
Strings, Integers, Floats, Lists, Functions, Module etc. are all objects.
Identity of an Object
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 2/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Every object that you use in a Python Program will be stored in Computer
Memory
The unique id will be related to the location where the object is stored in
the Computer Memory.
Name of an Object
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 3/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Namespaces
It ensures that names are unique and won’t lead to any conflict.
Code
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 4/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
PYTHON
1 def greet_1():
2 a = "Hello"
3 print(a)
4 print(id(a))
5
6 def greet_2():
7 a = "Hey"
8 print(a)
9 print(id(a))
10
Expand
Output
Namespace - 1
Hello
140639382368176
Namespace - 2
Hey
140639382570608
Types of namespaces
1. Built-in
2. Global
3. Local
Built-in Namespace
Created when we start executing a Python program and exists as long as the
program is running.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 5/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
This is the reason that built-in functions like id(), print() etc. are always
available to us from any part of the program.
Global Namespace
It is created when the module is loaded, and it lasts until the program ends.
Local Namespace
A new local namespace is created when a function is called, which lasts until
the function returns.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 6/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Scope of a Name
The scope of a name is the region of a program in which that name has
meaning.
Python searches for a name from the inside out, looking in the
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 7/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Global variables
Example 1
Code
PYTHON
1 x = "Global Variable"
2 print(x)
3
4 def foo():
5 print(x)
6
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 8/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
6
7 foo()
Output
Global Variable
Global Variable
Example 2
Code
PYTHON
1 def foo():
2 print(x)
3
4 x = "Global Variable"
5
6 foo()
Output
Global Variable
Local Variables
This variable name will be part of the Local Namespace which will be
created when the function is called and lasts until the function returns.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 9/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 def foo():
2 x = "Local Variable"
3 print(x)
4
5 foo()
6 print(x)
Output
Local Variable
NameError: name 'x' is not defined
As,
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=145650… 10/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Local Import
Code
PYTHON
1 def foo():
2 import math
3 print(math.pi)
4
5 foo()
6 print(math.pi)
Output
3.141592653589793
NameError: name 'math' is not defined
Code
PYTHON
1 x = "Global Variable"
2
3 def foo():
4 x = "Local Variable"
5 print(x)
6
7 print(x)
8 foo()
9 print(x)
Output
Global Variable
Local Variable
Global Variable
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=145650… 11/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 x = "Global Variable"
2
3 def foo():
4 global x
5 x = "Global Change"
6 print(x)
7
8 print(x)
9 foo()
10 print(x)
Output
Global Variable
Global Change
Global Change
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=145650… 12/12