Member-only story

How To Write Better Python Code

Manpreet Singh
3 min readJul 23, 2022

--

Welcome back! Python is one of the most important programming languages out there, if you’re new to it, check out the link below to learn more about it:

So, let’s take a look at some ways to write better Python code!

Comment Comment Comment

Another important way to keep your code clean is by commenting out your code, most of the time, my project consists of more comments than code (which is probably why i’m a terrible programmer 😂). Comments help keep your code clean by explaining what those blocks of code do, but it can also double as separating different portions of your projects.

quantityofitems = 12
name = 'tom'
totalcost = 1234
print("total of " + name + "order is" + totalcost)

When we comment, it can help explain what we’re doing with our project:

#GETTING THE QUANTITY OF ITEMS
quantityofitems = 12
#NAME OF CUSTOMER
name = 'tom'
#TOTAL COST OF ORDER
totalcost = 1234
#PRINTING TOTAL ORDER
print("total of " + name + "order is" + totalcost + "with" + quantityofitems + "of items")

Clean Variable/Function Names

A very common issue that I do all of the time is the naming of variables/functions, basically, you want your names to be self explanatory, if you’re variables/functions look like this:

var1 = 12
var2 = 'tom'
var3 = 1234

You can easily run into issues when referencing those values, instead, try something like this:

quantityofitems = 12
name = 'tom'
totalcost = 1234

This makes it much easier to remember/reference those variables/functions.

Separating Blocks Of Code

Another awesome way to clean out your Python code is by separating your blocks of code, although the project functions the same, it helps make your code look so…

--

--

Manpreet Singh
Manpreet Singh

No responses yet

Write a response