Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'output': 0.05; 'remind': 0.05; 'python3': 0.07; 'subject:code': 0.07; 'integers': 0.09; 'measure': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.11; 'python': 0.11; 'def': 0.12; 'finney': 0.16; 'integer.': 0.16; 'integers,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'xrange': 0.16; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'decimal': 0.31; 'types.': 0.31; 'writes:': 0.31; 'url:python': 0.33; 'subject: (': 0.35; 'subject:with': 0.35; 'but': 0.35; 'opposed': 0.36; 'url:org': 0.36; 'represent': 0.38; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'numbers': 0.61; 'url:3': 0.61; "you're": 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'myself': 0.63; 'skip:\xe2 10': 0.65; 'here': 0.66; '8bit%:40': 0.68; 'day': 0.76; 'subject:this': 0.83; 'hundred': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Representing fractions (was: Help me with this code) Date: Wed, 06 Nov 2013 13:06:41 +1100 References: <612c0028-ae66-4200-b9a5-a613eca94762@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: rasputin.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:nnnSlgjyU0qUFzsGYKy8Fga3Zkg= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383703613 news.xs4all.nl 15903 [2001:888:2000:d::a6]:59249 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58544 chovdary@gmail.com writes: > def sequence_b(N): > N = 10 > result = 0 > for k in xrange (1,N): > result += ((-1) ** (k+1))/2*k-1 Every number here is an integer. Python 2 will keep all the computations integers by default:: $ python2 >>> 1 / 2 0 You want to use Python 3, which does “true division” by default:: $ python3 >>> 1 / 2 0.5 > But i want output as > 1 > -1/3 > 1/5 > -1/7 > 1/9 > -1/11 > 1/13 > -1/15 > 1/17 > -1/19 You're not going to get that output from Python built-in number types. If you want numbers which represent fractions (as opposed to integers, or decimal numbers, or floating-point numbers), you want the ‘fractions’ module which will represent the number explicitly with numerator and denominator:: $ python3 >>> import fractions >>> result = fractions.Fraction(1) / fractions.Fraction(2) >>> result Fraction(1, 2) >>> print(result) 1/2 -- \ “A hundred times every day I remind myself that […] I must | `\ exert myself in order to give in the same measure as I have | _o__) received and am still receiving” —Albert Einstein | Ben Finney