Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What’s the differences between these two pieces of code ?

57 views
Skip to first unread message

iMath

unread,
Jul 7, 2012, 12:56:35 AM7/7/12
to
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 ?

Gary Herron

unread,
Jul 7, 2012, 1:41:04 AM7/7/12
to pytho...@python.org
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


Steven D'Aprano

unread,
Jul 7, 2012, 3:21:41 AM7/7/12
to
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

iMath

unread,
Sep 9, 2012, 9:27:53 AM9/9/12
to
在 2012年7月7日星期六UTC+8下午12时56分35秒,iMath写道:
> What’s the differences between these two pieces of code ?
>
> (1)
>
> for i in range(1, 7):
>
> print(2 * i, end=' ')
>
>
>
> thx everyone

Kwpolska

unread,
Sep 9, 2012, 1:07:06 PM9/9/12
to iMath, pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list

Well, (2) is inserting an additional newline, and (1) isn’t. The
shell might not show that, but try running this as a standalone
script.

--
Kwpolska <http://kwpolska.tk>
stop html mail | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16

Piet van Oostrum

unread,
Sep 9, 2012, 2:29:57 PM9/9/12
to
The first one gives a syntax error (IndentationError: expected an indented block)
--
Piet van Oostrum <pi...@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
0 new messages