python base - line

Logical line, physical line

  • Physical line: programmers wrote the code row.
  • Logical line: refers to a pre-compiled source code through the code where the row.

Python is assumed that each row corresponds to a physical logical line. For example: print ( "Hello World") is a physical line, Python hope that every row, only one statement, because it seems easier to read.

If you want a physical line in more than one logical line, then you need to use a semicolon (;) to indicate this particular usage. Semicolon indicates the end of a logical line / statement.

E.g:

>>> count=5
>>> print(count)
5

This is equivalent to the following:

>>> count=5;
>>> print(count);
5

This can also be written as the following:

>>> count=5;print(count);
5

Even can be written like this:

>>> count=5;print(count)
5

However, it is strongly recommended that insist on writing only one logical line in a physical line, because as much as possible to avoid this is to use a semicolon to make the code more readable.

An example of writing a logical line in the plurality of physical lines in the example, it is called explicit row connector.

>>> s="I am a boy, \
... she is a birl."
>>> print(s)
I am a boy, she is a birl.

 

 

Multi-line

  • Python is usually a one liner statement, but if the statement is very long, we can use a backslash (\) to achieve multi-line statement
>>> s=1+2\
... +3+\
... 4
>>> s
10
  • In [], {}, or () statement in a plurality of rows, without using a backslash (\)
>>> list=['a',
... 'b',
... 'c']
>>> list
['a', 'b', 'c']

 

Not wrap

The default is to print print plus end of a line wrap. end = ' "" Si is not the end of the wrap, spaces. Example:

>>> print("hello world!")  #不加end效果
hello world!
>>> print("hello world!",end=" ")
hello world! >>>
>>> print("hello world!",end="     ")
hello world!     >>>

 

 

 

 

Blank line

Separated by a blank line between the function or class method, it indicates start a new code. Also separated by a blank line between the class and the function entry, function entry to highlight start.

Blank lines of code indentation with a different part of the blank line is not a Python syntax. Do not insert a blank line when writing, Python interpreter to run it will not go wrong. However, the role that the partition blank line meaning two different functions or code to facilitate future maintenance or reconstructed code.

Guess you like

Origin www.cnblogs.com/wenm1128/p/11549646.html