We can analyze the rules that are to be followed while coding in python, through a step by step interpretation we can start from top that is from imports.
Imports should be seperated by lines
For eg: import os
import sys
They should be put at the top of the file over all global module and constants.
They also should be grouped in following order.
** Standard library imports.
** Standard related library imports.
** Local application/specific imports.
White spaces should be not more than one space around an assignment (or other) operator to align it with another.
such as
x = 2 not x = 2
Always surround the binary operators with single white spaces such as for assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
Use 4 spaces per indentation level.
For really old code that you don't want to mess up, you can continue to use 8-space tabs.
Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.
Comments should be complete sentences. If a comment is a phrase or sentence, its first word should be capitalized, unless it is an identifier that begins with a lower case letter.
Using TRUE in assignment and conditions is not encouraged.
Imports should be seperated by lines
For eg: import os
import sys
They should be put at the top of the file over all global module and constants.
They also should be grouped in following order.
** Standard library imports.
** Standard related library imports.
** Local application/specific imports.
White spaces should be not more than one space around an assignment (or other) operator to align it with another.
such as
x = 2 not x = 2
Always surround the binary operators with single white spaces such as for assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
If operators with different priorities are used, consider adding
whitespace around the operators with the lowest priority. However, never use more than one space, and
always have the same amount of whitespace on both sides of a binary
operator.
For really old code that you don't want to mess up, you can continue to use 8-space tabs.
Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.
foo = long_function_name(var_one, var_two, var_three,
var_four)hen invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors.
Comments should be complete sentences. If a comment is a phrase or sentence, its first word should be capitalized, unless it is an identifier that begins with a lower case letter.
Block Comments generally apply to some (or all) code that follows
them, and are indented to the same level as that code. Each line of a
block comment starts with a # and a single space.
Inline Comments is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the
statement. They should start with a # and a single space.
Naming Conventions of Python's library are a bit of a mess, so
we'll never get this completely consistent -- nevertheless, here are
the currently recommended naming standards. New modules and packages
(including third party frameworks) should be written to these
standards.
joined_lower for functions, methods, attributes
joined_lower or ALL_CAPS for constants
StudlyCaps for classes
camelCase only to conform to pre-existing conventions
Attributes: interface, _internal, __private
Try not to use loops as much as possible instead you can use inbuilt functions or methods such as join() so to join all the list elements as an example.
Using TRUE in assignment and conditions is not encouraged.
for example you can use
if x:
print 'yes'
not
if x=TRUE:
print 'yes'
You can use a split in built method to extract words from paragraphs instead of using for or while loops nested. This decreases program readability.
>>>a='This is prasanth's program'.split()
>>>a
['This','is','prasanth's','program']
""""simple""""
All the conventions has to be followed thoroughly so as to improve readability to make yourself better while reading your own program.............
.................THANK YOU...................