Using Int and Float


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.


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


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?


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.


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.


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