Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   python only prints integers (http://www.velocityreviews.com/forums/t741653-python-only-prints-integers.html)

francesco 01-06-2011 10:49 PM

python only prints integers
 
I'm pretty new in Python language. I have a problem with numbers: it
seems python doesn't know any more how to count!
I get only the down rounded integer
20/8 = 2
8/3=2
I probably changed some option to round the numbers, but I don't
remember how.
Is there a way to reset the number of digits to default?
Thanks in advance

geremy condra 01-06-2011 10:58 PM

Re: python only prints integers
 
On Thu, Jan 6, 2011 at 2:49 PM, francesco
<cerutti.francesco.to@gmail.com> wrote:
> I'm pretty new in Python language. I have a problem with numbers: it
> seems python doesn't know any more how to count!
> I get only the down rounded integer
> 20/8 = 2
> 8/3=2
> I probably changed some option to round the numbers, but I don't
> remember how.
> Is there a way to reset the number of digits to default?
> Thanks in advance


Use floats instead of integers:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 20/8

2
>>> 20.0/8

2.5


or use Python3:

Python 3.2a1 (r32a1:83318, Aug 13 2010, 22:32:03)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 20/8

2.5
>>> 20.0/8

2.5

Ian 01-06-2011 10:59 PM

Re: python only prints integers
 
On Jan 6, 3:49*pm, francesco <cerutti.francesco...@gmail.com> wrote:
> I'm pretty new in Python language. I have a problem with numbers: it
> seems python doesn't know any more how to count!
> I get only the down rounded integer
> 20/8 = 2
> 8/3=2
> I probably changed some option to round the numbers, but I don't
> remember how.
> Is there a way to reset the number of digits to default?


In Python 2, the '/' operator performs integer division by default
when both its operands are integers. To change this, either place
this at the top of the file:

from __future__ import division

or convert your numbers to floats:

>>> 20.0 / 8.0

2.5
>>> float(20) / float(8)

2.5

In Python 3, the '/' operator always performs true division.

kost BebiX 01-06-2011 11:09 PM

Re: python only prints integers
 
Well, that's because 20 is integer. To get float you can write 20.0 (or 20.).
20.0/8.0 = 2.5
8.0/3.0 = 2.6666666666666665

07.01.2011, 00:49, "francesco" <cerutti.francesco.to@gmail.com>:
> I'm pretty new in Python language. I have a problem with numbers: it
> seems python doesn't know any more how to count!
> I get only the down rounded integer
> 20/8 = 2
> 8/3=2
> I probably changed some option to round the numbers, but I don't
> remember how.
> Is there a way to reset the number of digits to default?
> Thanks in advance
>
> --
> http://mail.python.org/mailman/listinfo/python-list


--
jabber: kost-bebix@ya.ru

francesco 01-06-2011 11:12 PM

Re: python only prints integers
 
On 6 Gen, 23:59, Ian <ian.g.ke...@gmail.com> wrote:
> On Jan 6, 3:49*pm, francesco <cerutti.francesco...@gmail.com> wrote:
>
> > I'm pretty new in Python language. I have a problem with numbers: it
> > seems python doesn't know any more how to count!
> > I get only the down rounded integer
> > 20/8 = 2
> > 8/3=2
> > I probably changed some option to round the numbers, but I don't
> > remember how.
> > Is there a way to reset the number of digits to default?

>
> In Python 2, the '/' operator performs integer division by default
> when both its operands are integers. *To change this, either place
> this at the top of the file:
>
> from __future__ import division
>
> or convert your numbers to floats:
>
> >>> 20.0 / 8.0

> 2.5
> >>> float(20) / float(8)

>
> 2.5
>
> In Python 3, the '/' operator always performs true division.


Thanks to all! Very quick answer!
I fixed the problem by using floats.
Thanks again

Edward A. Falk 01-07-2011 03:32 AM

Re: python only prints integers
 
In article <cd9d1c80-b1d2-4d20-9896-a6fd77bd7db2@j25g2000yqa.googlegroups.com>,
Ian <ian.g.kelly@gmail.com> wrote:
>
>In Python 3, the '/' operator always performs true division.


How can I get integer division?

--
-Ed Falk, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/

Steven D'Aprano 01-07-2011 07:09 AM

Re: python only prints integers
 
On Fri, 07 Jan 2011 03:32:54 +0000, Edward A. Falk wrote:

> In article
> <cd9d1c80-b1d2-4d20-9896-a6fd77bd7db2@j25g2000yqa.googlegroups.com>, Ian
> <ian.g.kelly@gmail.com> wrote:
>>
>>In Python 3, the '/' operator always performs true division.

>
> How can I get integer division?



>>> 25//4

6


--
Steven


All times are GMT. The time now is 03:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.