What is a statement in Python
The statement is the most basic form of an executable element within a Python program. A variable on its own does nothing-it must be part of a statement in order to be created, modified, and manipulated. There are a number of generic statement types that you’ll learn about in this section, including basic statements, assignments, function calls, and control and loop statements.
Python uses a very simple method for parsing the individual statements that make up a typical program Within Python, each line is identified as a single statement: the normal line termination of a carriage-return or linefeed acts as the statement terminator. For example, the following two-line program is valid:
print "Hello World!" print "I am a test program"
For extra long lines, such as the following line, you have a number of options, depending on the line contents:-
print "Hello, I am a test program and I am printing an extra long line so I can demonstrate how to split me up"
For most lines, the easier method is to append a backslash to the end of the line where you want to split it, as in the following example:
print "Hello, I am a test program and I am printing an extra\ long line so I can demonstrate how to split me up"
When the Python interpreter sees the backslash as the last character in a line it automatically appends the next line before the entire line is parsed. This process is cyclic, so a line can span as many lines as you like, providing that each line ends in a backslash:
print "Hello, I am a test program\ and I am printing an extra\ long line so I can demonstrate \ how to split me up"
For statements that incorporate a pair of matching parentheses, you do not have to use the backslash technique. Python automatically searches the next line looking for the terminating parenthesis. You can modify your print statement in the preceding example to print a tuple of the message, which requires parentheses and therefore implies a termination character:
print ("Hello, I am a test program", "and I am printing an extra", "long line so I can demonstrate", "how to split me up")
Finally, there are situations where without either the backslash or parentheses, Python automatically expects further information and therefore examines the next line. For C and Perl programmers, Python also accepts the semicolon as a line terminator if it makes you feel more comfortable. However, the use of a semicolon is completely optional, and using one has no effect on how Python parses lines-you must continue to use one of the tricks described earlier whether you use semicolons or not.
Python allows you to incorporate comments by inserting the hash symbol (#) into code as in the following example:
version 1.0 # This is the current version number
Everything after the hash sign is taken to be a comment and is ignored by the Python interpreter. Note that this does affect hash signs embedded within quotes, as you might expect.