GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python Syntax

  • Home
  • ▼Introduction to Python
  • Python Home
  • Python 2 vs 3
  • Python Installation
  • Python IDLE
  • ▼Python Basic
  • Python Syntax
  • Python Print Statement
  • Python Variable
  • Python Data Type
  • Python Operators
  • Python If elif else
  • Python For Loop
  • Python While Loop
  • Python break, continue
  • ▼Data Handling and Structures
  • Python String
  • Python Lists
  • Python Dictionary
  • Python Tuples
  • Python Sets
  • Python Bytes, Bytearray
  • Python Advanced Data Structure
  • ▼Date and Time
  • Python Date and Time
  • Python Calendar Module
  • ▼Functions and Modules
  • Python User define function
  • Python Built-in Functions
  • Python Module
  • Python Functions Examples
  • ▼File Handling and I/O
  • Python File Handling
  • ▼Object-Oriented Programming
  • Python Classes
  • ▼Advanced Programming Constructs
  • Python Iterators
  • Python Generators
  • Python Functional Programming
  • Python Multithearding
  • ▼Regular Expressions
  • Python Regular Expressions
  • ▼Error Handling and Debugging
  • Python Exception Handling
  • Python Testing and Debugging
  • ▼Database Interaction
  • Python SQlite Database
  • Python SQLalchemy ORM
  • ▼Advanced Python Topics
  • Advanced Python Topics
  • Python Standard Library
  • ▼Web Programming
  • CGI Programming
  • ▼Python Miscellaneous
  • Python-docx: Create and Manipulate
  • Software Frameworks
  • Floor Division in Python
  • Thonny Python IDE
  • pip install mysql-connector-python
  • Astral Python in 2026
  • PyCharm 2026 The Ultimate Guide
  • Python for Data Analysis in 2026
  • Self-Driving Python Server
  • Python-Native Hypermedia UI
  • Building Dashboards with Pythons
  • Building an Educational HTTP Server
  • Python-First Rapid App Generator
  • Markdown-Native Personal Site Framework
  • pyRAP Framework
  • Hypermedia-Driven Python Framework
  • Shinylives Reactive Framework
  • ASGIMachine: HTTP Decision Graph Framework
  • Embedded Web Framework for MicroPython
  • ..More to come..

Python Syntax

Last update on April 13 2026 12:53:05 (UTC/GMT +8 hours)

Introduction

Python is designed to be a highly readable language with a straightforward syntax. The syntax defines the rules for writing a Python program. A Python parser reads the program and translates it into executable code.

Python Line Structure:

A Python program consists of logical lines. Every logical line is terminated by a NEWLINE token. A logical line may span one or more physical lines.

  • A blank line contains only spaces, tabs, or comments. The Python interpreter ignores blank lines.
  • A physical line is a sequence of characters terminated by an end-of-line sequence:
    • On Windows: CR LF (carriage return followed by a line feed)
    • On Unix/Linux: LF (line feed)

See the following example.

Python Line structure

 

Comments in Python:

  • A comment starts with the # symbol and continues until the end of the line.
  • Comments are ignored by the Python interpreter and are not part of the program's output.
  • Python does not have multi-line comment syntax like some other languages. If multiple lines are required for comments, each line should start with #.

Python Comment

 

Joining two lines:

To write a long statement across multiple physical lines, use the backslash (\) at the end of the first line. This allows you to break the code logically without causing syntax errors.

Example:

Python lines breaking rule

 

Multiple Statements on a Single Line:

You can write multiple statements on a single line using the semicolon (;) as a separator.

Python multiple statement into a single line

 

Indentation:

  • Python uses whitespace (spaces or tabs) to define code blocks, unlike languages like C or Java that use curly braces {}.
  • The amount of indentation is flexible, but all statements within a block must have the same level of indentation.

Incorrect example (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 (PEP 8)

  • Indentation: Use 4 spaces per indentation level. Avoid tabs.
  • Line Length: Limit lines to a maximum of 79 characters for better readability on small screens
  • Blank Lines:
    • Separate top-level functions and class definitions with two blank lines.
    • Separate methods inside a class with one blank line.
  • Inline Comments: Use inline comments sparingly, and make sure they are complete sentences.
  • Whitespace: Add spaces around operators and after commas, to improve readability.

Python Reserve words:

The following are Python's reserved words. These cannot be used as variable names or identifiers in your program:

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  

PREV : CGI Programming
NEXT : Python print statement

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.