03 - Python Lexical Conventions Syntax

An expression in a programming language is a combination of some specific values, constants, operators, variables and functions that are interpreted by an interpreter for a particular programming language, which computes and produces/returns an output value.
Python programs are treated as the sequence of expressions and as like that, they are interpreted and executed by Python Interpreter. All expressions are executed sequentially one at the time (line by line in simple terms), until program comes to the end. All expressions are equal.

3.1 Identifiers

Identifiers are names used for identifying functions, classes, modules or variables. They can start with an underscore or with letter (uppercase or lowercase). Can be followed by numbers, letters or underscores, but can’t be followed by special characters (@, $, %). You can’t use reserved words (listed later) as an identifier. Identifiers that start with underscore usually have special meaning:

  • one single leading underscore (_name) - identifier is meant to be private
  • two leading underscores (__name) - identifier is private and you can’t call it from outside of the class (more about classes later)
  • two leading and two ending underscores (__name__) - identifier is special name python-defined. You will see later when we will redefine __str__method of some class to print custom defined string and variables of some object (most of this will be clear later)

Here is the list of reserved words you can’t use as identifiers:

and, as, assert, break, class, continue,def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, print, raise, return, try, while, with, yield.

Python is a case-sensitive language. X and x are two different identifiers.

Examples of correct identifiers: identifier, _identifier, Identifier, IdenTifier, _Identifier, identifier__, identifier_identifierIdentifier, Identifier5430…

Examples of incorrect identifiers: any reserved word, identifier@, $identifier, identif%er…

3.2 Indentation

In designing Python, the main idea was that programmers spent more time to read than to write a program, so the Python is designed in such a way to be readable. The main difference between Python and other programming languages is that Python does not use any brackets to indicate blocks of code. Instead of brackets indentation is used. By PEP8 standard 4 spaces (not tab) are recommended for the indentation. Indentation must be consistent in one block of code

if condition:                            
            stetement1
            statement2
            statementN
else:
            stetement1
            statement2
            statementN

 

if condition:
            stetement1
            statement2
            statementN
else:
            stetement1
       statement2
            statementN

The second example will produce syntax error because of bad indentation in else block of code. The conclusion is that all continuous lines with the same indentation would form a block of code. If one line of code in one block doesn’t have the same indentation as other lines, that Interpreter will report a Syntax error because of indentation and program will not run. All lines in one block must have the same indentation.

3.3 Line Structure

Every expression ends in a new line.

a = b - a

b = b - a

a = b + a

Multiple expressions are possible in one line by using semicolon character (;), but that is not by the PEP8 standard. The interpreter will not report an error, but when you became an expert programmer you will see that form like that disrupts readability of the program.

a = b - a; b = b -a; a = b + a

Long expressions can be divided into more lines by using \ character, except inside brackets - (), [], {}. In brackets, you don’t need a special character like \ to divide expression into multiple lines.

a = math.cos(b) + \
                 math.cos(c)

a = {

                   "a": 1,

                   "b": 2,

                    "c": 3

}

a = function_name(argument1,
                                            argument2,
                                            argument3)

Blank lines are skipped during the program executing. Lines with whitespaces or only comments are considered as blank lines and they will also be skipped in executing. The line that contains only whitespaces is not allowed by the PEP8 standard, it has to be empty but without whitespaces. Of course, this is not an error and for the beginning, you don’t have to take care of this.

In the interpreted mode when you enter in the multiline statement, you have to enter one blank line to exit multiline mode, i.e. to terminate a multiline statement. Just press enter one more time and that’s it, in simple terms.

3.4 Comments

Comments in Python starts with the hash sign (#) and every character in that line after the hash sign is considered as the comment and will not be executed by Interpreter.

# Comment, will not be executed
           print "Hello World"
           -> Hello World

Comments can be located after the code in the same line and they also won’t be executed.

print "Hello World" # Comment, will not be executed -> Hello World

Multiple lines can also be commented and they won’t be executed as well

# This is a comment
# This is also a comment
# and this

None of the lines above will be executed.

3.5 Quotes

In Python, there are three types of quotes: single quotes ('), double quotes (") and triple quotes (''' or """). They are used to denote string literals, but string literal must begin and end with the same type of quote. This is done in purpose that if you need to print one type of quotes, you don’t have to write some special character instead of that type of quote, but only to use another type of quotes to denote string literal.

example_word = ’example’
example_sentence = "This is an example sentence"
example_paragraph = ’’’This is an example paragraph and
                                             is made up of multiple lines’’’
print "This is an ’example’ how to print quote" -> This is an ’example’ how to print quote

Single quotes are equal to double quotes and can be used equally in the code. They are also with the same priority, so you just have to choose your favorite type and to start using it.

Like us on Facebook