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
#2

A post was split to a new topic: Current date and time


#3

why does the number 10 in variable =10 not have " " or ’ ’ ?


#4

Numbers are not characters, and are not evaluated as such, They are numeric values taken as whole. 42 is a numeric value, as is 3.141589.


#5

what doe Variables mean


#6

It means the name represents a value that can and may change during the session. variable comes from vary, meaning not fixed or constant.

Keep in mind that the name is arbitrary. We may name our variables any way that suits us, but should give meaningful names so the reader can identify the value it represents. Variables are labels, not objects. They point to the object, which may be a value in memory such as a string or number, of may be a reference object such as a list, dictionary, tuple or set.