Learn about the 91 tips for writing high-quality code-improving Python programs and documenting your reading notes
Use version: Python 3.4.0
System: Windows7
1. String formatting:
1 defShow (name,age,email):2 #Common Methods3 Print('Your name is:%s \nyour:%i \nyour e-mail is:%s'%(name,age,email))4 #more clearly referenced methods5 Print('your name is:% (name) s \ n Your age are:% (age) I \nyour e-mail is:% (email) s'%{'name': Name,' Age': Age,'Email': Email}) 6 #Str.format () string formatting method7 Print('your name is: {name} \nyour age was {age} \nyour e-mail is: {email}'. Format (name = Name,age = Age,email =email))8 9 if __name__=='__main__':TenShow'WWL', 24,'[email protected]')
2. Package Manager: Pip
official website: https://pip.pypa.io/en/latest/3. Version 4 contains the scripts directory where pip is stored in the Python installation path. You only need to add this path to the system's path path.
Install package: Pip Install Somepackage
Unload Package: Pip Uninstall somepackage
Upgrade Package: Pip Install--upgrade somepackage
Show installation package: Pip list
Show installation packages which need to be upgraded: Pip list--outdated
Display package information: Pip show somepackage
Shows which files are included in the installation package: Pip Show--files somepackage
$ pip Install somepackage # latest version$ pip install somepackage==1.0.4 # specific version$ pip install ' Somepa ckage>=1.0.4 ' # Minimum version
StackOverflow above discussion of PIP: http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows/ http://stackoverflow.com/questions/2436731/does-python-have-a-package-module-management-system/13445719 Http://stackoverflow.com/questions/3220404/why-use-pip-over-easy-install
3. Code Analysis Tools
Pylint official Website: http://pylint.org/8
4. Ternary operation
C? X:y value x When C is true, otherwise the value Ypython equivalent form x if C else Y>>> A = 3>>>Print('haha')ifA <5Else 'hehe'haha>>>Print('haha')ifA <2Else 'hehe''hehe'
5.switch.. Case
there is no switch...case in Python ... The following two methods are used instead of:1.if...elif...ifx = =0:return(' the')elifx = = 1: return('1111')2. {}.get (x) {0:' the', 1:'1111'}.get (0)
6. Assert assert
>>> x = 1>>> y = 2>>>assertx = = y,'Not equals'Traceback (most recent): File"<pyshell#2>", Line 1,inch<module>assertx = = y,'Not equals'Assertionerror: notequals is equivalent to the following>>>if __debug__ and notx = =y:RaiseAssertionerror ('Not equals') Traceback (most recent): File"<pyshell#13>", Line 2,inch<module>RaiseAssertionerror ('Not equals') Assertionerror: notequals where __debug__ is true and cannot be modified
There is a cost to using assertions, which can have a certain effect on performance. The way to disable assertions is to add-o when running scripts (ignoring assertion-related statements)
Assertions are designed to capture user-defined constraints, rather than to capture the program's own errors
7. Intermediate values are not recommended for numeric exchange
Common methods: Temp=xx=yy=Temp Recommended method: X, y=y,x analysis: Calculate first=the value on the right is (y,x) and then assigns a value to the X, y variable performance comparison:>>>ImportTimeit>>> Timeit. Timer ('x, y = y,x','x = 1;y = 2'). Timeit ()0.03071109231768787>>> Timeit. Timer ('temp = x;x = Y;y = Temp','x = 1;y = 2'). Timeit ()0.035570626849676046
8. Lazy Calculation Lazy evaluayion
if and Y no longer calculates when X is false if or Y when x is true y no longer calculates the order of the X and Y expressions that affect the execution time of the program.
9. Enumerate enum
Enums has been added to Python 3.4 as describedinchPEP 435. It has also been backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and2.4On PyPI. To use Backports, do $ pip install ENUM34, installing enum (no numbers) would install a completely different andincompatible version. fromEnumImportEnumanimal= Enum ('Animal','ant Bee Cat Dog') is equivalent toclassAnimals (Enum): Ant= 1Bee= 2Cat= 3Dog= 4The above is a link to the handling: http:stackoverflow.com/questions/36932/how-can-i-represent-an-enum-inch-python
10. Type check recommended isinstance instead of type
A type check is OK for the built-in base type (), and for a user-defined type that is based on the built-in type extension, type does not accurately return the result;
>>> Import Types
>>> class Userint (int):
def __init__ (self,val=0):
self._val = Int (val)
>>> n = userint (2)
>>> N
2
>>> type (n)
<class ' __main__. Userint ' >
>>> isinstance (N,int)
True
There is a difference between the processing of type () 2.7 and 3.4 for class, such as:
Python reading notes 1