04 - ENG1013 Coding Standards - 1.0
04 - ENG1013 Coding Standards - 1.0
Python
ENG1013 Semester 1, 2022
Version 1.0
Python Coding Standards ENG1013
Purpose 3
Variables 4
Name of the variable 4
Best practices 4
Bad example 4
Style of the variable 4
Best practices 4
Bad examples 4
Functions 5
Name of the function 5
Best practices 5
Bad example 5
Style of the function 5
Best practices 5
Bad examples 5
Magic numbers 6
Best practices 6
Bad example 6
Indentation 7
Criteria 7
Example 7
Documentation 8
Inline comments 8
Example 8
Multiple lines of comments 8
Example 8
File headers 8
Example 8
Function headers 9
Example 9
Semester 1, 2021 2
Python Coding Standards ENG1013
Purpose
This document serves as a python coding standard document for all documentation and
coding done for ENG1013: Engineering Smart Systems at Monash University.
You should follow the advice in this document for Assignment work and Assessed content
(i.e. tests, exams)
Semester 1, 2021 3
Python Coding Standards ENG1013
Variables
Name of the variable
Variables should be named in a way that the reader can understand what the variable is
representing
Best practices
buildingHeight = 100
Bad example
With the sole exception of when x is represented in a formula, it is best to avoid ambiguous
variable meanings.
x = 100
Best practices
numberOfInputs
Bad examples
NumberOfInputs
Number_of_inputs
number_of_inputs
Semester 1, 2021 4
Python Coding Standards ENG1013
Functions
Name of the function
When naming the functions using a descriptive name of what the function should do is
considered a best practice.
Best practices
def find_max_number(numb1,numb2):
Bad example
def fx(numb1,numb2):
Best practices
def find_max_number(numb1,numb2):
Bad examples
def findmaxnumber(numb1,numb2):
def Find_Max_Number(numb1,numb2):
def find_Max_Number(numb1,numb2):
Semester 1, 2021 5
Python Coding Standards ENG1013
Magic numbers
The magic number is a numeric literal that is used in the code without any explanation of its
meaning
Best practices
pi = 3.14
radius = float(input("Enter the radius of a circle:"))
area = pi * radius * radius
print("Area of a circle = %.2f" %area)
Bad example
radius = float(input("Enter the radius of a circle:"))
area = 3.14 * radius * radius
print("Area of a circle = %.2f" %area)
Semester 1, 2021 6
Python Coding Standards ENG1013
Indentation
Indentation is the way of telling the python interpreter that the block of codes belongs
together. Grouping of statements for a specific purpose is considered a block.Python uses
four spaces as indentation as the default
Criteria
● One tab is equal to four spaces
● Blocks of code should be clear and human-readable
● All content within a block should be indented to the right, including the function
bodies, loop bodies, and blocks in if-else if statements
Example
def some_function(x,y):
if x>100:
if y>100:
doThing(x,y)
elif y<100:
doThing(x,100-y)
elif x<100:
if y>100:
doThing(x+50,y)
elif y<100:
doThing(x+50,100+y)
Semester 1, 2021 7
Python Coding Standards ENG1013
Documentation
When you write a code you write the code for two primary audiences, your users and your
developers. For them to understand the code you need to have the proper documentation. In
general, commenting describes the code to the users and the developers.
Inline comments
Comments can be created using the pound (#) sign and in general, comments should be no
more than a few sentences.
Example
def hello_world():
# A simple comment preceding a simple print statement
print("Hello World")
Example
# comment line 1
# comment line 2
# comment line 3
File headers
The file header is a block of comments on the top of the code. This should include the file
name, author, date, and some details about the file and its content
Example
# Details about the module and for what purpose it was built for
# Created By : name_of_the_creator
# Created Date: date/month/time ..etc
# version ='1.0'
Semester 1, 2021 8
Python Coding Standards ENG1013
Function headers
Each function you create in your code should have a function header where it states what
type of data it expects to receive and the type of data it will return to the calling function or
the program. Furthermore, small description of what the function does
Example
# Prints "Hello world" to the console
# no input parameters and no return value
def hello_world():
print("Hello World")
Semester 1, 2021 9