Python Data Presistence – Statements

Python Data Presistence – Statements

Python interpreter treats any text (either in interactive mode or in a script) that ends with the Entering key (translated as ‘\n’ and called newline character) as a statement. If it is valid as per syntax rules of Python, it will be executed otherwise a relevant error message is displayed.

Example

>>> “Hello World”
‘Hello World’
>>> Hello world
File “<stdin>”, line 1
Hello world

SyntaxError: invalid syntax

In the first case, the sequence of characters enclosed within quotes is a valid Python string object. However, the second statement is invalid because it doesn’t qualify as a representation of any Python object or identifier or statement, hence the message as SyntaxError is displayed.

Normally one physical line corresponds to one statement. Each statement starts at the first character position in the line, although it may leave certain leading whitespace in some cases (See Indents – next topic). Occasionally you may want to show a long statement spanning multiple lines. The ‘V character works as a continuation symbol in such a case.

Example

>>> zen=”Beautiful is better than ugly. \
… Explicit is better than implicit. \
… Simple is better than complex.”
>>> zen
‘Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex.’

This is applicable for script also. In order to write an expression you would like to use two separate lines for numerator and denominator instead of a single line as shown below:

Example

>>> a=10
>>> b=5
>>> ratio=( pow ( a , 2 ) + ( pow ( b , 2 ) ) ) /  \
. . . ( pow ( a , 2 ) – ( pow ( b , 2 ) ) )
>>>

Here pow ( ) is a built-in function that computes the square of a number. You will learn more built-in functions later in this book.
The use of a backslash symbol (\) is not necessary if items in a list, tuple, or dictionary object spill over multiple lines. (Plenty of new terms, isn’t it? Never mind. Have patience!)

Example

>>> marks=[34,65,92,55,71,
. . . 21,82,39,60,41]
>>> marks
[34, 65, 92, 55, 71, 21, 82, 39, 60, 41]