Which means "the interpreter inserts a new line before printing the next prompt if the last line was not completed."

I am learning Python by reading the Python Tutorial of python.org. When I read the third chapter: 3. An informal introduction to Python , I cannot understand the last sentence of the article, in which "the interpreter inserts a new line before it prints out the next prompt if the last line has not been completed." Does anyone know what this means? Better if there is an example. Thank.

+3


source to share


3 answers


Every time you use a statement print

that ends with a comma, the interpreter sets a flag to remember that it hasn't written a new line yet.

Since it would be awkward to place the next prompt on the same line as the numbers (it won't be in the leftmost column as you'd expect), Python will write a newline character so that the message ends in the left column; it uses this flag set by the operator print

to detect this situation.

So, instead, in an interactive session:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> 

      



you see the following:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> 

      

Interestingly, in Python 3, this flag is no longer set because it is print()

now a function. As a result, the interactive interpreter can no longer detect this situation and enters a hint immediately after the numbers:

>>> import sys
>>> sys.version
'3.4.2 (default, Feb 10 2015, 10:25:29) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]'
>>> a, b = 0, 1
>>> while b < 1000:
...     print(b, end=',')
...     a, b = b, a+b
... 
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,>>> 

      

+5


source


This will not happen:

>>> a, b = 0, 1
>>> while b < 1000:
...     print b,
...     a, b = b, a+b
...
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> (prompt here)

      

Instead, this happens:



>>> a, b = 0, 1
>>> while b < 1000:
...     print b,
...     a, b = b, a+b
...
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> (prompt here, after a newline)

      

Even though the line was never "completed": any subsequent instructions print x,

would continue it.

+2


source


The tutorial you are referring to uses the following snippet as an example

a, b = 0, 1
while b < 1000:
    print b,
    a, b = b, a+b

      

The sentence you don't understand refers to how the line ends after the final print. The comma tells the interpreter that there is no newline on the current line, which can be easily rendered by doing something like this

a, b = 0, 1
while b < 1000:
    print b,
    a, b = b, a+b
print "Look where I am"

      

This will print this final statement on the same line as your numbers, like

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 Look where I am

      

Now by adding another print below the previous one (which does not have a comma, so it has a new line), you will see that the new statement will be printed below the Look where I state

a, b = 0, 1
while b < 1000:
    print b,
    a, b = b, a+b
print "Look where I am"
print "And me"

      

This gives the following output

 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 Look where I am
 And me

      

+2


source







All Articles