How do I assign a value to a variable?

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.

12 Likes

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

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

5 Likes

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.

3 Likes

what doe Variables mean

1 Like

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.

5 Likes

How did you choose the number 10? Does it matter or as long as it is a whole number? does changing the number affect anything? Can I choose a number at random as long as it is a whole number?

1 Like

It’s important that we read the lesson text thoroughly so we gain some understanding from it. Then read the instructions carefully and follow any examples given in the lesson.

What is the instruction for this exercise? Can you post a link to the exercise page, please?

In your answer, why don’t print here need single or double quotation marks?

I’m sorry.
I’m still don’t really understand the way cuz I’m new to programming.
Can you tell me more about it?

1 Like

I came across an error doing this exercise because I write the date in the format day/month/year. I think in this that a number cannot be used in the first instance in this kind of variable. It has to be a word.

Or, a character string.

"06/04/2019"

or,

'06/04/2019'

Outside of a string, / is the division operator, and numbers do not have leading zeros.

>>> a = 06
SyntaxError: invalid token
>>> 

Essentially, all dates are at minimum, a string, or in the case of Python, datetime objects with a number of properties.

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.day
6
>>> now.month
4
>>> now.year
2019
>>> print ("%s/%s/%s" % (now.day, now.month, now.year))
6/4/2019
>>> print ("{}/{}/{}".format(now.day, now.month, now.year))
6/4/2019
>>> 

Both forms a string formatting are valid though the latter is newer and more robust and flexible. Witness the following:

>>> print ("{:02d}/{:02d}/{:04d}".format(now.day, now.month, now.year))
06/04/2019
>>> 

String formatting comes up later in the course so don’t let this tie you down, today.

4 Likes

Thank you MTF. I am working my way up to parentheses. I’m an absolute beginner where coding is concerned but it has become surprisingly addictive.

2 Likes

I tried todays_date=12-12-12
Tried printing today’s-date

I get -12

Tried 12/12/12 if I print the variable I get 0 as output.could you please tell me why…

1 Like

The Python interpreter is reading the right side of your variable assignment statements as arithmetic expressions.

Remember: an assignment statement (one that uses the assignment operator, =) always follows these steps:

  1. Evaluate the expression to the right of the assignment operator to obtain a value
  2. Assign that value to the variable on the left of the assignment operator

In your first example, step 1, it evaluates the arithmetic expression 12-12-12, which equals -12 (“twelve minus twelve minus twelve”).
In the second, it treats the problem as(12/12) / 12, since Python does operations which are at the same level in Order of Operations from left to right. Python 2 treats / as integer division (dropping the fractional or decimal part), so 12/12 is 1 and 1/12 is 0 (zero).

But what to do to get the output you are looking for? Don’t use numbers, use strings!!

todays_date = "12/12/12"

The right side is simply treated as a string (sequence of characters), not a series of numbers. The value of a string is the string itself, so that is what is assigned to the variable, and you get something closer to what you want.

7 Likes

Amazing! This is amazing! So good!

1 Like

i tried the following exercise:
todays_date = 2052020
print todays_date

on running it gave the output
2052020

which was as expected

however when i change it to
todays_date = 02052020
print todays_date

running it gives
545808

so does it have something to do with starting the assigned numerical value with a 0? why am i getting this output instead of the date i put in?

A number that is prefixed by a zero is seen as OCTAL (base 8)

Octal Number:

02052020

Decimal number:

545808

Conversion: Octal to Decimal

(02052020)8 = (545808)10

It’s important to note that a date string is not a number; a timestamp is a number. Your input is not recognized as a date. Try writing it as a string…

"2020-02-05"
>>> from datetime import date
>>> date.fromisoformat('2020-02-05')
datetime.date(2020, 2, 5)
>>> date(2020, 2, 5).isoformat()
'2020-02-05'
>>> 

because 10 is an integer variable and double qoutes is not used for integer variables

Okay, my only question is that why arent we using the quotations marks now with the print sign? i.e. todays_date =“10th of June”
print “todays_date” results in todays_date but when I exclude quotation marks, the result is 10th of June, How? Please explain. Thank you