Why don't I need to define my variable as a float or integer?

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

hello there,

for the statement: str_pi = int(pi), will it round pi or just see the first number before the decimal?

1 Like

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.

1 Like

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.

1 Like

Thanks ionatan. That’s boss mate.

My nature would run head long down this rabbit hole. Thanks to your contribution a COMPLETE newbie to Python…err…make that all things coding can remain focused on the BIG PICTURE.

There are many reasons we need should choose between int and float when storing numeric values. Since 99.99999999% of processors (i.e. Intel & AMD for example), use binary to store numbers and count for example, it is extremely fast to use integers as opposed to floating point numbers. The reason has everything to do with how actual calculations are performed on these two very different numeric types.

The text mentioned precision, which applies to floating point numbers. For example, how do we accurately represent the fraction 2/3 in binary? The numerator is just a 2 and the denominator is just a 3. These are 00000010 and 00000011 in binary. The problem occurs when we divide 2 by 3. We get a number that repeats forever, meaning an infinite number of digits to the right of the decimal place, aka .666666666666666666666666666666666666666666666 (goes on forever). In the real world, it might be good enough for our purposes to limit the number of decimal places and this is where precision comes in.

So, when choose int over float or vice versa, think about what your data might be. If you are keeping track of the number of rows in a table, or the number of students in a class, you should be safe with integers and never need float. If you needed to take a percentage of the number of students in a class and did not care if the result was a fraction, then again use integers. However, if you require precision and will deal with fractions in any way, then start with a float.

There is a lot of overhead for the processor to deal with floating point numbers compared to integers. if you plan to loop through a function that deals with lots of floating point calculations, then it could take thousands of times longer to execute compared to if you have chosen integers instead.

Therefore, if you think you will be doing millions of millions of iterations or loops on a dataset that could be represented accurately by integers, then use them. But, otherwise use floating point variables.

In theory, you could even represent all dollar amounts in pennies instead of dollars and cents. For example, $1 is the same a 100 pennies, and $1.33 is the same as 133 cents. So, technically, you could use integers to keep track of many accounting type numbers if you wanted to. When you display or print the results, you could wrap your output with a function that displays the result in dollars and cents, yet internally your program would just work with pennies. Sounds crazy, but in reality it is not. Computers are very, very good at dealing with integers and incredibly fast as performing calculations, so why not exploit what computers are good at? Python as a language lets you do just that, and still allows to make your program easy to understand for humans.

I hope this explains why integers are used instead of floating point variables and vice-versa.

can you demonstrate that in code?

floats are pretty fast, I imagine if I was to write a loop in C that did a billion additions and multiplications for both int64 and double, I’d get little to no difference in the time spent on that.

#include <inttypes.h>
#include <stdio.h>
#include <time.h>

#define INT int64_t
#define FLOAT double
#define STEPS 1000000000

INT int_test() {
    INT total = 0;
    for (int i = 0; i < STEPS; i++) {
        total += 125;
        total /= 2;
    return total;

FLOAT float_test() {
    FLOAT total = 0;
    for (int i = 0; i < STEPS; i++) {
        total += 1.25;
        total /= 2;
    return total;

int main() {
    printf("size of int: %ld\n", sizeof(INT));
    printf("size of float: %ld\n", sizeof(FLOAT));
    clock_t t = clock();
    INT res = int_test();
    t = clock() - t;
    printf("int says: %ld after time=%f\n", res, (double) t / CLOCKS_PER_SEC);
    t = clock();
    FLOAT res2 = float_test();
    t = clock() - t;
    printf("float says: %f after time=%f\n", res2, (double) t / CLOCKS_PER_SEC);
$ gcc -O2 a.c && ./a.out
size of int: 8
size of float: 8
int says: 124 after time=0.702292
float says: 1.250000 after time=2.100958

Here it’s a factor of 3.

Changing the divisor to something less convenient than 2 (to 3) gives higher time, but the factor remains the same:

size of int: 8
size of float: 8
int says: 62 after time=2.100414
float says: 0.562459 after time=6.003749
x = 22 / 7

Precision can be described as,

x | lim x -> exact value

When we round we merely distort the degree of precision.


can never equal the exact,

2 / 3

Thank you! That’s really helpful even though I wasn’t the one originally asking the question.

I have another question based on your answer if you don’t mind answering.

You wrote in your answer "

int_pi = int(pi)

… will parse the integer out of the value.

str_pi = str(pi)

… will cast the value to a string."

What are the two equations? Do we type in, int_pi = int(pi) and str_pi = str(pi) ? What was the significance of that? I think I missed it do you mind elaborating what that means?

Thank you so much for your time!

Where numbers are concerned in Python there are only two types: int and float. One supposes on a factual basis we could include boolean since that is a subclass of int.

As far as data is concerned, we would carry numbers in discernible packaging, typically str. String data is the easiest to transmit, hence CSV. Raw text. A carton of eggs. It is what it is, until it gets interpreted.

If we give Python, int("3.141589") it will give us back, 3.

1 Like

Thank you! That is really amazing advice! I just started coding last year and didn’t get really far since I would focus on each minor detail. Now, I have my composition notebook and just take notes as I go instead of typing EVERYTHING up and trying to understand every little thing.