Skip main navigation

How to Start Coding in Python

This piece has been written with the following assumptions: You already know what Python is and you have installed Python and IDE and have written your first programs. Now we can start thinking about how to use all this knowledge and, more importantly, how to start coding.

This piece has been written with the following assumptions:

  1. You already know what Python is
  2. You have installed Python and IDE, and have written your first programs

Now we can start thinking about how to use all this knowledge and, more importantly, how to start coding.

Source code, comments, keywords, identifiers, and coding standards are key concepts in writing Python programs. Let’s explore each of these concepts.

Source code

Source code is the code you will write to execute a set of instructions in Python. It’s essentially the contents of a .py file. It’s similar to the text that tells you how to bake a cake in a recipe. Python has a very simple syntax and is known for being beginner friendly.

It can be tricky to manage your source code with good commenting and to manage project folders when you first begin but it is an important part of being a good software developer.

Comments in Python

Larger, more complex programs have thousands of lines of code or even more – imagine going through all that in search of one specific thing. How would you find it? Luckily, comments can be used in Python to annotate and explain code. Whenever you write code, it’s best practice to use comments to describe its purpose.

Here is an example of a comment. Try it for yourself:

# this variable is used for adding two numbers
addition = 2 + 2

This example is pretty obvious, but you get the idea. Comments are written as follows: you start with # and write your comment – the # tells the computer, ‘this is a comment, so do not try to run this’.

In the professional software development world, most programs are written in teams, simply because the projects are often too large for one person to complete alone. What’s more, software needs to be updated and maintained. Users might discover errors in the program, the goals of the program might change over time, or your organisation may want to add new features and use new technologies in the software. In these situations, the person updating the code might not be the same as the person who wrote the original version. Even if you did write the original code, you might not remember why you wrote it in that way after a few months or years.

In all of these scenarios, comments are an essential tool to document what the code is supposed to do, and why the code was written that way. So, develop the good habit of commenting code now to make your job, and other people’s jobs, easier in the future.

Read: What Makes a Good Code Comment? [1], which details good practice for creating code comments.

Python Identifiers

When you go out to buy a new mobile phone, each model and type will have a distinct name that distinguishes it from other similar devices. This is similar to an identifier in Python.

Identifiers are user-defined names given to specific objects in Python, like a variable, a module, a class and a function. Think of them as names given to Python objects that you can use to remember what they do and what they are. They can be as long as you want (although a maximum of 79 characters is best practice) and they can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or contain digits (0 to 9) or an underscore (_). Examples of valid identifiers are Set_1, myList, and share_with_others.

Bear in mind, however, that an identifier cannot start with a digit – 1Example will not be recognised as an identifier, but can start with an underscore, while _Example is just fine. Additionally, special symbols, like @, #, &, $, also can’t be used in identifier names.

Python Keywords

By contrast, a keyword is a term reserved for Python’s syntax and structure – the basic building blocks of a Python program. Keywords include: False, True, in, def, del, as, return and for. Since they are reserved, keywords cannot be used as identifiers. You can see a list of Python keywords at Python Keywords – Everything You MUST Know About Them. [2]

Coding standards

When writing code, there are certain rules and conventions you are expected to follow. This is essential if you’re working as part of a large team of developers, as this allows the team to collaborate, review and maintain code to the same standard.

Python Enhancement Proposal (PEP)

For Python, the Python Enhancement Proposal (PEP) is an evolving style guide that provides guidelines and best practices on how to write Python code. It describes new features proposed for Python and documents aspects of design and style for the community. The primary focus of PEP 8 is to improve the readability and consistency of Python code.

Take a look at the latest version of Python’s style guide, PEP 8 [3], browse the contents page, read the introduction, skim through the guide itself. Explore! And then bookmark it for easy reference and review. You don’t need to know the ins and outs of it at this stage but it’s important that you know that it is there. You may want to revisit it and consider PEP 8’s best practice for each new thing you learn to do.

Coding Conventions

Organisations may also have their own conventions, standards, and practices that they follow. If you join a new team, you should make an effort to get to know their conventions and ways of working so that you can seamlessly contribute to the team’s work. Additionally, in some industries, particularly where there is a high risk or need of failsafe systems, there are regulatory safety requirements that must be followed.

Read: Coding Standards For Quality and Compliance. This discusses how coding standards apply in various contexts and across industries. [4]

Python Coding Best Practices

To conclude, here are seven best practices to remember when writing Python code:

  1. Write one statement per line. It’s bad practice to write codes that are unnecessarily complex.
  2. Check your Python version. Python 3 and higher is best suited for a smooth coding experience.
  3. Check line length. For readability and clarity, keep the number of characters limited to 79 per line (72 characters per line for comments).
  4. Be mindful of indentations. Python relies a lot on indentations in the coding process. Four spaces, and not a tab, are the preferable way to leave an indentation. (Never mix spaces and tabs!)
  5. Effective line breaks. Again, for better readability, write code with frequent (and necessary) line breaks to structure your program in ‘paragraphs’.
  6. Practise writing good comments. If you change your code, don’t forget to update the corresponding comments too.
  7. Know the naming conventions. Although Python has flexible naming styles, Python experts recommend certain forms. For example, consult PEP 8 to learn how to use lower case, mixed case, and underscores.

References

  1. Cronin M. What Makes a Good Code Comment? [Internet]. Medium; 2019 Oct 16. Available from: https://itnext.io/what-makes-a-good-code-comment-5267debd2c24
  2. Python Keywords – Everything You MUST Know About Them [Internet]. AskPython; 2019 May 22. Available from: https://www.askpython.com/python/python-keywords
  3. PEP 8 – Style Guide for Python Code [Internet]. Python.org; [date unknown]. Available from: https://www.python.org/dev/peps/pep-0008/
  4. Coding Standards For Quality and Compliance [Internet]. Perforce; [date unknown]. Available from: https://www.perforce.com/resources/qac/coding-standards
This article is from the free online

Introduction to Programming 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