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.

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.

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.

Multiple Statements on a Single Line:
You can write two separate statements into a single line using a semicolon (;) character between two 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.

This is a program with single space indentation.

This is a program with single tab indentation.

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

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
Python: Tips of the Day
Returns the symmetric difference between two lists, after applying the provided function to each list element of both:
Example:
def tips_symmetric_difference_by(p, q, fn): _p, _q = set(map(fn, p)), set(map(fn, q)) return [item for item in p if fn(item) not in _q] + [item for item in q if fn(item) not in _p] from math import floor print(tips_symmetric_difference_by([4.2, 2.4], [4.6, 6.8],floor))
Output:
[2.4, 6.8]
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook