Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24999 > unrolled thread
| Started by | iMath <redstone-cold@163.com> |
|---|---|
| First post | 2012-07-06 21:56 -0700 |
| Last post | 2012-07-07 07:21 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
What’s the differences between these two pieces of code ? iMath <redstone-cold@163.com> - 2012-07-06 21:56 -0700
Re: What’s the differences between these two pieces of code ? Gary Herron <gherron@digipen.edu> - 2012-07-06 22:41 -0700
Re: What’s the differences between these two pieces of code ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-07 07:21 +0000
| From | iMath <redstone-cold@163.com> |
|---|---|
| Date | 2012-07-06 21:56 -0700 |
| Subject | What’s the differences between these two pieces of code ? |
| Message-ID | <48fbfc85-c495-4131-a18a-1ebb94e7ce20@googlegroups.com> |
What’s the differences between these two pieces of code ?
(1)
for i in range(1, 7):
print(2 * i, end=' ')
(2)
for i in range(1, 7):
print(2 * i, end=' ')
print()
when executed both respectively in Python shell ,I get the same effect . Who can tell me why ?
[toc] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2012-07-06 22:41 -0700 |
| Message-ID | <mailman.1883.1341640207.4697.python-list@python.org> |
| In reply to | #24999 |
On 07/06/2012 09:56 PM, iMath wrote: > What’s the differences between these two pieces of code ? > (1) > for i in range(1, 7): > print(2 * i, end=' ') > > > (2) > for i in range(1, 7): > print(2 * i, end=' ') > print() > > > when executed both respectively in Python shell ,I get the same effect . Who can tell me why ? What "effect" are you referring to? What did you expect? What did you get? What version of Python? (3 I'd guess). As for me, the first one fails because of a syntax (indentation) error and the second prints the even numbers 2 through 12. What are we supposed to be comparing? Gary Herron -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-07-07 07:21 +0000 |
| Message-ID | <4ff7e384$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #24999 |
On Fri, 06 Jul 2012 21:56:35 -0700, iMath wrote: > What’s the differences between these two pieces of code ? Have you tried it? What do you see? Obviously the difference is that the second piece calls print() at the end, and the first does not. Since the for-loops are identical, we can ignore them. The difference between the two pieces of code is the same as between (1) (do nothing at all) and (2) call print() which should be obvious: doing nothing does nothing. Calling print() prints a blank line. More details below. > (1) > for i in range(1, 7): > print(2 * i, end=' ') > > > (2) > for i in range(1, 7): > print(2 * i, end=' ') > print() > > > when executed both respectively in Python shell ,I get the same > effect . Who can tell me why ? No you don't get the same effect. At least not with the code as given. The first gives a SyntaxError, as the indentation is missing. If you fix that problem by inserting the appropriate indentation, it prints the even numbers from 2 to 12, ending each number with a triple space ' ' instead of a newline so they all appear on the same line. Because no newline gets printed, the prompt appears on the same line: py> for i in range(1, 7): ... print(2 * i, end=' ') ... 2 4 6 8 10 12 py> (Notice that I use "py>" as my prompt instead of ">>>".) The second one as given also gives a SyntaxError, due to a limitation of the Python interactive interpreter. (When you outdent a level, you need to leave a blank line. The non-interactive interpreter does not have this limitation.) Fixing that problem by inserting a blank line, you get exactly the same numbers printed (of course! the for loops are identical), except at the end, after the for-loop has finished, you also call print(), which gives you this output: py> for i in range(1, 7): ... print(2 * i, end=' ') ... 2 4 6 8 10 12 py> print() py> Notice the blank line printed? -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web