4 Tips and best practices for writing clean and efficient Python code

4 Tips and best practices for writing clean and efficient Python code

Increase the quality and readability of your code and everyone would think you're a pro

ยท

3 min read

Commenting

A good programmer always explains his code through comments (except the code is self-explanatory). Comments are very important for numerous reasons:

  • They guide you as you write your code, like a minimal pseudocode

  • They help you remember why you wrote a line of code

  • It makes your code more readable to other people including non-programmers

  • Programmers feel more confident about their skills when they can read someone else's code

Well Defined Variable Names

When your variable names are self-explanatory, someone does not have to guess what kind of value the variable is holding. Don't be scared to use a lengthy name for your variable, python wouldn't complain. Remember the second line of the zen of python:

explicit is better than implicit

So a name like concatenate_strings is much preferable to strcat (no offense C devs).

Don't Reinvent The Wheel

Most times as developers, we are tempted to try and recreate something that someone else has done already. This can be a good way to build and reassure ourselves about our skills, but unless it's necessary, do not reinvent the wheel.

The standard library already provides us with so many tools and data we need to build with python. I recommend you check out the cool tools that come prebuilt into python here.

Python also has a large community with developers continuously deploying tools and utilities that would make writing programs easy and faster. Most of those tools are available on the Python Packaging Index(PyPI) and can be installed via the pip command.

Use Python One-liners

You can think of one-liner code as a block of code compressed together so that it fits inside one line. It is a concise, useful program packed in just one single line.

Python provides a few awesome one-liners that when used correctly, make your code look like it was written by a pro!

lambda functions

A lambda function is a small anonymous function, which means it doesn't have a name. A lambda function can take any number of arguments, but can only have one expression. As we already know that the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python.

Some quick examples of lambda functions are:

  • a lambda function that returns the reverse uppercase version of a string.

    >>> reverse_uppercase = lambda string: string.upper()[::-1]
    >>> text = "mirror"
    >>> reverse_uppercase(text)
    'RORRIM'
    
  • a lambda function that checks if a number is a multiple of another number

    >>> is_multiple = lambda number, factor: number % factor == 0
    >>> is_multiple(10, 2)
    True
    >>> is_multiple(20, 3)
    False
    
  • a lambda function that finds the greater of two numbers

    >>> greater_number = lambda num1, num2: num1 if num1 > num2 else num2
    >>> greater_number(74, 38)
    74
    >>> greater_number(38, 74)
    74
    

Sequence Unpacking

In Python, a sequence is a term for an ordered collection of items. Strings, Lists and Tuples are important sequences that are available in python. A sequence can be unpacked into variables using a technique called "sequence unpacking".

  • unpacking a list

    >>> students = ["Alex", "Tosin", "Chimobi"]
    >>> student1, student2, student3 = students
    >>> student1
    'Alex'
    >>> student3
    'Chimobi'
    
  • unpacking a tuple

    >>> my_info = ("John Doe", 25, "Male", 5.9)
    >>> name, age, gender, height = my_info
    >>> name
    'John Doe'
    >>> age
    25
    >>> height
    5.9
    
  • unpacking a string

    >>> grades = "DABBC"
    >>> biology, maths, english, physics, geography = grades
    >>> biology
    'D'
    >>> geography
    'C'
    

Last Words

Thank you for reading and I hope you found this article useful!

If there are tips you think I should have included or other suggestions then please do comment. ๐Ÿ’ฌ

Happy coding! ๐Ÿ’ปโ˜•๏ธ

ย