How do I assign a value to a variable?


#1

Question

How does variable assignment work? Also, do you always put the variable on the left and the value on the right?

Answer

In Python you write a variables name followed by a single equals sign and the value you want to store in that variable. That is always the case when assigning value to a variable - the variable on the left is assigned the value on the right, like this:

variable = 10

Now the number 10 is stored in the variable named variable, and can be accessed and used by referring to variable! That’s why in this exercise when we store the string with today’s date in todays_date, we can print its value to the screen by typing print todays_date.


FAQ: Learn Python - Python Syntax - Variables