# NEWB: dividing numbers

Discussion in 'Python' started by Lo, Mar 8, 2009.

1. ### LoGuest

I just tried python first time.

2/3

the result is zero

I want the result to be .333...

How do I get this?

Thanks a lot

L

Lo, Mar 8, 2009

2. ### Michal WyrebskiGuest

Lo pisze:
> I just tried python first time.
>
> 2/3
>
> the result is zero

Float type must be specified explicitly:
2/3.0 or 2.0/3

In Python 3.x operators behave differently and '2/3' would give float
number as a result.

>
> I want the result to be .333...

Than try: 1/3.0 because 2/3.0 will never show you that

Michal

Michal Wyrebski, Mar 8, 2009

3. ### Diez B. RoggischGuest

Lo schrieb:
> I just tried python first time.
>
> 2/3
>
> the result is zero
>
> I want the result to be .333...

Well, that's not going to happen - 2/3 is .666 if not done with integers...

>
> How do I get this?

Use floating points.

>>> 2.0 / 3.0

0.6666666666663

Diez

Diez B. Roggisch, Mar 8, 2009
4. ### Chris RebertGuest

On Sun, Mar 8, 2009 at 3:08 PM, Lo <> wrote:
> I just tried python first time.
>
> 2/3
>
> the result is zero
>
> I want the result to be .333...
>
> How do I get this?

from __future__ import division

That tells Python to use the proper kind of division, which is now the
default in Python 3.0.

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com

Chris Rebert, Mar 8, 2009
5. ### MRABGuest

Lo wrote:
> I just tried python first time.
>
> 2/3
>
> the result is zero
>
> I want the result to be .333...
>
> How do I get this?
>

That's integer division (integer divided by integer is integer).

If you want the result to be floating point then make one of them
floating point:

2.0 / 3

or do this first:

from __future__ import division

In the future and in Python 3.x "/" will always return floating point.
For an integer result use "//" instead.

BTW, 2.0/3.0 still isn't 0.333... ;-)

MRAB, Mar 8, 2009
6. ### Daniel DaltonGuest

Hi,

On Mon, Mar 09, 2009 at 12:08:16AM +0100, Lo wrote:
> I just tried python first time.
>
> 2/3
>
> the result is zero

That's because your dividing an int by an int to an int. The definition
of an int is a "whole number". So just use floating point I think it's
called, this should work, and does just tested with the python
interactive shell:
2.0/3

Cheers,
Daniel.

Daniel Dalton, Mar 9, 2009