Skip main navigation

Semantics and style guides for Python

Learn some basic vocabulary in the Python programming language before you proceed to writing code.

There are different types of programming languages: general-purpose, interpreted, interactive, and object-oriented. Python is an interpreted language, which means that the instructions are not directly executed by the target machine but are instead read and executed by another program, known as an interpreter.

Let’s learn some basic vocabulary in the Python programming language before we proceed to writing code.

Key terms in Python

Let’s learn some key terms that we would need to know before beginning to code with Python.

Term Definition
Class A class is a prototype or sketch or blueprint for a collection of data or functions (object).
Object An object is simply a collection of data and functions that act on those data.
Function A function is a group of statements that perform a certain task. As programs grow longer and more complex, functions make it manageable and organised.
Method A method is a function that belongs to an object.

For example, a class could be a sketch or a ‘blueprint’ for a cat. It specifies all the details about the cat such as age, colour, and breed, but it does not contain the age, colour, or breed of a specific cat.

Semantics in Python

Just as any language has a set of grammatical rules to define how to put together a sentence that makes sense, programming languages have similar rules, called syntax.

Python language’s design is distinguished by its emphasis on its:

  • readability
  • simplicity
  • explicitness.

Understanding the language’s semantics is the first step towards getting a grip on coding in Python, and recognising how these features make it an ideal programming language. Now, open your Jupyter Notebook and follow along with each code snippet to see how it works for yourself.

Use indentation, not braces

Python uses whitespaces, tabs, or spaces to structure its code instead of braces (or curly brackets) like in many other programming languages, such as C++. This method of structuring in Python results in similar-looking formatted code, making it easier to comprehend when reading a snippet of code written by another programmer.

The following code snippet shows a part of the quicksort algorithm in Python, Java, or C++. Comparing the two is proof enough to realise how a piece of code can look different in different languages:

Written in Python:

for x in array:
if x < pivot:
less.append(x)
else:
greater.append(x)

Written in Java or C++:

for x in array
{
if x < pivot
{
less.append(x)
}
else
{
greater.append(x)
}
}

Reflect

  • What do you think are the critical differences between these syntaxes?
  • Which would you choose to program with as a data analyst?

Share your responses in the comments section.

Multiline statements in Python

Statements in Python typically end with a new line, but there is a way to split a Python statement into multiple lines by using a line continuation character ().
For example:

Sum = num1 + 
num2+
num3
}

Comments in Python

We use comments in the code to describe a code snippet. In programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code and logic behind solving a problem easier for humans to understand, especially when dealing with long lists of inherited code.
In Python, a hash (#) sign is used to mark a line of code as comments.
For example:

#First comment
print ("Hello Python") #This is second comment

You can comment on multiple lines:

#First comment
#Second Line of Comment
#This is also a comment

Or you can use the triple-quoted string (‘’’) before and after a multi-line comment:

'''
This is a multiline
comment.
'''

Identifiers in Python

A Python identifier is a name used to identify a variable, function, class, module, or another object. For example, quantity, _1234, num4 are all identifiers.
There are certain rules and conventions that are followed in defining identifiers in Python.

Rules for identifiers in Python

There are two rules for naming identifiers in Python. These are:

  1. The name can be a letter A to Z, or a to z, or an underscore (_).
  2. The name can be followed by zero or more letters, underscores, and digits (0 to 9).

Conventions for identifiers in Python

Some of the widely accepted naming conventions are as follows:

  • Class names start with an uppercase letter. All other identifiers, such as the name of variables and functions, start with a lowercase letter.

For example:
~~~ python
p1 = MyClass()
~~~

where p1 is a variable, and MyClass is the class name.

print(var_a)

where print is a function.

  • Starting an identifier with an underscore (_) indicates that the identifier is private and should be treated as a non-public part of the code. Private identifiers are useful for breaking tasks up into smaller parts, or for preventing duplication of code, which is needed often by other methods in a class but should not be called outside of that class.

For example:

_var

where var is a variable.

  • Starting an identifier with two leading underscores ()__ indicates a strongly private identifier. Two leading underscores ()__ causes the Python interpreter to rewrite the attribute name to avoid naming conflicts in subclasses. This is also called name mangling, which means that the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.

For example:

__var

where var is a variable.

  • A single trailing underscore, where an identifier is followed by an underscore, is used by convention to avoid naming conflicts with Python keywords.

For example:

var_

where var is a variable.

  • If the identifier starts and ends with two leading and trailing underscores, the identifier is a Python-defined special name. These identifiers affect an object’s high-level behaviour and its interactions with operators, and learning to leverage special terms will enable you to design elegant and powerful classes of objects.

For example:

__var__

where var is a variable.

For detailed explanations of further guidelines in Python, refer to the resource below.

Access: Python style Guide [1]

References

  1. Python style guide [Internet]. Python; [date unknown]. Available from: https://www.python.org/dev/peps/pep-0008/
This article is from the free online

Introduction to Data Analytics with Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education