• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Cheatsheet / Python Style Rules

Python Style Rules

Author: PFB Staff Writer
Last Updated: December 2, 2020

As I wrote in Python Style Guide Part 1, Google has put together a really nice style guide summary. This style guide is a list of dos and don’ts for Python programs.

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.

PEP8 has emerged as the style guide that most projects. it promotes a very readable and eye-pleasing coding style. PEP8 has emerged as the style guide that most projects. it promotes a very readable and eye-pleasing coding style. It’s a well-established convention in the Python community, and in general we should follow these.

The page can be found here: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

Each style point has a summary for which additional information.

Python Style Rules

Semicolons

Do not terminate your lines with semi-colons and do not use semi-colons to put
two commands on the same line.

Line length

Maximum line length is 80 characters.

Parentheses

Use parentheses sparingly.

Indentation

Indent your code blocks with 4 spaces.

Blank Lines

Two blank lines between top-level definitions, one blank line between method
definitions.

Whitespace

Follow standard typographic rules for the use of spaces around punctuation.

Shebang Line

Most .py files do not need to start with a #! line.

Start the main file of a program with #!/usr/bin/python.

Comments

Be sure to use the right style for module, function, method and in-line comments.

Classes

If a class inherits from no other base classes, explicitly inherit from object.

This also applies to nested classes.

Strings

Use the % operator for formatting strings, even when the parameters are all
strings.

Use your best judgement to decide between + and % though.

Files and Sockets

Explicitly close files and sockets when done with them.

TODO Comments

Use TODO comments for code that is temporary, a short-term solution,
or good-enough but not perfect.

Imports formatting

Imports should be on separate lines.

Statements

Generally only one statement per line.

Access Control

If an accessor function would be trivial you should use public variables
instead of accessor functions to avoid the extra cost of function calls in Python.

When more functionality is added you can use property to keep the syntax
consistent.

Naming

module_name, package_name, ClassName, method_name, ExceptionName,
function_name, GLOBAL_CONSTANT_NAME, global_var_name,
instance_var_name, function_parameter_name, local_var_name.

Main

Even a file meant to be used as a script should be importable and a mere import
should not have the side effect of executing the script’s main functionality.

The main functionality should be in a main() function.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Cheatsheet Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us