M
last year
Montrellcountry asked

What does the = symbol do in Python?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

In Python, the = symbol is used as the assignment operator. This is how we store a value in a variable.

It's a little bit like putting something into a box and giving that box a name. Let's look at an example to see how it works:

color1 = "blue"
print(color1)  

color2 = "pink"

color1 = color2

print(color1) 
print(color2)

Here's what's happening step by step:

  1. color1 = "blue": You create a variable called color1 and store the string "blue" in it. Think of color1 as a label stuck onto the box that has "blue" inside it.

  2. print(color1): This prints "blue" to the console because color1 currently points to "blue".

  3. color2 = "pink": You create another variable called color2 and store the string "pink" in it.

  4. color1 = color2: This is where the assignment operator shows its magic. You're telling Python to make color1 store whatever value color2 currently holds, which is "pink". Now, both color1 and color2 point to "pink". Think of it as removing "blue" from the color1 box and adding whatever is in the color2 box, which is "pink".

  5. print(color1): Prints "pink", because color1 is now storing the value "pink".

  6. print(color2): Also prints "pink", as expected since color2 hasn't changed.

Hope this clears things up! Feel free to reach out if you have any more questions or need further clarification.

Python
This question was asked as part of the Getting started with Python course.