Coding conventions

While there are lots of ways that the code that you type can work, you should get into a consistent habit so that your code always follows the same rules. When working with others you should agree on how you will format the code.

Some examples are:

  • Each level of indent should be 4 spaces
  • Variable and function names should be lowercase separated by underscores e.g. list_of_animals
  • Class names should be capitalised and run together e.g. CollisionDetection
  • Lines of code should be a maximum of 72 characters long

For an example of more Python coding conventions, look at https://pep8.org

You should also use comments to explain what your code is doing. Start a line with a hash # to make it a comment.

Good

# A program to work out the average of 
# two numbers and print the result
# Author:  J. Smith

# The input numbers
number1 = 13
number2 = 45

# Calculate the average
total = number1 + number2
average = total / 2

# Print the result
print(average)

Bad

a=13
b=45
print((a+b)/2)

Compare the two blocks of code. They do the same thing but it is much clearer what the good block of code is doing.

Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.