Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In the previous lesson, we learned what variables are and how to name them correctly.
Now let's understand something very important:
Consider this simple code:
It looks very simple.
But Python is doing something important:
The name name is associated with the value "Amit".
Python variables are better understood as names that refer to objects rather than fixed boxes that permanently contain one type of data.
In this lesson, we will learn:
= operator doesid() does= and ===In Python, you create a variable when you assign a value to a name.
The basic syntax is:
For example:
Here:
Python creates or uses the appropriate object and associates the given name with it.
The = symbol in Python means:
It does not mean "is equal to" in the mathematical sense.
Let's look at a simple example:
You can think of it as:
Now if we write:
the name age is associated with the new value:
The old value is no longer associated with the name age.
This process is called rebinding.
One of the most common beginner mistakes is confusing:
and:
They have different purposes.
| Operator | Meaning | Example |
|---|---|---|
= | Assignment | age = 18 |
== | Comparison | age == 18 |
This assigns 18 to age.
This asks:
age equal to 18?"The result is either True or False.
For example:
Output:
Python variables can refer to different types of objects.
For example:
Python automatically understands the type of each value.
Output:
You don't need to specify the type when creating these variables.
Python is a dynamically typed language.
This means that a variable name does not have to be permanently restricted to one data type.
For example:
Output:
Later, we can assign a string:
Output:
We can even assign a list:
Output:
The same name data has been associated with different types of objects at different times.
Imagine a name tag:
Initially:
Then:
Now:
Later:
Now:
The name data stays the same, but what it refers to changes.
A useful way to understand Python is:
For example:
The value 18 is an integer object.
Python knows that the object is of type int.
You can check it:
Output:
Now:
The name age now refers to a string object.
Output:
You don't need to memorize the internal memory model yet.
Just remember:
and:
Consider:
What happens?
First:
Then:
Now both names refer to the same object in this example:
We can check object identity using is.
Output:
is Mean?The is operator checks whether two names refer to the same object.
Example:
Output:
This means both names refer to the same object.
Don't use is when you simply want to compare values.
For value comparison, use:
For example:
This asks whether the values are equal.
Use is when you specifically want to test object identity.
id() FunctionPython provides a built-in function called:
It returns an integer representing the identity of an object during its lifetime.
Example:
You may see a large number such as:
The exact number can be different on different runs and different Python implementations.
You can compare identities:
In this example, both names refer to the same object, so their identities are the same.
You can also write:
which is easier to understand when learning object identity.
It is better to think of id() as an object identity number, rather than assuming it is always a direct physical RAM address.
Let's look at an important example:
At this point:
Now we change score_b:
The result is:
The assignment changed what score_b refers to.
It did not change the value associated with score_a.
Let's verify:
Output:
Consider:
Now:
Then:
Now:
If no other references are using the old object, Python can eventually reclaim the memory associated with it.
Python automatically manages memory.
This means you usually do not have to manually free memory for ordinary Python objects.
Python uses automatic memory management.
One important mechanism in the standard Python implementation is reference counting.
Very simply:
means there is a reference from the name data to the object.
After:
the name data now refers to the string object.
If nothing else refers to the old object, Python can reclaim it.
Python also has a garbage collector that helps deal with certain groups of objects that reference each other.
You don't need to manually delete normal Python objects from memory.
For now, remember:
Garbage collection is Python's automatic process for finding certain objects that are no longer needed and reclaiming their memory.
A simplified example:
After:
Now:
If the old object has no remaining references and is eligible for cleanup, Python can reclaim its memory.
Don't think of garbage collection as:
Python's memory management is more nuanced, and the exact cleanup behavior depends on the implementation.
For beginner-level programming, the important idea is:
A variable should be assigned before you try to use it.
This causes an error:
if score has never been defined.
Python will produce an error similar to:
Correct:
Output:
Create or assign the variable first:
Prefer:
instead of:
A meaningful name makes your code easier to understand.
= for AssignmentCorrect:
Don't confuse it with:
which performs a comparison.
Correct:
Incorrect:
Although Python allows this:
you should avoid unnecessary changes of meaning in real programs.
For example, if:
it is clearer to continue using student_age for an age rather than later turning it into unrelated data.
== for assignment❌ Wrong:
✅ Correct:
❌ Wrong:
✅ Correct:
Python allows:
The name can be rebound to an object of another type.
is for normal value comparisonUsually use:
when asking whether two values are equal.
Use:
when checking whether they are the same object.
A simple mental model:
For example:
Think:
= operator do?id() return?Create a file:
Then write:
First:
Both names refer to the same object in this example.
After:
you should see:
Finally, observe how data is rebound to:
The type changes because the name is being associated with different objects.
Try these examples yourself:
Before running each program, predict the output first.
This is one of the best ways to improve your programming skills.
In the next lesson, we will continue your Python learning journey with Output Variables & Printing Techniques (2: Variables).
👉 Continue to Next Lesson: Output Variables & Printing Techniques →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Variables Introduction and Rules | Output Variables & Printing Techniques |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.