Sideway
output.to from Sideway
Draft for Information Only

Content

Python program
 Python Language Structure
  Line Structure
   Logical Lines
   Physical lines
  Line Joining
   Explicit Line Joining
   Implicit Line Joining
   Multiple Logical Line in One Physical Line
  Blank Lines
  Whitespace Between Tokens
  Indentation
  Other Tokens
 Source and Reference

Python program

A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer.

Python Language Structure

A Python program can be divided into a number of script lines.

Line Structure

There are two kinds of line structures.

Logical Lines

Logical line is a complete Python sentence. The end of a logical line is represented by the token NEWLINE. Statements usually cannot cross logical line boundaries except where NEWLINE is allowed by the syntax, e.g. between statements in compound statements. A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

Physical lines

A physical line is a sequence of characters terminated by an end-of-line sequence. In source files and strings, any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of platform. The end of input also serves as an implicit terminator for the final physical line. When embedding Python, source code strings should be passed to Python APIs using the standard C conventions for newline characters (the \n character, representing ASCII LF, is the line terminator).

Line Joining

Explicit Line Joining

Two or more physical lines may be joined into logical lines using backslash characters \, as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For 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: # Looks like a valid date return 1 A line ending in a backslash cannot carry a comment. A 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.

Implicit Line Joining

Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslasher. For example: month_names = ['Januari', 'Februari', 'Maart', # These are the 'April', 'Mei', 'Juni', # Dutch names 'Juli', 'Augustus', 'September', # for the months 'Oktober', 'November', 'December'] # of the year 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.

Multiple Logical Line in One Physical Line

If more than one logical line is specified in a physical line, then a semicolon ; can be used to specify explicitly to indicate the end of a logical line/statement.

Blank Lines

A logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored (i.e., no NEWLINE token is generated). During interactive input of statements, handling of a blank line may differ depending on the implementation of the read-eval-print loop. In the standard interactive interpreter, an entirely blank logical line (i.e. one containing not even whitespace or a comment) terminates a multi-line statement.

Whitespace Between Tokens

Except at the beginning of a logical line or in string literals, the whitespace characters space, tab and formfeed can be used interchangeably to separater tokens. Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token (e.g., ab is one token, but a b is two tokens).

Indentation

Indentation is whitespaces at the beginning of the line. Indentation is important in Python. Leading whitespace, tabs and spaces, at the beginning of the logical line are used to determine the indentation level of the logical line which in turn is used to determine the grouping of statements. A group of statements that have to go together are marked by an indentation level. These statements are referred to as a block of statements.

Other Tokens

Besides NEWINE, INDENT, and DEDENT, the following categories of tokens exist: identifiers, keywords, literals, operators, and delimiters. Whitespace characters (other than line terminators) are not tokens, but serve to delimit tokens. Where ambiguity exists, a token comprises the longest possible string that forms a legal token, when read from left to right.

Source and Reference


©sideway

ID: 200501102 Last Updated: 11/5/2020 Revision: 0

IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 151

Software

Application 202

Digitization 25

Latex 10

Manim 159

Numeric 19

Programming

Web 285

Unicode 494

HTML 65

CSS 59

ASP.NET 194

OS 391

DeskTop 7

Python 37

Knowledge

Mathematics

Formulas 8

Algebra 29

Number Theory 206

Trigonometry 18

Geometry 18

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Electric 27

Biology 1

Geography 1


Copyright © 2000-2021 Sideway . All rights reserved Disclaimers last modified on 06 September 2019