# Why is it invalid to add a period after a variable name to make it a float?

#1

### Question

I tried putting a period after my variable name to make it a float so my result would be a float, but it gave an error. Why is this?

While it’s perfectly valid to make an integer a float by typing a period after the number, we cannot do the same to a variable, even though an integer is stored inside of it. If you try doing something like `cucumbers.` it will give you an error that says invalid syntax because a period after a variable name does not do what we want in this case.

Instead, we make use of the handy, built-in `float()` function! You can use it by writing any number or string between its parentheses, like so:
`float(cucumber)`

FAQ: Learn Python - Python Syntax - Two Types of Division
#2

You can just use (cucumbers+.0) instead of cucumber.

#3

or you can just use (cucumbers* 1.0)

#4

Or you can simply multiply by a float…

``````cucumbers * price_per_cucumber
``````

…given that the price is set at 3.25. No finagling involved.

Aside

This is a subject that will have little meaning in a couple year’s time since Python 2.7 is going dark sometime after 2020. But for the meantime, it is still meaningful to have some guidelines when it comes to coercing floats.

``````7 / 2                   =>  3

7 / 2.                  =>  3.5

sum(range(1,10)) / 9    =>  5

sum(range(1,11)) / 10   =>  5

sum(range(1,11)) / 10.  =>  5.5
``````

On the command line this sort of shortcut is useful, but in code it is not. The more likely place to find a float in this rational is in the expression in the numerator. Maybe not in this instance but by-the-by in other dynamic situations. A counting number is certainly not a float and we should not entertain the fact that it is, or should be coerced to one just for expediency.

``````>>> float(sum(range(1,11))) / 10
5.5
>>>
``````

#5

A post was split to a new topic: Can not call the function

#6

cucumbers = 100
num_people = 6
whole_cucumbers_per_person = 100/6
print whole_cucumbers_per_person
float_cucumbers_per_person = 100./6
print float_cucumbers_per_person

Is this right? Ive never seen how to use the one in brackets and even if the instruction says its right Im not entirely sure since you guys are using () thing

#7

When a number literal is followed immediately by a period (dot) it is represented as a float for evaluation purposes.

``````>>> print (100.)
100.0
>>>
``````

However, a variable is not a number literal, but a reference pointer to a value stored in memory. We are not acting directly upon the number, but on the reference, which will raise a Syntax error…

``````>>> a = 100
>>> print (a.)
SyntaxError: invalid syntax
>>>
``````

#9

It’s a very badly written exercise in my opinion. The only time it even mentions Float() is in the hints and at the community forums section.
The actual exercise makes no mention of it despite it being what they’re trying to get you to do.

This seems to be the solution it’s trying to get from you but nothing points to that.

``````cucumbers = 100
num_people = 6
whole_cucumbers_per_person = cucumbers/num_people
print(whole_cucumbers_per_person)

float_cucumbers_per_person = float(cucumbers)/float(num_people)
``````

#10

Only one operand needs to be cast as a float.

``````float(a) / b
``````

or,

``````100. / 6
``````

will return a float.

Python 3 eleviates this by forcing all rationals to return a float by default.

``````10 / 1 => 10.0
``````

but Python 2 requires us to declare it.

#11

Thanks for explaining. I just thought i’d shed some light on where I got confused in the hopes it might save someone else’ time.

Is there any particular reason you’d want to have both as a float? The exercise itself does claim

To yield a float as the result instead, programmers often change either the numerator or the denominator (or both) to be a float:

I just took this as being a good practice thing or something.

#12

Simple is better than complex, so write for `meaning`, with purpose fairly clear and evident. As the programmer, you control the inputs and the output. How one composes the code is arbitrary, based on desired outcome.

As for setting both to float, that would not be necessary, ever. If the number in the denominator is a counting number, the it should rightfully be left as a counting number (integer).

``````100 / 6  => 16 (Python 2)

100 / 6 => 16.666666666666668 (Python 3)

100. / 6 => 16.666666666666668 (Python 2)
``````

If we know that both values are floats (literal or variable) then casting is not needed.

#13

guys, guys, guys! what is a ‘float’? are we talking about floating decimals??

#14

Decimal fractions are not natural to binary, so computers implement a special brand of math called `floating point arithmetic`. It is from this term that we get the word, `float`.

#15

in other words, Yes?

Python is binary code? a bunch of 0s and 1s?

#16

No. There is no such term as `floating decimals`. Floating point arithmetic is very complex. The computer has a special chip called the math co-processor, or MPU to deal with number operations.

The closer term to describe the type of numbers, is Real. Floats are Real numbers.

``````1, 2, 3, ...                          Natural Numbers

... -1, 0, 1, 2, ...                  Integers

... -1.1, -0.6, 0, 1.1, 1.6, 2.1, ... Real Numbers (floats)
``````

Technically speaking, floats are numbers between 0 and 1, such as those generated by `random.random()`.

#17

adding machines have a switch option for something like ⅝, floating, and a couple of other options. I always took the “floating” to mean you could key 1.1 + 2.12 and the machine could line up the decimals correctly without the user having to key 1.10 to make it add correctly.

not sure I’m making any sense…

and was I right about binary being 0s and 1s?

#18

Adding machines have a fixed decimal point. It cannot float left or right, but will always appear in the same print column.

The 5/4 key is for rounding. Up on 5, down on 4.

Yes, hence the term `binary`, which means `two` as in base 2.

``decimal 10  =>  0b1010 binary``

#19

ok. You lost me on that last bit. the 5/4s makes sense. It’s amazing what you can forget over a period of a few years (ok. more like 40 years…). and look what I found:
What is floating decimal mode?
Definition of floating decimal. : a system of decimal point placement in an electronic calculator in which the decimal point is free to move automatically across the display in order to allow the maximum number of decimal places in the readout.
Floating Decimal | Definition of Floating Decimal by Merriam-Webster
https://www.merriam-webster.com/dictionary/floating%20decimal

I’m not ALL crazy. just part.crazy. lol!

#20

The key word is `electronic` which incorporates an LED display. Later on they went to crystal displays with pixels. In either case, they were more flexible for users than adding machines with only a paper tape.

So the term does exist (my bad) but does not apply, here. Google floating point arithmetic for a richer explanation. Expect it to be a tough read.

#21

…and math used to be a strong point. My son is a math teacher. I should hire him to tutor me.

this convo won’t go away, will it? I need to copy all this down and put it somewhere I’ll remember. Thanks for all your help!