Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22174 > unrolled thread
| Started by | redstone-cold@163.com |
|---|---|
| First post | 2012-03-26 04:45 -0700 |
| Last post | 2012-03-26 19:20 +0200 |
| Articles | 13 — 11 participants |
Back to article view | Back to comp.lang.python
Is there any difference between print 3 and print '3' in Python ? redstone-cold@163.com - 2012-03-26 04:45 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Chris Angelico <rosuav@gmail.com> - 2012-03-26 23:01 +1100
Re: Is there any difference between print 3 and print '3' in Python ? Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-26 14:07 +0200
Re: Is there any difference between print 3 and print '3' in Python ? Robert Kern <robert.kern@gmail.com> - 2012-03-26 13:10 +0100
Re: Is there any difference between print 3 and print '3' in Python ? Dave Angel <d@davea.name> - 2012-03-26 08:11 -0400
Re: Is there any difference between print 3 and print '3' in Python ? redstone-cold@163.com - 2012-03-26 07:28 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Stefan Behnel <stefan_ml@behnel.de> - 2012-03-26 17:03 +0200
Re: Is there any difference between print 3 and print '3' in Python ? redstone-cold@163.com - 2012-03-26 07:28 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-27 03:43 +0000
Re: Is there any difference between print 3 and print '3' in Python ? rusi <rustompmody@gmail.com> - 2012-03-26 22:00 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Terry Reedy <tjreedy@udel.edu> - 2012-03-26 11:45 -0400
Re: Is there any difference between print 3 and print '3' in Python ? "J. Cliff Dyer" <jcd@sdf.lonestar.org> - 2012-03-26 13:06 -0400
Re: Is there any difference between print 3 and print '3' in Python ? Peter Otten <__peter__@web.de> - 2012-03-26 19:20 +0200
| From | redstone-cold@163.com |
|---|---|
| Date | 2012-03-26 04:45 -0700 |
| Subject | Is there any difference between print 3 and print '3' in Python ? |
| Message-ID | <5128580.32.1332762326119.JavaMail.geo-discussion-forums@pbom7> |
I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-03-26 23:01 +1100 |
| Message-ID | <mailman.993.1332763303.3037.python-list@python.org> |
| In reply to | #22174 |
On Mon, Mar 26, 2012 at 10:45 PM, <redstone-cold@163.com> wrote: > I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? One of them takes the integer 3, converts it into a string, and prints it. The other takes the string '3' and prints it. There's a lot of difference under the covers, but both will print a 3, followed by a newline. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Kiuhnm <kiuhnm03.4t.yahoo.it> |
|---|---|
| Date | 2012-03-26 14:07 +0200 |
| Message-ID | <4f705bf1$0$1385$4fafbaef@reader2.news.tin.it> |
| In reply to | #22174 |
On 3/26/2012 13:45, redstone-cold@163.com wrote: > I know the print statement produces the same result when both of > these two instructions are executed ,I just want to know Is there any > difference between print 3 and print '3' in Python ? The former prints a number while the latter a string. Kiuhnm
[toc] | [prev] | [next] | [standalone]
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2012-03-26 13:10 +0100 |
| Message-ID | <mailman.994.1332763876.3037.python-list@python.org> |
| In reply to | #22174 |
On 3/26/12 12:45 PM, redstone-cold@163.com wrote:
> I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?
Yes, there is a difference, but not much.
[~]
|6> import dis
[~]
|7> dis.disassemble(compile('print 3', '<string>', 'exec'))
1 0 LOAD_CONST 0 (3)
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 1 (None)
8 RETURN_VALUE
[~]
|8> dis.disassemble(compile('print "3"', '<string>', 'exec'))
1 0 LOAD_CONST 0 ('3')
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 1 (None)
8 RETURN_VALUE
As you can see, the only difference is in the first instruction. Both of these
put the object that you specified by the literal onto the stack. The difference
is that one is the int object specified by the literal 3 and the other is the
str object specified by the literal "3". Both of these objects happen to give
the same __str__ output, so that's what gets printed.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-03-26 08:11 -0400 |
| Message-ID | <mailman.995.1332763895.3037.python-list@python.org> |
| In reply to | #22174 |
On 03/26/2012 07:45 AM, redstone-cold@163.com wrote: > I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? This is a non-question. The input is the same, the output is the same, what else matters? On the other hand, if you want to dig deeper, there are lots of differences: 1) the former has a shorter source file 2) different C code is utilized inside the interpreter 3) different machine code executes 4) the temporary objects created have different id's and types 5) different execution times (by a trivial amount) 6) it takes different keystrokes to edit the two source files once you want to make it do something useful 7) the processor works a little harder on one than the other, possibly resulting in a different power consumption 8) different byte code is produced Or you could be asking about Python version 3, in which case 1) the syntax error message points to a different character -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | redstone-cold@163.com |
|---|---|
| Date | 2012-03-26 07:28 -0700 |
| Message-ID | <9916924.0.1332772105737.JavaMail.geo-discussion-forums@pbbls7> |
| In reply to | #22181 |
在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: > On 03/26/2012 07:45 AM, redstone-cold@163.com wrote: > > I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? > > On the other hand, if you want to dig deeper, there are lots of differences: > > 1) the former has a shorter source file > 2) different C code is utilized inside the interpreter > 3) different machine code executes > 4) the temporary objects created have different id's and types > 5) different execution times (by a trivial amount) > 6) it takes different keystrokes to edit the two source files once you > want to make it do something useful > 7) the processor works a little harder on one than the other, possibly > resulting in a different power consumption > 8) different byte code is produced > > Or you could be asking about Python version 3, in which case > 1) the syntax error message points to a different character > > -- > > DaveA 在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: > On 03/26/2012 07:45 AM, redstone-cold@163.com wrote: > > I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? > > On the other hand, if you want to dig deeper, there are lots of differences: > > 1) the former has a shorter source file > 2) different C code is utilized inside the interpreter > 3) different machine code executes > 4) the temporary objects created have different id's and types > 5) different execution times (by a trivial amount) > 6) it takes different keystrokes to edit the two source files once you > want to make it do something useful > 7) the processor works a little harder on one than the other, possibly > resulting in a different power consumption > 8) different byte code is produced > > Or you could be asking about Python version 3, in which case > 1) the syntax error message points to a different character > > -- > > DaveA Oh ,God ! I think this is what I really want to know ,thank you very much !
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2012-03-26 17:03 +0200 |
| Message-ID | <mailman.1006.1332774214.3037.python-list@python.org> |
| In reply to | #22191 |
redstone-cold@163.com, 26.03.2012 16:28: > 在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: >> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote: >>> I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? >> >> This is a non-question. The input is the same, the output is the same, >> what else matters? >> >> On the other hand, if you want to dig deeper, there are lots of differences: >> >> 1) the former has a shorter source file >> 2) different C code is utilized inside the interpreter >> 3) different machine code executes >> 4) the temporary objects created have different id's and types >> 5) different execution times (by a trivial amount) >> 6) it takes different keystrokes to edit the two source files once you >> want to make it do something useful >> 7) the processor works a little harder on one than the other, possibly >> resulting in a different power consumption >> 8) different byte code is produced >> >> Or you could be asking about Python version 3, in which case >> 1) the syntax error message points to a different character > > Oh ,God ! I don't think she takes any responsibility for the list above. Stefan
[toc] | [prev] | [next] | [standalone]
| From | redstone-cold@163.com |
|---|---|
| Date | 2012-03-26 07:28 -0700 |
| Message-ID | <mailman.1004.1332772679.3037.python-list@python.org> |
| In reply to | #22181 |
在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: > On 03/26/2012 07:45 AM, redstone-cold@163.com wrote: > > I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? > > On the other hand, if you want to dig deeper, there are lots of differences: > > 1) the former has a shorter source file > 2) different C code is utilized inside the interpreter > 3) different machine code executes > 4) the temporary objects created have different id's and types > 5) different execution times (by a trivial amount) > 6) it takes different keystrokes to edit the two source files once you > want to make it do something useful > 7) the processor works a little harder on one than the other, possibly > resulting in a different power consumption > 8) different byte code is produced > > Or you could be asking about Python version 3, in which case > 1) the syntax error message points to a different character > > -- > > DaveA 在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: > On 03/26/2012 07:45 AM, redstone-cold@163.com wrote: > > I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? > > On the other hand, if you want to dig deeper, there are lots of differences: > > 1) the former has a shorter source file > 2) different C code is utilized inside the interpreter > 3) different machine code executes > 4) the temporary objects created have different id's and types > 5) different execution times (by a trivial amount) > 6) it takes different keystrokes to edit the two source files once you > want to make it do something useful > 7) the processor works a little harder on one than the other, possibly > resulting in a different power consumption > 8) different byte code is produced > > Or you could be asking about Python version 3, in which case > 1) the syntax error message points to a different character > > -- > > DaveA Oh ,God ! I think this is what I really want to know ,thank you very much !
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-03-27 03:43 +0000 |
| Message-ID | <4f713772$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #22181 |
On Mon, 26 Mar 2012 08:11:03 -0400, Dave Angel wrote:
> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
>> I know the print statement produces the same result when both of these
>> two instructions are executed ,I just want to know Is there any
>> difference between print 3 and print '3' in Python ?
>
> This is a non-question. The input is the same, the output is the same,
> what else matters?
def fib1(n):
if n == 0: return 0
elif n == 1: return 1
f2, f1 = 0, 1
for _ in range(2, n+1):
f2, f1 = f1, f2 + f1
return f1
def fib2(n):
if n == 0: return 0
elif n == 1: return 1
else: return fib2(n-1) + fib2(n-2)
Try calling fib1(35) and fib2(35). Still think only the input and output
matter? :)
For the record, fib2(35) ends up making a total of 29860703 function
calls, compared to 35 iterations for fib1.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2012-03-26 22:00 -0700 |
| Message-ID | <0e7412b9-8443-49d9-b9d9-5dd3f08d2054@to5g2000pbc.googlegroups.com> |
| In reply to | #22225 |
On Mar 27, 8:43 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > On Mon, 26 Mar 2012 08:11:03 -0400, Dave Angel wrote: > > On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: > >> I know the print statement produces the same result when both of these > >> two instructions are executed ,I just want to know Is there any > >> difference between print 3 and print '3' in Python ? > > > This is a non-question. The input is the same, the output is the same, > > what else matters? > > def fib1(n): > if n == 0: return 0 > elif n == 1: return 1 > f2, f1 = 0, 1 > for _ in range(2, n+1): > f2, f1 = f1, f2 + f1 > return f1 > > def fib2(n): > if n == 0: return 0 > elif n == 1: return 1 > else: return fib2(n-1) + fib2(n-2) > > Try calling fib1(35) and fib2(35). Still think only the input and output > matter? :) > > For the record, fib2(35) ends up making a total of 29860703 function > calls, compared to 35 iterations for fib1. > > -- > Steven http://www.cse.buffalo.edu/~rapaport/intensional.html
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-03-26 11:45 -0400 |
| Message-ID | <mailman.1009.1332776769.3037.python-list@python.org> |
| In reply to | #22174 |
On 3/26/2012 7:45 AM, redstone-cold@163.com wrote:
> I know the print statement produces the same result when both of
> these two instructions are executed ,I just want to know Is there any
> difference between print 3 and print '3' in Python ?
If you want to see the difference between the number and string
representation thereof, use repr(x).
>>> print(repr(3), repr('3'), [3, '3'])
3 '3' [3, '3']
Note that printing a collection prints the repr() of each item precisely
so one can tell the difference between the item being a number or a string.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | "J. Cliff Dyer" <jcd@sdf.lonestar.org> |
|---|---|
| Date | 2012-03-26 13:06 -0400 |
| Message-ID | <mailman.1014.1332781633.3037.python-list@python.org> |
| In reply to | #22174 |
As others have pointed out, the output is the same, because the result of converting an integer to a string is the string of that integer. However, other numeric literals might not do what you want, due to the fact that they are converted to an internal numeric representation, then converted back to a string in a canonical format. >>> print 3, '3' 3 3 >>> print 3.00, '3.00' 3.0 3.00 >>> print 0x3, '0x3' 3 0x3 >>> print 03, '03' 3 03 >>> print 3e0, '3e0' 3.0 3e0 You might think that the take away message is to use the string representation, since it prints what you tell it to print. However, if you use a number, you can specify the output formatting with more fine-grained control, and even exert that control on calculated number: >>> print '%0.2f' % (3,) 3.00 >>> print '%0.2f' % (2 + 1) 3.00 This is better because you can't perform math on a string: >>> print '2' + '1' 21 >>> print '2.00' + '1.00' 2.001.00 print '2 + 1' 2 + 1 So in general, you should use numbers, and then format them using standard string formatting operations when you want to print them. There's more information on how to do formatting here: http://docs.python.org/library/stdtypes.html#string-formatting Cheers, Cliff On Mon, 2012-03-26 at 04:45 -0700, redstone-cold@163.com wrote: > I know the print statement produces the same result when both of these > two instructions are executed ,I just want to know Is there any > difference between print 3 and print '3' in Python ?
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-03-26 19:20 +0200 |
| Message-ID | <mailman.1015.1332782388.3037.python-list@python.org> |
| In reply to | #22174 |
redstone-cold@163.com wrote: > I know the print statement produces the same result when both of these two > instructions are executed ,I just want to know Is there any difference > between print 3 and print '3' in Python ? The question you really wanted to ask is: under what circumstances will the two statements produce different output? Here's what I found: $ PYTHONIOENCODING=hex python -c'from __future__ import unicode_literals print 3' 3 $ PYTHONIOENCODING=hex python -c'from __future__ import unicode_literals print "3"' 33 $ :)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web