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 a linefeed and in Unix, it is called LF or linefeed). See the following example.

Python Line structure

 

Comments in Python:

A comment begins with a hash character(#) which is not a part of the string literal and ends at the end of the physical line. All characters after the # character up to the end of the line are part of the 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.

Python Comment

 

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 character(\). Therefore when a physical line ends with a backslash characters(\) and not a part of a string literal or comment then it can join another physical line. See the following example.

Python lines breaking rule

 

Multiple Statements on a Single Line:

You can write two separate statements into a single line using a semicolon (;) character between two line.

Python multiple statement into a single line

 

Indentation:

Python uses whitespace (spaces and tabs) to define program blocks whereas 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 the indented same amount. In the following program, the block statements have no indentation.

Python statements without indentation

 

This is a program with single space indentation.

Python single space indentation

 

This is a program with single tab indentation.

Python single tab indentation example

 

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

Python single space and -tab indentation

 

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 help users with a 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 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  

Previous: CGI Programming
Next: Python Variable

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.