Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Frank Millman" Newsgroups: comp.lang.python Subject: Re: *= operator Date: Sat, 21 Nov 2015 15:46:46 +0200 Lines: 59 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de b58rq2im8N8o+jHzyf8WuwijzDQUswiBQYS5pgqZ6G7Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'modified': 0.05; 'assignment': 0.07; 'assigning': 0.09; 'augmented': 0.09; 'effect.': 0.09; 'get.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'similar,': 0.09; 'target,': 0.09; 'python': 0.10; 'assume': 0.11; 'index': 0.13; 'def': 0.13; 'interpreter': 0.15; '"an': 0.16; 'calculates': 0.16; 'excerpt': 0.16; 'in-place,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'rewritten': 0.16; '>>>': 0.20; 'meant': 0.22; 'skip:n 60': 0.22; 'bit': 0.23; 'feb': 0.23; 'wrote': 0.23; 'header:In-Reply-To:1': 0.24; 'header:X-Complaints-To:1': 0.26; 'question': 0.27; 'skip:( 20': 0.28; 'actual': 0.28; 'fine': 0.28; 'omitted': 0.29; 'once.': 0.29; 'print': 0.30; 'creating': 0.30; 'code': 0.30; 'version,': 0.30; 'bill': 0.32; 'equal': 0.34; 'possible,': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'two': 0.37; 'received:org': 0.37; 'mean': 0.38; 'end': 0.39; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'here': 0.66; 'frank': 0.72; 'yourself': 0.73; '100': 0.79; 'headed': 0.84; 'tax:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.89.92.4 In-Reply-To: X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 15.4.3502.922 X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3502.922 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99208 "Cai Gengyang" wrote in message news:a76b1b5b-4321-41bb-aeca-0dac787752d9@googlegroups.com... > This is a piece of code that calculates tax and tip : > > def tax(bill): > """Adds 8% tax to a restaurant bill.""" > bill *= 1.08 > print "With tax: %f" % bill > return bill > > def tip(bill): > """Adds 15% tip to a restaurant bill.""" > bill *= 1.15 > print "With tip: %f" % bill > return bill > > meal_cost = 100 > meal_with_tax = tax(meal_cost) > meal_with_tip = tip(meal_with_tax) > > Does bill *= 1.08 mean bill = bill * 1.15 ? Firstly, I assume that you actually meant 'bill = bill * 1.08' at the end of the last line. Secondly, how can I help you to answer this kind of question yourself. Here are two ways. 1. Try it out at the interpreter - c:\> Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> bill = 100 >>> bill *= 1.08 >>> bill I deliberately omitted the last line. Try it yourself and see what you get. 2. Read the fine manual. The Index has a section headed 'Symbols'. From there you will find '*=', with a link to 'augmented assignment'. If you follow the link, you will find a detailed explanation. Here is an excerpt - "An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead." Frank Millman