How come you don’t add “_int” or “_float” to define whether it’s a float or an integer?
Python is not type cast, but type assigned. A variable gets its type from the value assigned to it. We do not have to declare the type.
pi = 3.1416
It will be a float by assignment.
int_pi = int(pi)
will parse the integer out of the value.
str_pi = str(pi)
will cast the value to a string.
Nothing prevents us from changing the values in any of the above to another type. We can change from one type to another and simply re-use variables, but we cannot perform operations on unmatched types.
1 + "1"
will throw an error.
We can even help garbage collection by releasing objects when they are no longer needed.
my_huge_array = None
The array will vanish, assuming there are no other references still in play.
I am trying to get back into coding and having worked with Java before, I will admit that this was making me bang my head against the wall! Thank you for the explanation, one thing I did notice though. According to the example below, you can still cast the type depending on how you want your expressed.
myfloat = 7.0
myfloat = float(7)
I got that from here
for the statement: str_pi = int(pi), will it round pi or just see the first number before the decimal?
Don’t bother trying to learn small individual facts about what various things do, you’ll never get a complete picture of things that way. Get into the habit of looking it up instead.
which is the integer part of that number, as you will find in your reading.
I don’t understand the significance of the variables “int” and “float”. the exercise does not require that you use either. You just write release_year = 2019, not int= 2019 or release_year_int = 2019 or anything like that. why does it matter whether the release year is an integer or a float?
I also am confused about this.
Go through your lessons again.
int is not a variable, it is a type. Same with float.
int stands for “integer,” and floats are fractional numbers (numbers that have a decimal point).
So yes, you did use ints in the variables that were assigned to you (release_year is one of those variables, and a variable is, in simple terms, just a name that could represent any value, which is why we call it a “variable.” Because its value could “vary.”) when you set 2019, which is an integer or int, to be represented by the variable release_year.
All of these terms are defined in your lessons. Go back through them again for review.