Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99205 > unrolled thread
| Started by | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| First post | 2015-11-21 05:20 -0800 |
| Last post | 2015-11-21 05:55 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
*= operator Cai Gengyang <gengyangcai@gmail.com> - 2015-11-21 05:20 -0800
Re: *= operator BartC <bc@freeuk.com> - 2015-11-21 13:37 +0000
Re: *= operator Steven D'Aprano <steve@pearwood.info> - 2015-11-22 00:39 +1100
Re: *= operator Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-21 08:58 -0500
Re: *= operator "Frank Millman" <frank@chagford.com> - 2015-11-21 15:46 +0200
Re: *= operator Cai Gengyang <gengyangcai@gmail.com> - 2015-11-21 05:55 -0800
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-21 05:20 -0800 |
| Subject | *= operator |
| Message-ID | <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 ?
[toc] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-11-21 13:37 +0000 |
| Message-ID | <n2prun$3du$1@dont-email.me> |
| In reply to | #99205 |
On 21/11/2015 13:20, Cai Gengyang wrote: > 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 ? bill *= 1.08 would mean bill = bill*1.08 (not 1.15; I assume that's a typo). Although you can never be 100% sure in Python (as * or *= could have been redefined to do something else), there's no reason to suspect that here. Also, there's probably some technical difference that someone could up with (*= doing in-place modification for example), but that shouldn't matter when bill is just a number. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-11-22 00:39 +1100 |
| Message-ID | <56507422$0$1594$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #99205 |
On Sun, 22 Nov 2015 12:20 am, Cai Gengyang wrote: > Does bill *= 1.08 mean bill = bill * 1.15 ? No. It means `bill = bill * 1.08`, not 1.15. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-11-21 08:58 -0500 |
| Message-ID | <mailman.36.1448114322.2291.python-list@python.org> |
| In reply to | #99207 |
On Sat, Nov 21, 2015 at 8:39 AM, Steven D'Aprano <steve@pearwood.info> wrote: > On Sun, 22 Nov 2015 12:20 am, Cai Gengyang wrote: > > > Does bill *= 1.08 mean bill = bill * 1.15 ? > > No. It means `bill = bill * 1.08`, not 1.15. > > > > -- > Steven > > -- > https://mail.python.org/mailman/listinfo/python-list > Others have answered, but don't forget to try things like this yourself in the interactive python shell of your choice: Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> bill = 1 >>> bill *=1.08 >>> bill 1.08 >>> -- Joel Goldstick http://joelgoldstick.com/stats/birthdays
[toc] | [prev] | [next] | [standalone]
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Date | 2015-11-21 15:46 +0200 |
| Message-ID | <mailman.35.1448113630.2291.python-list@python.org> |
| In reply to | #99205 |
"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
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-21 05:55 -0800 |
| Message-ID | <43e70c39-30a0-45c6-9414-c98304bf829f@googlegroups.com> |
| In reply to | #99208 |
>>> bill = 100 >>> bill *= 1.08 >>> bill 108.0 >>> On Saturday, November 21, 2015 at 9:47:29 PM UTC+8, Frank Millman wrote: > "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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web