Using Int and Float


#1

I got stuck in Unit 1 on numbers. I did not see an example of the syntax for int and float, and keep getting syntax errors. I tried:

int release_year = 2007
int(release_year) = 2007
int(“release_year”) = 2007

And several others. All syntax errors.

https://www.codecademy.com/courses/learn-python-syntax/lessons/learn-python-syntax/exercises/numbers?action=resume_content_item&program_content_id=3cb091868d2416284cae4830e48a898c&program_id=d57db0497f19d50b1080f0541cda9953


#2

python variables have no types.
they’re names that can refer to values, and the values have types
(as opposed to a typed variable referring to something that doesn’t know its own type)

a = 5

a itself has no type. the value that a refers to has the type int, and often a will be talked about as if it is the value itself eg. a is an int


#3

Once you have set a variable as an integer, can it then become a float? For example, if the initial value of a float happens to be an integer, but you later need to make it a decimal, is that possible?


#4

A variable has no type
Behaves just like a variable in Java with the type Object. It’ll refer to anything (aside from primitive values, but python doesn’t have that)

And, whether a float represents a whole number or not doesn’t change its type. That’s a float either way.


#5

Actually there’s a contradiction in that, there’s no answer

A float can’t be an integer, it already is something, and that something is float.

You can have a float which represents an integer value, such as 1.0

You’re probably suggesting that the variable has type float. It doesn’t, variables do not have types, they are names, that is all they are.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.