Python Notes
How to run python codes
1. Interactive Mode in cmd
Open the command prompt.
Type python or python3 to start the Python interpreter.
When you write and run Python code in the interactive mode of the Python
interpreter it is not stored permanently anywhere. It exists only temporarily in
memory and is lost when you close the Python session.
To save your Python program for reuse or editing later, you need to Use an
editor like Notepad, VS Code, or PyCharm to write your code.
2. Writing Code in a .py File
Open a text editor (e.g., Notepad) and write code
Save that file with .py extension (e.g my_script.py)
Open the command prompt, navigate to the file's directory using cd, and run:
python my_script.py
3. Using jupyter notebook
, What Is Garbage Collection in Python?
Garbage collection in Python is an automated memory management process that
deletes objects when they are no longer in use. It uses two methods of garbage
collection: reference counting and generational. Reference counting removes an
object after it reaches zero, and generational garbage collection can break cyclic
references to delete unused objects.
How Memory Management Works in Python
Python is a dynamically typed language. We don’t declare the type of a variable
when we assign a value to the variable in Python. It states the kind of variable in
the runtime of the program.
As you can see below, we just assign a variable to an object and Python detects the
type of the object.
What Are Python Objects?
Python objects have three things: Type,value and reference count.
When we assign a name to a variable, Python automatically detects its type. Value is
declared while defining the object. Reference count is the number of names
pointing to that object.
,How Garbage Collection in Python Works
Garbage collection is the process of releasing memory when the object is no
longer in use. This system destroys the unused object and reuses its memory slot
for new objects.
Python has an automated garbage collection process. It has an algorithm to
deallocate objects that are no longer needed. Python has two ways to delete the
unused objects from the memory
2 Methods of Garbage Collection in Python
There are two main ways that Python collects garbage:
Reference counting generational garbage
collection.
1. Reference Counting
In reference counting, references are always counted and stored in memory.
In the example, we assign c to 50. Even if we assign a new variable, the object is
the same. The reference count increases by one. Because every object has its
own ID, we print the IDs of objects to see if they are the same or different.
, When we change the value of a, we create a new object. Now, a points to
60, b and c point to 50.
When we change a to None, we create a none object. Now the previous
integer object has no reference. Garbage collection deletes it.
We assign b to a boolean object. The previous integer object is not deleted
because it still has a reference by c.
Now, we delete c. We decrease the reference count to c by one.
del() statement doesn’t delete objects, it removes the name (and reference) to
the object. When the reference count is zero, the object is deleted from the
system by the garbage collection.
it’s easy to implement.
Programmers don’t have to worry about deleting objects when they
are no longer in use.
How to run python codes
1. Interactive Mode in cmd
Open the command prompt.
Type python or python3 to start the Python interpreter.
When you write and run Python code in the interactive mode of the Python
interpreter it is not stored permanently anywhere. It exists only temporarily in
memory and is lost when you close the Python session.
To save your Python program for reuse or editing later, you need to Use an
editor like Notepad, VS Code, or PyCharm to write your code.
2. Writing Code in a .py File
Open a text editor (e.g., Notepad) and write code
Save that file with .py extension (e.g my_script.py)
Open the command prompt, navigate to the file's directory using cd, and run:
python my_script.py
3. Using jupyter notebook
, What Is Garbage Collection in Python?
Garbage collection in Python is an automated memory management process that
deletes objects when they are no longer in use. It uses two methods of garbage
collection: reference counting and generational. Reference counting removes an
object after it reaches zero, and generational garbage collection can break cyclic
references to delete unused objects.
How Memory Management Works in Python
Python is a dynamically typed language. We don’t declare the type of a variable
when we assign a value to the variable in Python. It states the kind of variable in
the runtime of the program.
As you can see below, we just assign a variable to an object and Python detects the
type of the object.
What Are Python Objects?
Python objects have three things: Type,value and reference count.
When we assign a name to a variable, Python automatically detects its type. Value is
declared while defining the object. Reference count is the number of names
pointing to that object.
,How Garbage Collection in Python Works
Garbage collection is the process of releasing memory when the object is no
longer in use. This system destroys the unused object and reuses its memory slot
for new objects.
Python has an automated garbage collection process. It has an algorithm to
deallocate objects that are no longer needed. Python has two ways to delete the
unused objects from the memory
2 Methods of Garbage Collection in Python
There are two main ways that Python collects garbage:
Reference counting generational garbage
collection.
1. Reference Counting
In reference counting, references are always counted and stored in memory.
In the example, we assign c to 50. Even if we assign a new variable, the object is
the same. The reference count increases by one. Because every object has its
own ID, we print the IDs of objects to see if they are the same or different.
, When we change the value of a, we create a new object. Now, a points to
60, b and c point to 50.
When we change a to None, we create a none object. Now the previous
integer object has no reference. Garbage collection deletes it.
We assign b to a boolean object. The previous integer object is not deleted
because it still has a reference by c.
Now, we delete c. We decrease the reference count to c by one.
del() statement doesn’t delete objects, it removes the name (and reference) to
the object. When the reference count is zero, the object is deleted from the
system by the garbage collection.
it’s easy to implement.
Programmers don’t have to worry about deleting objects when they
are no longer in use.