PEP 8: A Guide to Python Coding Style

Python Methods and Functions

For Python, PEP 8 has become the style guide followed by most projects; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; Here are the most important points learned for you:

1. Use 4 spaces without tabs.

 # Aligned with opening delimiter. grow = function_name (variable_one, variable_two, variable_three, variable_four) 
 # First line contains no argument. Second line onwards # more indentation included to distinguish this from # the rest. def function_name (variable_one, variable_two, variable_three, variable_four): print (variable_one) 

The 4-space rule is not always required and can be overridden for the continuation line.

2. Use doc strings: in Python you can use both single and multiline doc strings. However, a single line comment is placed on one line, in both cases triple quotes are used. They are used to define a specific program or define a specific function. 

 def exam (): "" "This is a single line docstring" "" "" "This is a multiline comment" "" 

3. Wrap lines so that they do not exceed 79 characters: The Python standard library is conservative and requires a line limit of 79 characters. Strings can be enclosed in parentheses, parentheses, and parentheses. They should be used in preference to backslashes. 

 with open ('/ path / from / where / you / want / to / read / file') as file_one, open (' / path / where / you / want / the / file / to / be / written', 'w') as file_two: file_two.write ( ()) 

4. The use of regular and updated comments is valuable to both programmers and users . There are also different types and conditions that, if observed, can be of great help in terms of programs and users. Comments should form complete sentences. If the comment is a complete sentence, its first word must start with an uppercase letter, unless it is an identifier that starts with a lowercase letter. In short comments, the period at the end can be omitted. Block comments have more than one paragraph and each sentence must end with a period. Block comments and inline comments can be written followed by a single "#". 
An example of inline comments:

 geek = geek + 1 # Increment 

5. Using commas: this is optional, except for creating a tuple. 

 tup = ("geek",) 

5. Use standard encodings Python UTF-8 or ASCII, and not any fancy encoding if intended for an international environment.

6. Use spaces around operators and after commas, but not inside brackets:

 a = f (1, 2) + g (3, 4) 

7. Naming Conventions: There are several naming conventions that should be followed to make your program less complex and more readable. At the same time, the naming conventions in Python are a bit confusing, but here are a few conventions that can be easily followed. 
There is a general principle that names that are visible to the user as public parts of an API should follow conventions that reflect usage, not implementation. 
Here are some other naming conventions:

 b (single lowercase letter) B (single upper case letter) lowercase lower_case_with_underscores UPPERCASE UPPER_CASE_WITH_UNDERSCORES CapitalizedWords (or CamelCase). This is also sometimes known as StudlyCaps. Note: While using abbreviations in CapWords, capitalize all the letters of the abbreviation. Thus HTTPServerError is better than HttpServerError. mixedCase (differs from CapitalizedWords by initial lowercase character!) Capitalized_Words_With_Underscores 

In addition to these, multiple leading or trailing underscores are also considered. 
single_leading_underscore: weak indicator of "internal use". For example, objects whose name begins with an underscore are not imported from M import *.

single_trailing_underscore_: is used to avoid conflicts with the Python keyword. 

 Tkinter.Toplevel (master, class_ = 'ClassName') 

__double_leading_underscore: when naming class attribute causes name mangling. 
(inside the FooBar class, __boo becomes _FooBar__boo;).

__double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-managed namespaces. For example, __init__, __import__, or __file__ . Use them only as documentary.

8. Characters that should not be used for identifiers: "l" (lowercase el), "O" (uppercase oh), or "I" (uppercase eye) as variable names of the same character, as they are similar digits one and zero.

9. Do not use non-ASCII characters in identifiers if it is likely that people speaking another language will only read or support the code in the slightest degree. 

10. Name your classes and functions consistently
. The convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument.

11. Whereas method function naming always uses self for the first argument for instance methods and cls for first argument for class methods. If the name of the function argument matches the reserved words, then it can be written with a comma. For example, class_

You can refer to this simple program to learn how to write clear code:


 The factorial of 7 is 5040 

This article courtesy of Chinma Lenka . If you are as Python.Engineering and would like to contribute, you can also write an article using or by posting an article contribute @ See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.

# Python program to find factorial user supplied number.

# change the value for another result

num = 7

# uncomment to get user input
# num = int (input (& quot; Enter a number: & quot;))


factorial = 1

# check if the number is negative, positive or zero

if num & lt;  0 :

print ( "Sorry, factorial does not exist for negative numbers" )

elif num = = 0 :

print ( "The factorial of 0 is 1" )

else :

for i in range ( 1 , num   + 1 ):

factorial = factorial * i

print ( " The factorial of " , num, "is" , factorial)