0% found this document useful (0 votes)
13 views

03-Python Syntax

The document discusses Python syntax including topics like line structure, comments, joining lines, indentation, coding style and reserved words. It provides examples and explanations of basic Python syntax constructs.

Uploaded by

John
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

03-Python Syntax

The document discusses Python syntax including topics like line structure, comments, joining lines, indentation, coding style and reserved words. It provides examples and explanations of basic Python syntax constructs.

Uploaded by

John
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

27/11/2015 Python Syntax - w3resource

Python Syntax

Introduction

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, 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

Joining two lines

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.

Multiple Statements on a Single Line

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.

This is an program with single space indentation.

http://www.w3resource.com/python/python-syntax.php 3/6
27/11/2015 Python Syntax - w3resource

This is an program with single tab indentation.

Here is an another program with an indentation of a single space + a single tab.

http://www.w3resource.com/python/python-syntax.php 4/6
27/11/2015 Python Syntax - w3resource

Python Coding Style


Use 4 spaces per indentation and no tabs.
Do not mix tabs and spaces. Tabs create confusion and it is recommended to use only spaces.
Maximum line length : 79 characters which helps users with small display.
Use blank lines to separate top-level function and class definitions and single blank line to separate
methods definitions inside a class and larger blocks of code inside functions.
When possible, put inline comments (should be a complete sentences).
Use spaces around expressions and statements.

Python Reserve 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  

http://docs.python.org/3/tutorial/controlflow.html#defining-functions

 
http://www.w3resource.com/python/python-syntax.php 5/6

You might also like