NEWB: dividing numbers

L

Lo

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
 
M

Michal Wyrebski

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
 
D

Diez B. Roggisch

Lo said:
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.

0.6666666666663


Diez
 
C

Chris Rebert

I just tried python first time.

2/3

the result is zero

I want the result to be .333...

How do I get this?

Add the following to the top of your program:

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
 
M

MRAB

Lo said:
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... ;-)
 
D

Daniel Dalton

Hi,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top