conventions

Here are a few conventions to know in Python


names


CapWords


class names in Python are written in the CapWords format, where the first letter of every word in the name is capitalized, for example

class AClassName(object):

I can use any case I want but CapWords keeps things consistent


how to use class methods and attributes


class attributes and methods can be used inside a class with self

for example

class AClass(object):

    an_attribute

    def a_method(self):
        return self.an_attribute

    def another_method(self):
        return self.a_method()
  • a_method can use the an_attribute class attribute with self.an_attribute instead of AClass.an_attribute

  • another_method can use the a_method method with self.method instead of AClass.a_method()

Note

self is Python convention, I can use any name I want


snake_case


variable and function/method names are in lowercase with underscores in between when it is more than one word, for example

  • variables

    variable_name = None
    
  • functions

    def function_name(*positional_arguments, **keyword_arguments):
    

for more details see the Python Style Guide


comments

Comments are made with a hashtag/pound before the thing that is commented for example

# This is a comment

comments do not do anything, they are notes. Python ignores them


enclosures

Enclosures must be closed once open, which means they happen in pairs for example

  • quotes

    ""      <- double quotes
    """"""  <- triple double quotes
    ''      <- single quotes
    ''''''  <- triple single quotes
    
  • parentheses

    ()
    
  • square brackets/braces

    []
    
  • curly brackets/braces

    {}
    

The Integrated Development Environment (IDE) takes care of this, it automatically closes them when you open one


quotes

Quotes are for strings and can be

  • single quotes

    'single quotes'
    
  • double quotes

    "double quotes"
    
  • triple single quotes to write one string on many lines

    '''text on different lines
    with
    triple double quotes
    '''
    
  • triple double quotes to write one string on many lines

    """text on different lines
    with
    triple double quotes
    """
    

tuples

A tuple is a sequence or container of objects that cannot be changed later, this means it is immutable. They are represented with parentheses/brackets (()), and the things in them are separated by commas, for example

()
(0,)
(1, 2.5, "three", [4, 'five'])

lists

A list/array is a sequence or container of objects that can be changed after it is defined, it is mutable. They are represented with square brackets ([]) and the things in them are separated by commas, for example

[]
[0]
[1, 2.5, 'three', (4, "five")]

sets

A set is a container of objects that have no duplicates, and are represented with curly braces/brackets ({}), for example

{1, 2.5, 'three', (4, 'five')}

dictionaries/mappings

Dictionaries/Mappings are also represented with curly braces/brackets ({}) but have key-value pairs, for example

{}
{
    'integer': 1,
    'floating_point': 2.5,
    'string': 'three',
    'tuple': (1, 2.5, "three", [4, 'five'])
    'list': [1, 2.5, 'three', (4, "five")]
    'set': {1, 2.5, 'three', (4, 'five')}
    'dictionary': {
        'integer': 1,
        'floating_point': 2.5,
        'string': 'three',
        'tuple': (1, 2.5, "three", [4, 'five'])
        'list': [1, 2.5, 'three', (4, "five")]
        'set': {1, 2.5, 'three', (4, 'five')},
        'dictionary': ...
    }
}

rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too