Python Syntax

A Python program is read by a parser. Python was designed to be a highly readable language. The syntax of the Python programming language is the set of rules which defines how a Python program will be written.

PYTHON LINE STRUCTURE

A Python program is divided into a number of logical lines and every logical line is terminated by the token NEWLINE. A logical line is created from one or more physical lines. 
A line contains only spaces, tabs, form feeds possibly a comment, is known as a blank line, and Python interpreter ignores it.
A physical line is a sequence of characters terminated by an end-of-line sequence.

COMMENTS IN PYTHON

A comment begins with a hash character(#) which is not a part of string literal and ends at the end of the physical line. All characters after the # character up to the end of line are part of comment and the Python interpreter ignores them. See the following example. It should be noted that Python has no multi-lines or block comments facility.

JOINING TWO LINES

When a long code is written in two lines, in python we can break the logical line into two or more physical lines by using the backslash character(\). So when a physical line ends with a backslash character(\) and not a part of the string literal or comment, it can join another physical line.

Multiple Lines can also be written in a single statement by using semi-colon(;).

INDENTATION

In Python, to define a program block whitespaces i.e. Space or Tabs are used. Whereas in other languages use braces/brackets to indicate blocks of codes for class, functions or flow control. The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all statements within the block must be indented same amount. In the following program the block statements have no indentation.

Here the interpreter is expecting an indented block for the if statement, which is a syntax error that shows up when we run the module.

The program below is with a single indentation.

 

PYTHON RESERVED WORDS

The following identifiers are used as reserved words of the language, and cannot be used as ordinary identifiers.

False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as el if or yield
assert else import pass
break except in raise

Leave A Reply

Your email address will not be published. Required fields are marked *