A Python program is divided into a number of logical lines.
We’ll be covering the following topics in this tutorial:
Physical and logical lines
A physical line is a sequence of characters terminated by an end-of-line sequence. The end of a logical line is represented by the token NEWLINE. A logical line is ignored (i.e. no NEWLINE token is generated) that contains only spaces, tabs, or a comment. This is called “blank line”. The following code
>>> i=5 >>> print(5) 5 is same as: >>> i=5; print(i) 5
A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.
Explicit line joining
Two or more physical lines may be joined into logical lines using backslash characters ( \), as shown in the following example:
>>> if 1900 < year < 2100 and 1 <= month <= 12 \ ... and 1 <= day <= 31 and 0 <= hour < 24 \ ... and 0 <=minute < 60 and 0 <= second < 60: ... print year
A line ending in a backslash cannot carry a comment. Also, backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.
>>> str='This is a \ ... string example' >>> str 'This is a string example'
Implicit line joining
Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. For example:
>>> month_names=['January', 'Feb', 'March', ... 'April', 'May', 'June', ... 'July', 'August', 'September', ... 'October', 'November', 'December']
Implicitly continued lines can carry comments. The indentation of the continuation lines is not important. Blank continuation lines are allowed. There is no NEWLINE token between implicit continuation lines. Implicitly continued lines can also occur within triple-quoted strings; in that case they cannot carry comments.
>>> str="""This is ... a string ... example""" >>> str 'This is \na string \nexample' >>> str='' 'This is ... a string ... example''' >>> str 'This is \na string \nexample'
A comment starts with a hash character(#) that is not part of a string literal, and terminates at the end of the physical line. A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Also, comments are not executed.
Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements. This means that statements which go together must have the same indentation. Each such set of statements is a block. One thing should be remembered is that wrong indentation can give rise to error (IndentationError exception).
>>> i=10 >>>print "Value is ",i Value is 10 >>> print "Value is ",i File "<stdin>", line 1 print "Value is ",i
IndentationError: unexpected indent
The indentation levels of consecutive lines are used to generate INDENT and DEDENT tokens. One can observe that by inserting whitespace in the beginning gave rise to IndentationError exception.
The following example shows non-uniform indentation is not an error.
>>> var=100 >>> if var!=100: ... print 'var does not have value 100' ... else: ...print 'var has value 100' ... var has value 100
Need for indentation
In C programming language, there are numerous ways to place the braces for grouping of statements. If a programmer is habitual of reading and writing code that uses one style, he or she will feel at least slightly uneasy when reading (or being required to write) another style. Many coding styles place begin/end brackets on a line by themselves. This makes programs considerably longer and wastes valuable screen space, making it harder to get a good overview of a program.
Guido van Rossum believes that using indentation for grouping is extremely elegant and contributes a lot to the clarity of the typical Python program. Since there are no begin /end brackets, there cannot be a disagreement between grouping perceived by the parser and the human reader. Also, Python is much less prone to coding-style conflicts.