Syntax - Python Academy: Learning The Basics Of Python Programming (2016)

Python Academy: Learning The Basics Of Python Programming (2016)

Chapter 2. Syntax

Python has a simple and straightforward syntax. It even encourages programmers to write programs without using prepared or boilerplate codes. The print directive is actually the simplest of all directives. It prints out lines and includes newlines. You may notice that the print directive is different in the new major versions of the programming language.

Python 2.0 is the more common version while Python 3.0 supports the latest features and is more semantically correct. Anyway, the print statement is not considered as a function in version 2.0; hence, you can invoke it without including parentheses in your code. On the other hand, the print statement is considered as a function in version 3.0; hence, you have to use parentheses if you wish to invoke it.

Interactive Mode Programming

You can execute your programs in different modes. If you invoke the interpreter without passing the script file as a parameter, this is what you will get:

$ python

Python 2.4.3 ( #1, Nov 11 2010, 13:34:43 )

[GCC 4.1.2 20080704 ( Red Hat 4.1.2– 48 )] on linux2

Type“help”,“copyright”,“credits” or“license” for more information.

>>>

When you see this prompt, you can type in your desired text then press Enter. In this example, we will be using the words‘Monty Python and the Holy Grail’.

>>> print“Monty Python and the Holy Grail” ;

Take note that if you are using a newer version of the programming language, you need to use opening and closing parentheses with your print statement, such as in the following:

>> print (“Monty Python and the Holy Grail” ) ;

Regardless of which version you are using, if you run the sample code shown above, you will get the following output:

Monty Python and the Holy Grail

Script Mode Programming

If you invoke the interpreter with a script parameter, the script will start to execute and continue to run until it is done. When it is done, the interpreter will not be active anymore. Consider the following example. The sample program is written in a script and has a .py extension:

print“Monty Python’s Flying Circus”;

If you type in the above given source code in a test.py file and run it as

$ python test. Py

you will obtain the following output:

Monty Python’s Flying Circus

Another way to execute scripts is to modify the .py file, such as:

#! /usr /bin /python

print“Monty Python’s Flying Circus”;

If you run it as

$ chmod + x test.py

$ ./test.py

you get the following output:

Monty Python’s Flying Circus

Identifiers

An identifier is basically used to determine functions, variables, modules, classes, and any other objects. It begins with an underscore ( _ ) or a letter. It is then followed by digits, underscores, zero or other letters. As a programmer, feel free to use any letter or digit. You can use uppercase and lowercase letters.

However, you cannot use punctuations and special characters, such as @, $, and %, within the identifiers. In addition, Python is a case sensitive programming language. This means that you have to be careful when you use uppercase and lowercase letters in your codes. For instance, wendy,Wendy, and WENDY are all the same name and yet they are regarded as three different identifiers in Python.

Rules for Identifiers in Python

There are several rules that you have to abide by when writing programs in Python:

· The class name must always start with an uppercase character while the rest of the identifiers must start with a lowercase character.

· The identifier is private if it starts with just one leading underscore.

· The identifier is strongly private if it starts with two leading underscores.

· The identifier is a language-defined special name if it ends with two trailing underscores.

Reserved Words

Take note that there are certain words you cannot use as constants, identifier names, or variables in Python. All keywords are also written using lowercase letters. The following is a table of the reserved words in the programming language:

And

Assert

Break

Class

Continue

def

del

elif

else

except

exec

finally

for

from

global

if

import

in

is

lambda

Not

or

pass

print

raise

return

try

while

with

yield

Indentation and Lines

There are no braces for the indication of blocks of code for class definition and function in Python. Likewise, flow control is not included. If you want to denote such blocks of code, you have to make use of line indentation. You can adjust it for spaces, but make sure to indent all statements with a block, too. To help you understand this further, consider the following sample codes:

if True:

print“Correct”

else:

print“Incorrect”

if True

print“Input”

print“Correct”

else:

print“Input”

print“False

Running the first given example generates an output. Running the second one, however, results in an error. Why did this happen? Well, you have to keep in mind that in Python, blocks are formed by indenting continuous lines with the same amount of space.

Indentation is simply a way to group statements. Programmers use it in place of curly braces of blocks of code. Tabs and spaces are supported, but standard indentation requires standard codes to have similar amounts of spaces. In general, four spaces are used. Take a look at the following example:

w = 1

if w == 1 :

# This shows an indentation with exactly four spaces

print“ w is 1 .“

Indentation Myths

There are certain myths that surround indentation in Python. Here are some of them:

· A whitespace is necessary in every source code.

Actually, you do not have to use whitespaces in all your source codes. A whitespace is not necessarily significant, although an indentation is. As you have learned, this is the whitespace found at the very left part of a statement. Everywhere else, a whitespace is not that significant and may be omitted. You can use it in any way you like, even inserting arbitrary whitespaces or empty lines that do not contain anything anywhere in your program.

Moreover, the exact amount of indentation does not really matter, but the relative indentation of your nested blocks does. The indentation level is actually not recognized when you use implicit or explicit continuation lines. For instance, you may split a list across multiple lines. The indentation is just not significant at all. Take a look at the following example:

foo = [

‘ a string ‘ ,

‘ another string ‘ ,

‘ a short string ‘

]

print foo

If you run the above given code, you will get the following output:

[ ‘ a string ‘ , ‘ another string ‘ , ‘ a short string ‘ ]

Here is another example:

bar = ‘ look at this example ‘ \

‘ of a long string ‘ \

‘ that is split ‘ \

‘ across multiple lines ‘

print bar

If you run the above given code, you will obtain the following output:

look at this example of a long string that is split across multiple lines

· A certain style of indentation should be used in your programs.

Well, this one is both true and untrue. You can write the inner block on a line and not indent it. You can use any of the following versions of the “if statement” since all of them are valid and produce the same output:

if 1 + 1 == 2 :

print “ foo”

print “ bar ”

w = 99

if 1 + 1 == 2 :

print “foo” ; print “ bar “ ; w = 99

if 1 + 1 == 2 : print “ foo “ ; print “ bar “ ; w = 99

As a programmer, you may wish to write your block of code in separate lines, such as the one shown in the first example. However, there are times when there are similar if statements that you can conveniently write on each line.

In case you decide to write your block of code on separate lines, then you have to follow the rules of indentation. You have to indent the enclosed block more than the “if statement”.

In conclusion, you will be forced to abide by this rule in Python, unless you opted to make the structure of your program more complicated. The programming language does not allow program structure obfuscation with the use of fake indentations.

Keep in mind that blocks are denoted by indentation in the Python programming language; thus, the indentation is the same in every program. The consistency of the code formatting makes the program easier to read and understand.

· It is not possible to mix spaces and tabs in Python.

Yes, this one is true, even for programs written in the C language. You cannot mix spaces and tabs safely. Even though there would not really be a huge difference for your compiler, you may have a hard time dealing with codes. For instance, if you move a C source to one editor that has different tab stops, bugs will be easier to introduce.

Once again, keep in mind that it is not ideal to mix spaces and tabs for indentation. You can use spaces or tabs alone, though. In addition, you may want to avoid tabs altogether. After all, the semantics of tabs are not that well-defined and may appear differently on various types of editors and systems.

Tabs are also often wrongly converted or destroyed during copy and paste operations, as well as whenever a source code gets inserted into a Web page or any other type of markup code.

· It is possible to skip the indentation and use a keyword instead.

Yes, you can skip using an indentation and just use a keyword. There are actually a few programmers who prefer to use endif instead of an indentation to indicate the end of a block of code.

Well, it is not exactly a recognized keyword in Python. The earlier versions of the programming language come with a tool that converts code using the keyword end to correct the indentation and remove such keyword.

This may be used as a pre-processor to the compiler. In the recent versions of the programming language, however, the tool has been removed, most probably because it is not often used.

· How is the indentation parsed by the compiler?

The parsing is actually simple and well defined. In general, the changes to the level of indentation are inserted as tokens into the stream. The indentation levels are stored using a stack from the lexical analyzer or tokenizer. At first, the stack only has a value of 0, which is found at the leftmost part.

Each time a nested block starts, a new level of indentation gets pushed on the stack. The indent token is then inserted into the stream, which is eventually passed on to the parser. It is not possible to have more than a single indent token in succession.

In the event that a line is seen with a smaller level of indentation, the values start popping from the stack until one of them gets on top. This is equivalent to the new level of indentation. In case there is nothing found, a syntax error is generated. For ever value popped, there is a dedent token. It is possible to have multiple dedent tokens in succession. At the end of every source code, there are dedent tokens generated for the level of indentation that is left at the stack. This continues to occur until there is 0 left.

Multiline Statements

When you end a statement, you can either use a new line or a continuation symbol ( \ ) if you want to indicate that the line needs to continue. To help you understand this concept further, consider the following example:

total = first_item + \

second_item + \

third_item

There is no need for you to use the continuation symbol when you write statements that are contained within brackets, such as { }, ( ), and [ ]. For instance, if you wish to display the months in a year, you may simply write:

year = [‘January’ ,‘February’ ,‘March’ ,‘April’ ,‘May’ ,‘June’ ,‘July’ ,‘August’ ,‘September’ ,‘October’ ,‘November’ ,‘December’ ]

You are allowed to write multiple statements on a single line or create multiple groups as suites. When it comes to writing multiple statements, keep in mind that the inclusion of the semicolon ( ; ) is crucial. The semicolon allows you to write as many statements as possible, as long as they do not start a new block of code. Consider the following example:

import sys ; y =‘bar’ ; sys.stdout.write ( y +‘ \n’ )

So what are suites exactly? Well, they are groups of statements that consist of blocks of code. Compound or complex statements, such as if, while, def, and class require a suite and a header line.

So what are header lines? They begin statements with a keyword and end them with a colon ( : ) . Also, they are followed by one or more lines that make up a suite. Consider the following example:

if expression :

suite

elif expression :

suite

else :

suite

Quotation

As a programmer, you are allowed to use a single quote (‘ ) , double quote (“ ), and a triple quote (‘’’ or“”” ) when you denote string literals. Then again, see to it that you use the same type of quotes at the start and end of your string. Typically, triple quotes are used to span strings across multiple lines. Take a look at the following example:

paragraph =“”” You are reading an example of a paragraph that consists multiple lines and sentences. You are an excellent programmer.“””

Comments

When it comes to comments, you should use the hash symbol ( # ) to start them. However, this hash symbol should not be within a string literal. Also, the characters after it towards the end of the line should be included in the comment. In Python, comments are not recognized by the interpreter. To help you understand this concept further, take a look at the following example:

# This is the first comment

print“ Monty Python’s Flying Circus is a British sketch comedy series.” ;

# This is the second comment

If you run the example given above, you will obtain the following output:

Monty Python’s Flying Circus is a British sketch comedy series.

You can also write another comment after an expression or a statement, such as in the following:

name =“Wendy” # This is a sample comment

If you want to comment on multiple lines, you may do so as well. For example:

# This is a sample comment.

# This one is also a comment.

# This is another comment.

# This comment is written by Wendy.

Blank Lines

These lines are not recognized in the Python programming language. With this being said, they are pretty much like comments. They contain whitespaces and even comments. You have to use empty lines to terminate multiline statements in an interactive interpreter session.