python only prints integers

Discussion in 'Python' started by francesco, Jan 6, 2011.

  1. francesco

    francesco Guest

    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
    francesco, Jan 6, 2011
    #1
    1. Advertising

  2. On Thu, Jan 6, 2011 at 2:49 PM, francesco
    <> 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
    geremy condra, Jan 6, 2011
    #2
    1. Advertising

  3. francesco

    Ian Guest

    On Jan 6, 3:49 pm, francesco <> 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.
    Ian, Jan 6, 2011
    #3
  4. francesco

    kost BebiX Guest

    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" <>:
    > 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, Jan 6, 2011
    #4
  5. francesco

    francesco Guest

    On 6 Gen, 23:59, Ian <> wrote:
    > On Jan 6, 3:49 pm, francesco <> 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
    francesco, Jan 6, 2011
    #5
  6. In article <>,
    Ian <> wrote:
    >
    >In Python 3, the '/' operator always performs true division.


    How can I get integer division?

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
    Edward A. Falk, Jan 7, 2011
    #6
  7. On Fri, 07 Jan 2011 03:32:54 +0000, Edward A. Falk wrote:

    > In article
    > <>, Ian
    > <> wrote:
    >>
    >>In Python 3, the '/' operator always performs true division.

    >
    > How can I get integer division?



    >>> 25//4

    6


    --
    Steven
    Steven D'Aprano, Jan 7, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kitty
    Replies:
    0
    Views:
    566
    kitty
    Jul 18, 2005
  2. kitty
    Replies:
    0
    Views:
    425
    kitty
    Jul 18, 2005
  3. Replies:
    7
    Views:
    777
  4. Juerg Beck

    FF prints only first page

    Juerg Beck, Aug 30, 2007, in forum: HTML
    Replies:
    6
    Views:
    619
    Jonathan N. Little
    Aug 30, 2007
  5. daved170

    Looger object only prints "ERROR"s

    daved170, Sep 24, 2009, in forum: Python
    Replies:
    3
    Views:
    252
    Vinay Sajip
    Sep 24, 2009
Loading...

Share This Page