DEV Community

Cover image for Best Programming Practices in Python.
EzeanaMichael
EzeanaMichael

Posted on

Best Programming Practices in Python.

Here are some things to keep in mind while writing python codes.

  1. Comment on your code.
  2. Create and use functions to make your program smarter and more flexible.
  3. Use of proper identifiers.
  4. Space your code so it would be easier to read.
  5. Properly Indent your condition, loop, and when defining functions and classes.
  6. Code regularly.

1. Comment on your code.
This might seem unnecessary at first but commenting on your code is a good step in programming, anyone else that reads your code will understand what it does easier and so would you, I am personally guilty of not regularly commenting on code but I'm working on it to develop myself, so should you, I saw changes when I do so and separate the code to parts to understand what easy line or statement does and why it does that, commenting is thus a good programming practice. This can be done by using "#".

2. Create and use functions to make your program smarter and more flexible.
Python is regarded as a multi-paradigm programming language since it can be used for structural, functional, object-oriented, and so on. It is therefore a good practice to use functions as regularly as possible, to minimize repetition of codes written beforehand and make the code more understandable.

3 . Use of proper identifiers
Use names and not letters when programming so it can be easily read and understood, avoid the use of letters like "I"," a", "j", even when creating loops its unprofessional, instead use words that relate to the program so it will be understood by you and anyone else who reads your code, use your English wisely and your code will be more mature and professional.

4 . Space your code so it would be easier to read.
The interface in vs code, pydroid, pycharm, or any other python programming interface is closely limitless, so why write code on every single line that makes it quite difficult to know which path does what? A single space in between parts of the code goes a long way and looks more pleasing to the eye as much as you can leave space don't leave too much, that is, about 4 to 5 lines in a row, space smartly and see how your code looks.

5. Properly Indent your condition, loop, and when defining functions and classes.
Make sure you indent your conditional statements as well as your loops also when defining functions and classes to avoid generating an error and to know when the statement begins and when it ends to avoid a statement being under a such when it wasn't intended.

6. Code regularly.
The need for consistency cannot be overemphasized, code regularly, follow steps on programming, try challenges and problems, and soon writing code will be easier.

Thanks for reading, comment your thoughts and more practices you feel will make programming easier and more professional.

Top comments (3)

Collapse
 
jhelberg profile image
Joost Helberg

Nice article and applies, of course, to many other programming languages. But foremost, and that is missing from this article, document the problem you're trying to solve before writing down the first letter of code. This documentation includes business rules, goal and approach. Then, make the code part of this documentation. It's called literate programming and should be used a lot more.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Good list these can apply to all programming languages.

Collapse
 
techknowmug profile image
Techknowmug

learn from artoftesting