FAQ: Learn Python: Syntax - Numbers

It looks like your variable, a_release_year needs to be written as suggested:

release_year = 1985

thank you so muchi got through

1 Like

why is it that i cannot put quotations when assigning variable integers or float values for example release_year=“2021”
runtime=“120” . In the previous exercises in the introductory stage of variables the exercise of assigning meals dish name we used quotations while writing the variable dish name

We write quotes around text so the interpreter knows it is string data, not identifiers. In order for the interpreter to see our numeric values as number data, we leave off the quotes. The literal value of the number can then be used in computation (arithmetic).

3 Likes

thank you Sir have a blessed day ahead

2 Likes

A post was split to a new topic: Experimenting around with this exercise

I was wondering the same, isn’t release_year_int = 2012 a variable?

I’ve ran the code with:
release_year = 1950
runtime = 180
release_year_int = 1980
runtime_int = 180
print (release_year_int, runtime_int)

and the print is:1980 180
So my question is then why is it needed to declare a variable first then define float or int if the number used in the variable doesn’t even need to match the float or int line

It is not declared as an int, it is an integer. Python knows this by the way it is stored. What we use as a name to a variable has no real meaning other that what we give it. For the sake of us knowing that the value referred to by the variable is an integer, it has been written into the name. This is entirely arbitrary, and nothing to do with type declaration.

1 Like

I am confused on one thing. Do we have to define if a variable as an integer or a float? I understand the difference but is Python smart enough to do this without me specifying it? Can we simply just state the variable = an integer or float? by how the number is formulated either with a decimal or not?

Yes, Python will interpret it and use a type based on the way you write it, 3 for example is integer (3) and 3. would be a float (3.0). If you’re curious about the rules see the language reference: https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals (the examples on that page cover all the typical uses).

If you scroll about a bit you can see the other potential literals (e.g. lists, strings and such). If you’re used to an explicitly typed language this can be a little unusual at first but literals only really exist for a few built-ins that don’t take too long to familiarise yourself with.

I am not sure that I have answered the problem correctly. Please review and let me know if I need to correct anything.

release_year=2022
runtime=20
print(“release year 2022”)
print(“runtime 20”)

rating_out_of_10=7.5
print(“rating out of 10=7.5”)

Variables are names we choose to assign fixed or dynamic values (that could be changing in the program). Given we have a value, we can access that by referring to the variable.

print (f"Release Year: {release_year}")
print (f"Runtime: {runtime}"}

If the values change, those print statements will be up to date. If we are instead printing the literals, we cannot change them without actually editing the code.

1 Like

I apologize I am new to coding and I don’t understand the answer. Please explain further.

When we print a variable, we do not print the actual value (the literal), just the variable. In the above print statements the values are interpolated, meaning they are polled and cast to string so they become characters, not values.

a = 6
b = 7
c = f"{a} times {b} equals {a * b}"

print (c)
# 6 time 7 equals 42
1 Like

hello,

i am bit stuck with Numbers , so need your help… I have done 6 lessons(hello world from Welcome to Errors… but stuck with Numbers… so kindly need your help

Why does this creates an error in codeacademy compiler but runs perfectly fine outside.
num = input("Please enter a number: ")

print(int(num) ** 2)

Traceback (most recent call last):
File “main.py”, line 1, in
num = input("Please enter a number: ")
EOFError: EOF when reading a line

The problem is the rapid time out on input. When you press Run, quickly click in the console and enter a value. You have about two or three seconds.

I am sorry. If it prints “6 times 7 equals 42” to the console then is it not printing the values (or literals, as I understand it) of a and b rather than the variables?
My end-goal in asking this question is learning what exactly the term literal means because I am confused.

One can see how that would be confusing. Values in memory are just values, and variables only refer to them, not store them.

What is a literal?

a = 6

Above, a is the variable to which is assigned a reference to the value, 6. In the above, 6 is a literal since it is literally a 6.

If we say,

a = 3 * 2

then the referenced value is still 6 but the literals are, 3 and 2. So when we see the actual numbers, or in the case of strings, actual text in quotes, those are literals. That means it is visible as hard-coded values. They are part of the program.

1 Like