Python Coding techniques and programming practices are one of the features of a professional programmer. While writing code to solve a problem programmer should make simple choices and have to use basic techniques. This completely depends on the programmer's skill and expertise and how wisely he make choices.
Coding techniques are basically focused on improvising the readability and maintainability of the code.
- Readability of source code has a direct impact on the developer so, while coding one should try to write code that can make easy others read and understand.
- Code maintainability focuses on making enhancements to existed software system such as adding new features, modifying existed features, fixing bugs and performance improvement easily.
Programming practices are mainly focused on the performance of the code and its enhancements.
coding techniques serve developers as a guide for developing a standard code for a software system. These are categorised into three sections.
Names are most influenceable part to understand the logical flow or concept of the code what it mean to be or what it do . ( use related words as variables )
- Camel case Notation : variable name is formed with more than a single word next words begin with a uppercase letter so that we can identify words.
- Class level varibale names should starts with uppercase letters
- attribute, method names should starts with lowercase letters
- Examples: isActive, UserRegistration(class), UserLogin, totalPrice, userDetails(attribute), calculateDiscount(method)
- Delemeter Notation : variable name is formed with multile words , these words use a delemeter to join and form variable.
- in genereal "_" is used as delimeter.
- is_active, User_Registration(class), User_Login, total_price, user_details(attribute), calculate_discount(method)
- variable names should be as small as possible and meaningful
- don't use nagative logic for varibales names (ex: is_not_logged_in )
- think 3-4 minutes about variable names.
- comments are used in coding for writing help text
- comments provide a short description of the code
- use comments when they are needed don't write comments on unnecessary things
It is very important when we are coding for larger projects because the code is altered by a team. so, programmer should format the code in such way that others can easily debug the code and can modify it easily.
- Don't write clumsy code use line breaks wherever required or use variables. It makes the code more readable.
- Use proper indentation
follow pep8 standards
Python code optimizations:
1. Looping over numbers:
2. Looping over a list
3. Looping over a list and its idices
4. Looping backwards
5. sorting in ascending order
6. sorting in descending order
7. looping over two collections
8. looping over dictionary keys and values
10. construct a dictionary from two lists
izip is faster than zip, zip computes all the list at once, izip computes the elements only when requested. One important difference is that 'zip' returns an actual list, 'izip' returns an 'izip object', which is not a list and does not support list-specific features (such as indexing).
11. using comparison operator
note: comparison operator always returns a boolean[True, False]
12. achieve switch/case in python with dict
13. convert a list of strings to numbers
14. multiplying numbers a list of numbers
Best way is
15. finding multiples of 3 from a random set of numbers
Better way is