Python is an interpreted high-level programming language with dynamic and strong typing disciplines.
Use 4 spaces(U+0020) every indentation level.
Space(U+0020) is preferred instead of Tab(U+0009) for indentation.
If you use mixing indentation of spaces and tabs in Python 3, it will result a indentation error. In Python 2, tabs in the code will be converted to spaces.
The max line length should be limited to 79 characters (Excluding line feed and carriage return).
In this case, the proper format to include a extremely long name should be:
with open('/path/to/a/extremely/long/name/file\
/that/you/want/to/read.txt') as file_1:
# do something here
file_1.close()Separate lines of a long expression:
result = (calculate_result(param1, param2) - \
100 * fixed_coefficient / \
(large_int + another_large_int) + \
addition_number)
# Or place operators after line break
result = (calculate_result(param1, param2)
- 100 * fixed_coefficient
/ (large_int + another_large_int)
+ addition_number)Imports also should be on separate lines:
import os
import sys
# However, it is OK to import several
# methods from a library on single line.
from library import method1, method2Surround top-level function and class definitions with two blank lines. Surround method definitions single blank line.
# declare a function
def function1():
pass
def function2():
function1()
class no_name_class:
def __init__(self):
pass
def method(self, param):
pass
There are two ways to comment a line in python
Starts with a # - # This is a line of comment or surrounds with triple " - """ This is a line of comment """
# Describe what we want to do next,
# never describe the code itself
# eg. We use method to find out
# the largest number in the array
def method(param):
passA least two spaces from the statement.
x = (x + 1) & (y - 1); # Increment on x or reset to 0 if x >= yDocstrings are the comments to describe methods, functions and classes.
def merge_sort(arr, low, high):
"""Merge sort on array
Detail information to describe the merge sort.
Args:
arr: an array to sort.
low: start index of the array.
high: end index of the array.
Returns:
A sorted array.
e.g.
......
Raises:
some_typeof_error: in what cases will the error occurs.
"""
pass # Method goes here
class SampleClassName(object):
"""Briefly describe the class
Detail
...
Attributes:
"""# Lower case words with underscores (snake case)
import package_name
import module_name# Upper case words combined (camel case)
class ClassName(object):
__init__(self):
pass# Basically same as class names
class ExceptionName(Exception):
passglobal global_var_namevariable_name = 123
def function_name():
passclass ClassName:
def instance_method(self, ...):
pass
@classmethod
def classMethod(cls, ...):
pass
@staticmethod
def staticMethod():
pass
def function_name(method_parameter_name):
pass# All capital letters with underscores
CONSTANT_NAME = 123# Includes __new__, __init__, __str__ ...
class ClassName(object):
def __init__(self):
pass
def __str__(self):
pass
if some_conditions:
# some_conditions must return a boolean value
pass
else:
pass if statement can have multiple elif and else is optional.
x = int(input("Please enter an integer: "))
if x > 0:
print("Your input is positive")
elif x < -1:
print("Your input is less than -1")
elif x < 0 and x >= -1:
print("Your input is less than negative and larger than -1")
else:
print("Your input is 0")numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
# Output is
"""
1
2
3
4
5
"""while some_conditions:
# some_conditions must return a boolean value
do_something()In Python, there is no do-while loop, but you can implement it like this:
while True:
do_something()
if fail_condition:
breaktry:
do_something()
except IOError:
print("IOError occurs")
else:
print("No IOError occurs")
finally:
do_something_finally()
# THIS WOULD ALWAYS BE EXECUTEDRaise an exception:
raise ExceptionName(Error)integer = 1
float_number = 0.12345
pi = 3.1415
string = 'Hello'
object = function # function object
global global_val # a global variable
global_val = 123class ClassName:
def method(self):
pass
def function():
pass
# Call method and function
sample_class = ClassName()
sample_class.method()
function()CONSTANT_STRING = 'abc'
CONSTANT_INT = 123class ClassName(object):
def __init__(self, param):
self.param = param
def method(self, param1, param2):
return param1 + param2
def __str__(self):
return 'This is a class'
for _ in range(5):
do_something()Underscore (_) variable means throwaway in Python.
str1 = '%s, %d!' % ('Hello', 12345)
# str1: 'Hello, 12345!'
str2 = 'language: {}, version: {}'.format('Python', 3.6)
# str2: 'language: Python, version: 3.6'
string = str1 + str2
# string: 'Hello, 12345!language: Python, version: 3.6'It is better to use """ for multiline strings instead of '''.
# first way
print("This is a multiline string.\n"
"This is the second line.\n")
# second way
print("""This is a multiline string.
This is the second line.
""")Though they produce the same outputs, the first one is better in this case.
empty_list = []
integer_list = [1, 2, 3, 4, 5]
string_list = ['a', 'b', 'hello']
mixed_list = [1, 'hello', 1.5, (1, 'tuple'), ['banana', 1000]]
matrix = [
[1, 2, 3, 4],
[2, 4, 6, 8],
]Dictionary in Python is similar to JSON object.
dictionary = {} # an empty dictionary
example_small = {'key': 'value', 'number': 2}
example_large = {
'key1': 'value1',
'key2': 'value2',
...
}.matches any character except a newline.^matches the start of the string.$matches the end of the string.*matches 0 or more repetitions of the preceding regular expression.+matches 1 or more repetitions of the preceding regular expression.?matches 0 or 1 repetitions of the preceding regular expression.
- PEP 8, Style Guide for Python Code, van Rossum
- Naming convention (programming) - Wikipedia
- Google Python Style Guide, Amit Patel, et al.
- Regular expression operations - The Python Standard Library