# 🐍 Day 02 Python Variables: Understanding Assignment and References

If you're new to programming, you may have heard the word **variable** many times.

But what exactly is a variable?

Don't worry. The idea is much simpler than it sounds. Let's understand it with a few real-world examples and then see how Python actually handles variables.

* * *

# `📦 What Is a Variable?`

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/f878fd24-24ed-4c2c-9751-21a0b832102a.png align="center")

Imagine you have a box containing the number `10`.

Now, let's give that box a name: `x`.

In Python, we can write:

```python
x = 10
```

You can visualize this as:

```python
10  ←── x
```

Here, `10` is the **object**, and `x` is the **name that refers to that object**.

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/0af85b09-a133-4ca3-9b5b-06dcc5f1d366.png align="center")

So, a simple way to remember it is:

> **A variable is a name that refers to an object.**

This is slightly different from thinking of a variable as a physical box. Python variables are better understood as **names or references to objects**.

We can use Python's `id()` function to see the identity of the object:

```python
x = 10
```

print(x)

print(id(x))

The output might look like:

```python
10
```

140734123456

The exact `id()` number isn't important. It can vary between different runs and systems.

For now, just remember:

> `id()` allows us to observe the identity of an object.

* * *

# `✍️ Rules for Naming Variables`

Before creating variables, there are a few rules you need to know.

### No spaces

Variable names cannot contain spaces.

❌ Invalid:

```python
my name = "Akhil"
```

✅ Valid:

```python
my_name = "Akhil"
```

An underscore `_` can be used to separate words.

### A variable cannot start with a number

❌ Invalid:

```python
2name = "Akhil"
```

3value = 50

But numbers can appear after the first character:

```python
name2 = "Akhil"
```

value3 = 50

student100 = "John"

So remember:

> **A variable name cannot start with a number, but numbers can appear later in the name.**

### A variable can start with an underscore

This is valid:

```python
_name = "Akhil"
```

We'll learn more about Python's naming conventions and keywords later.

* * *

# 🔤 `Python Variables Are Case-Sensitive`

Python treats uppercase and lowercase letters as different.

For example:

```python
a = 10
```

`A = 50`

`print(a)`

`print(A)`

Output:

```python
10
```

50

Although `a` and `A` look similar, Python considers them two different names.

So:

```python
a ≠ A
```

Keep this in mind when writing your programs.

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/dceb9f0d-a032-40d1-b4f4-6ae55764746b.png align="center")

* * *

# 🔄 `What Happens When We Reassign a Variable?`

Now let's look at something interesting.

Consider:

```python
x = 10
```

`print(x)`

`print(id(x))`

We can visualize it as:

```python
10  ←── x
```

The name `x` refers to the integer object `10`.

![dd](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/270e6726-8bcc-474c-8342-20d7bac7d916.png align="center")

Now let's write:

```python
x = 20
```

print(x)

print(id(x))

The output will be:

```python
20
```

<different id>

So what happened?

We **reassigned** the variable `x`.

Initially:

```python
10  ←── x
```

After:

```python
x = 20
```

the situation becomes:

```python
20  ←── x
```

Notice that we didn't change the number `10` into `20`.

Instead, we made the name `x` refer to another object.

This is an important idea:

> **Assignment changes what a name refers to.**

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/9a69434b-7ce3-4ad6-9666-bac3aa3f91cb.png align="center")

* * *

## 🔎 `Let's Look at the Object Identity`

We can make this even clearer using `id()`:

```python
x = 10
```

`id_10 = id(x)`

`x = 20`

`id_20 = id(x)`

`print(id_10)`

`print(id_20)`

The two identity values will normally be different.

You can visualize it like this:

```python
Before:
```

`10 ←── x`

`After x = 20:`

`20 ←── x`

So instead of saying:

> "The address of `x` changed."

It is more accurate to say:

> **The object that** `x` **refers to changed.**

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/e968ec0d-68b7-44a0-91c1-a26906a2bd72.png align="center")

* * *

# 🔗 `Can Two Variables Refer to the Same Object?`

Yes!

This is one of the most important things to understand about Python variables.

Consider:

```python
x = 10
```

`y = x`

`print(x)`

`print(y)`

`print(id(x))`

`print(id(y))`

You will get:

```python
10
```

`10`

`same id`

`same id`

Why?

Because when we write:

```python
y = x
```

we are telling Python that `y` should refer to the **same object that** `x` **refers to**.

We can visualize it like this:

```python
10  ←── x
```

`10 ←── y`

There is one object, `10`, and two names, `x` and `y`, referring to it.

This is **not**:

```python
10  ←── x
```

`10 ←── y`

Instead, both names refer to the same object.

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/2d5e407b-c1cf-4ac0-9fd7-e7c1c4ab34e1.png align="center")

* * *

# 🔄 `What Happens When We Reassign x?`

Let's take the previous example:

```python
x = 10
```

`y = x`

`print("Before:")`

`print(x)`

`print(y)`

`print(id(x))`

`print(id(y))`

`x = 15`

`print("After:")`

`print(x)`

`print(y)`

`print(id(x))`

`print(id(y))`

Before `x = 15`, we have:

```python
10  ←── x
```

`10 ←── y`

Both `x` and `y` refer to the same object.

Now we execute:

```plaintext
x = 15
```

`x` is reassigned.

The new situation is:

```python
10  ←── y
```

`15 ←── x`

Therefore:

```python
x → 15
```

`y → 10`

The value of `y` did not change.

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/8567f5f4-58f2-4549-8314-96b1d0276f97.png align="center")

### Why?

Because we didn't modify the object `10`.

We simply changed what `x` **refers to**.

This is why reassigning one variable doesn't automatically change another variable that was previously referring to the same object.

* * *

# 🔁 `What If We Reassign y?`

Let's try the same thing with `y`:

```python
x = 10
```

`y = x`

`x = 15`

`y = 50`

`print(x)`

`print(y)`

`print(id(x))`

`print(id(y))`

Output:

```python
15
```

`50`

Let's follow it step by step.

Initially:

```python
10  ←── x
```

`10 ←── y`

After:

```python
x = 15
```

we have:

```python
10  ←── y
```

`15 ←── x`

Then:

```python
y = 50
```

reassigns `y`:

```python
10
```

`15 ←── x`

`50 ←── y`

So:

```python
x → 15
```

`y → 50`

Again, changing one variable doesn't change the other.

* * *

# 🔒 `Why Does This Happen With Integers?`

Now we come across an important Python concept:

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/52ef5e2d-5185-42b1-9547-5efb3154ce74.png align="center")

## Immutability

**Integers in Python are immutable.**

That might sound complicated, but the idea is simple.

**Immutable means that an existing integer object cannot be changed.**

For example:

```python
x = 10
```

`y = x`

We have:

```python
10  ←── x
```

`10 ←── y`

Now suppose we write:

```python
x = 15
```

Python doesn't change the existing `10` object into `15`.

Instead, `x` is made to refer to another integer object:

```python
10  ←── y
```

`15 ←── x`

The integer object `10` itself wasn't modified.

That's why `y` continues to refer to `10`.

* * *

# ➕ `What About x = x + 5?`

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/69abddf0-2fd2-4ffa-bd58-f13eb359e4a8.png align="center")

Here's another useful example:

```python
x = 10
```

`y = x`

`print("Before:")`

`print(x)`

`print(y)`

`print(id(x))`

`print(id(y))`

`x = x + 5`

`print("After:")`

`print(x)`

`print(y)`

`print(id(x))`

`print(id(y))`

Let's understand this line:

```python
x = x + 5
```

First, Python calculates:

```python
10 + 5 = 15
```

Then `x` is reassigned to the resulting integer object `15`.

Before:

```python
10  ←── x
```

`10 ←── y`

After:

```python
10  ←── y
```

`15 ←── x`

Therefore:

```python
x → 15
```

`y → 10`

And their object identities are now different.

# 🧠 `The Big Picture`

![](https://cdn.hashnode.com/uploads/covers/6a5f51c739f8a79b58393a96/1d0d463e-50a1-4b80-a38c-e59a7964b39e.png align="center")

At this point, you should have a clear mental model of Python variables.

When we write:

```python
x = 10
```

think:

```python
10  ←── x
```

When we write:

```python
y = x
```

think:

```python
10  ←── x
```

`10 ←── y`

When we write:

```python
x = 20
```

think:

```python
10  ←── y
```

`20 ←── x`

And when we write:

```python
y = 50
```

think:

```python
10
```

`20 ←── x`

`50 ←── y`

This simple visualization makes Python's variable behavior much easier to understand.

* * *

# 📝 `Variables in a Nutshell`

Let's summarize everything we've learned:

*   A **variable is a name that refers to an object**.  
    
*   `=` is used for **assignment**.  
    
*   Assigning a new value **reassigns the variable**.  
    
*   Multiple variables can refer to the **same object**.  
    
*   Reassigning one variable does not automatically change another variable.  
    
*   `id()` allows us to observe an object's identity.  
    
*   Integers in Python are **immutable**.  
    
*   `x = x + 5` results in `x` referring to the resulting integer object.  
    

The most important idea to remember is:

> **In Python, variables are names that refer to objects. Assignment changes what a name r**efers to; it does not change the object itself.
