03-Python Syntax
03-Python Syntax
Python Syntax
Introduction
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, formfeeds 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
(in windows it is called CR LF or return followed by linefeed and in Unix it is called
LF or linefeed). See the following example.
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.
http://www.w3resource.com/python/python-syntax.php 1/6
27/11/2015 Python Syntax - w3resource
When you want to write a long code in a single line you can break the logical line in
two or more physical lines using backslash characters(⧵). Therefore when a
physical line ends with a backslash characters(⧵) and not a part of string literal or
comment then it can join another physical line. See the following example.
You can write two separate statements into a single line using a semicolon (;)
character between two line.
http://www.w3resource.com/python/python-syntax.php 2/6
27/11/2015 Python Syntax - w3resource
Indentation
Python uses whitespace (spaces and tabs) to define program blocks where as
other languages like C, C++ use braces ({}) 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.
http://www.w3resource.com/python/python-syntax.php 3/6
27/11/2015 Python Syntax - w3resource
http://www.w3resource.com/python/python-syntax.php 4/6
27/11/2015 Python Syntax - w3resource
The following identifiers are used as reserved words of the language, and cannot
be used as ordinary identifiers.
as el if or yield
http://docs.python.org/3/tutorial/controlflow.html#defining-functions
http://www.w3resource.com/python/python-syntax.php 5/6