How to avoid as far as possible from the entry to abandon python

Reference links: HTTPS: //blog.csdn.net/csdnnews/article/details/103193692 , if wrong, please contact deleted.

1 Introduction

Now is better than never, Although never
is often better than right now.   This is the Zen of Python in the sentence translated mean, maybe do not do better, but it might as well do without thinking on the hands. Indeed, before you start learning Python, you need to understand some of the background, to make some choices, and be prepared to work on your computer.

  Many beginners learning road map is like this: After learning basic grammar, do not know HTML and Ajax asynchronous protocol request, excitedly studied reptiles go; or, completion of basic grammar, to engage in visual recognition, even OpenCV the data structure can not read; even, completion of basic grammar direct neural network, the result is a repeat again from entry to give up.
  Note: The following is aimed at preparing follow through progressive learning personnel, if the case is the result of work in a hurry, do not apply to the following method.

  A Python programmer's growth roadmap should be like this: basic grammar -> Language Sense Training -> topic Exercise -> points the direction to continue learning -> Intermediate Programmer -> expand the depth and breadth -> advanced programmers.
Here Insert Picture Description
Here Insert Picture Description
Recommended for beginners : do not be anxious, not ambitious, step by step, slowly and surely, the power of nature into. Not anxious to avoid detours, not only will not slow down your pace of growth it, it would save you time. Not ambitious to reach a goal, and then develop the next target, the continuous success and progress, build self-confidence, stimulate greater interest in learning.

2. Preparation

1) various releases of Python

  Python is the only son of turtle t, but he has a lot of cousins, more famous there are quite a few. If you're not sure which is more friendly to you, I suggest you choose Python-- lineal and even if you need to deal with the future of his other cousins, that is Shumenshulu. Not recommended for beginners choose AnacondaPython and ActivePython, in my opinion not as good as they provide convenience to the user the trouble of learning more.

CPython
  CPython reference implementation belong Python, Python can be regarded as all other derivative distributions of a standardized version. CPython written in C language, and its authors included a number of core staff Python language's top decision-making levels. CPython in terms of optimizing performance was the most conservative. Of course, this is not a drawback, but the design orientation. Python maintainers who want to build for the Python CPython compatibility with the most widely standardized implementation. CPython most suitable for high compatibility with the conformance requirements of the standard Python users. In addition, CPython want to apply equally to the basic way to use Python and are willing to give up the convenience of some professionals.

Python Anaconda
  Anaconda Anaconda Company from the hand (formerly known as Continuum Analytics), its design goal is to serve those Python developers need to be provided by a commercial vendor support and have business support services. Anaconda Python's main use cases include mathematics, statistics, engineering, data analysis, machine learning, and other related applications. Anaconda bundled with Python business and science among the various types of common usage scenarios libraries - including SciPy, NumPy and Numba etc., to provide more library access capabilities through a set of customized package management systems.

ActivePython
  and Anaconda similar, ActivePython same company by the creation and maintenance of --ActiveState-profit enterprises. The company also runs sales multilingual and multi-language Komodo IDE. ActivePython major business-oriented users and data scientists - that want to use the Python language, but do not want to waste a lot of energy in the assembly and management of Python. ActivePython using conventional pip in the Python Package Manager, but speaking also certified compact package to provide hundreds of sets of common libraries, plus the Intel Math Kernel Library and some other public libraries have a third-party dependencies.

PyPy
  PyPy belonging CPython interpreter alternatives, utilizing time (JIT) compiler to execute the acceleration program Python. According to the actual execution of the task, its performance may be very significant. For the people complaining about the sound Python-- especially CPython, mainly revolves around its speed performance. By default, Python's speed far less than the C language - the gap may even reach several hundred times. PyPy JIT will compile Python code into machine language, leading to an average of 7.7 times the speed of CPython. In certain specific tasks, which can reach 50 times the speed effect.

Jython
  the JVM (Java virtual machine) can as an option in several languages in addition to Java run time. The long list includes Groovy, Scala, Clojure, Kotlin, Python , and - yes, of course, Jython. Jython biggest drawbacks support 2.x version of Python is its only. Currently ability to support Python 3.x version is still in development, but still take quite some time. Moment, there is no relevant version release.

IronPython
  like Jython on the JVM Python implementations positioning, IronPython belong when running a .Net based - or CLR (Common Language Runtime) - the Python implementation. IronPython using the CLR the DLR (dynamic language runtime) to allow Python programs to dynamically equivalent to the level achieved CPython operation. And similar Jython, IronPython currently only supports Python 2.x version. But IronPython 3.x implementations already in full swing in development.

2) install Python
  When Python download from the official website, please pay attention to choose the correct version. If it is for learning, download the latest version is no problem, if it is used in the production, you will have to consider the development needs of third-party modules support the latest version of Python. Do not forget to check the bottom two check boxes during installation, or bring some trouble will follow the module is installed. Recommended default installation.

3) conveniently choose a development tool
  to learn a programming language, first of all to find a combination of integrated development tools, it seems to be a natural idea. Why not? IDE can automatically filled, can be a key to run, you can breakpoint debugging. Use the IDE development projects, like driving a luxury car interior, enjoy the pleasure of driving is like, who would take care of the inside of the engine hood is how to work it? My younger colleagues are also fans of IDE, originally used pycharm, then is hot nowadays vscode.
  However, I have always believed that the programming is like driving, the driver is a programmer, and professional drivers not to drive as colored white-collar Jinling commuter skills. Since it is a professional driver, it can not be satisfied with a bow tie, white gloves driving interior luxury, high-end configuration of the car, but must have the ability to open the engine cover repairs and maintenance.
  Based on this view, I do not recommend beginners to start using integrated development tools - at least, do not use the IDE at the beginning of the first week. For Python Such an interpreted scripting language, a conveniently editor is sufficient. On the Linux platform, Vim or Emacs are good enough (or both if you are familiar with one of them, please accept my respect); on the Windows platform, I recommend using notepad ++. Benefits Python editor rather than IDE for beginners is:
  focus on Python itself, instead of being plagued by the use of tools. Do not you see, to know about the almost Pycharm and VSCODE of instructions, solutions to common problems, and more discussion was almost more than Python itself;
manually running code, can be more intuitive, more profound understanding of the interpretation during the execution of the script; manual debugging code It helps to code optimization, improve the ability to control codes; better use pip install and manage third-party modules.

4) accustomed to using IDLE, this is the best way to learn Python
  advantage of interpreted languages is that you can write an implementation of one, think of where to where to write to, do not have to finish all the procedures as like a compiled language, the compiler can be successful run. I especially like to use the python IDLE, and even use it as a calculator. Many times, I used the wording IDLE verification code is correct, to see whether the module is successfully installed and version number. IDLE support tab completion, I always check the methods and properties of an object using this function.

>>> import numpy as np
>>> np.__version__
'1.17.0'
>>> np.hypot(3,4)
5.0
>>> def factorial(n): # 计算n的阶乘
    if n == 0: # 递归出口
        return 1
    return n*factorial(n-1) # 向递归出口方向靠近的自身调用
 
>>> factorial(5)
120
>>> _*2-100
140

Tips

  1. tab completion
  2. Move the cursor to the pressing the return statement is executed, the command may be repeated
  3. An underscore (_) can obtain the results of the last execution

  In fact, IDLE is a IDE, you can use it to create or open a .py script file, you can edit, run, and debug.

3. strict compliance with coding standards

  On the Linux platform, a python source code file should be the following parts. On the Windows platform, you can omit the first term.
1. Statement interpreter
2. Statement encoding format
3. module annotated document or string
4. import module
5. constants and global variable declarations
6. The top is defined (class definition or functions)
7. execution code

Encoding format declared
  Typically, the encoding format of the statement is required. If the python source code file does not declare the encoding format, python interpreter will use the default ASCII encoding, once the source file containing non-ASCII character encoding, python interpreter will report an error. In UTF-8, for example, the following two statements are in line with the encoding format rules.

# -*- coding: utf-8 -*-
# coding = utf-8

I have been UTF-8 encoding format, prefer to use a declarative way first.
On the Windows platform, the encoding format declaration must be in the first line of the python file. The Linux platform, the second line coding format normally located python declaration file, the first line is the path statement python interpreter.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Indent
  uniform use 4 spaces to indent. Never use tab, tab and space do not mix. In the case of line connection, I generally use 4 spaces hanging indent. E.g:

var_dict = {
    'name': 'xufive',
    'mail': '[email protected]'
}       

Marks
  a natural language in double quotes
  2 machine identification single quotes
  3 double quotes using regular expressions
  4 documentation string (the docstring of) use three double quotes

Note
  the number # a space, separated by blank lines paragraphs member (also need numbers #):

   # 块注释
   # 块注释
   #
   # 块注释
   # 块注释

Inline comments, statements and using at least two separate spaces:

age += 1  # 年龄增加一岁

More important comment period, the use of multiple equal sign separated, can be more eye-catching, highlighting the importance of:

  server= gogame(room, options)
    
    # =====================================
    # 请勿在此处倾倒垃圾!!!
    # =====================================
    
    if __name__ == '__main__':
        server.run()

Blank line
  coding format specification, introduced module, the space between the constant and global variable declarations, the top two rows to define and execute the code;
  space defined between the top two rows 2, a blank line between method definitions;
  3 inside a function or method, It can be a blank line where necessary to enhance the sense of rhythm, but should avoid consecutive blank lines;

Space
  1 In a binary operation on both sides of each operator a space, arithmetic operators on both sides of the space may be used flexibly, but be sure to keep the same on both sides
  2 and no spaces in comma, semicolon, colon front of, but behind them should be added ( unless the end of the line)
  the parameter list of the function 3, include a space after the comma
  parameter list of the function 4, the default value is not equal on both sides to add a space
  after the left parenthesis 5, do not add to add a space before the right bracket
  6 parameter list, index, or should not be left parenthesis before slicing spaces

Document string
  document string bag, modules, classes or functions in the first statement. These strings may be automatically extracted by the object __doc__ member, and is used by pydoc. Documentation strings use triple double quotes ( "" "). If the document string content is not written in a single line, the first line must end with a period, question mark or exclamation point, then a blank line, the end of the triple double quotes must be on a separate line.

Import module
  after the import should always be placed on top of the file, located in the module comments and docstrings, the module before global variables and constants. Should be introduced in order from most common to least common packet sequence, a blank line between the groups:

  A standard library introduced
  2 third-party libraries introduced
  3 application specifies introduced

You should avoid introducing the following methods:


```python

```python
```python
from math import *

Naming
  a module to make use lowercase names, initials remain lowercase, try not to use an underscore
  class 2 using the hump (CamelCase) naming style, capitalized, beginning with an underscore private classes available
  3 function name all lower case, if more than one word , separated by an underscore
  4 available private function underscore a
  5 possible variable names in lower case, if a plurality of words, separated by an underscore
  6 constant all-uppercase, if a plurality of words, separated by an underscore

For ease of description, the first on a demo:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""通常这里是关于本文档的说明(docstring),须以半角的句号、 问号或惊叹号结尾!

本行之前应当空一行,继续完成关于本文档的说明
如果文档说明可以在一行内结束,结尾的三个双引号不需要换行;否则,就要像下面这样
"""
import os, time
import datetime
import math

import numpy as np
import xlrd, xlwt, xlutils

import youth_mongodb
import youth_curl


BASE_PATH = r"d:\YouthGit"
LOG_FILE = u"运行日志.txt"


class GameRoom(object):
    """对局室"""
    
    def __init__(self, name, limit=100, **kwds):
        """构造函数!
        
        name        对局室名字
        limit       人数上限
        kwds        参数字典
        """
        
        pass

def craete_and_start():
    """创建并启动对局室"""
    
    pass

if __name__ == '__main__':
    # 开启游戏服务
    start()

4. Run the code, debugging

  If you use integrated development tools, run and debug code, it is entirely dependent on the tool.
1) open if you write code using the editor, it would need to manually run and debug code . Run the code in two steps:
Step 1, a command to open the window, the script file path is switched to the folder. I used to blank the location of the window script (make sure nothing is selected), press the shift key and click the right mouse button in the pop-up menu, select Open Powershell window. Step 2, enter python + space + script file name, press Enter to run. When entering the script file name, press the tab can be automatically filled. Run a script, error messages, and other operating results, are displayed in this window. This is the most primitive of information. You see the information in other development tools, all reprocessing of such information.
  Means no more manual debugging code, in addition to print information, debug information is written almost exclusively to debug files. It sounds like a serious problem, in fact, just different ideas and orientations.
2) using the built-in editor python the IDLE . Select to perform .py file, right click and choose "Edit with IDLE", open the editor.

5. Management Module

  In the development of the Python language, the installation of third-party modules and management methods have also gone through changes. Now, we finally have this almost pip perfect tool. In addition to pip install (install) and remove (uninstall) these two features, you can also specify the installed version of the module (without pre-delete the currently installed version), you can display all installed modules can also be upgraded by self-upgrade.

Many students in the use of pip will encounter all sorts of weird, I explain here:

1. install the py2 / py3, there will be multiple versions of pip.exe, if you run directly:

pip install <模块名>

It is possible because pip.exe the wrong version of the error. The correct approach is to specify the python version, -m parameter python interpreter, meaning that the pip module as a script, complete command as follows:

py -3 -m pip install <模块名>

2. Anaconda, IDE or install module
if you are using Anaconda Python, or you are IDE install module, it is necessary to check their configuration and settings, the situation is different, there is no uniform solution.

3. Only installed py2 or py3
If your computer has only a py2 or py3 installation, then the above two methods of installation should be no problem. If your computer can not find pip.exe file, you can use this command to generate:

python -m ensurepip

4.pip application demonstrates
  the following, with regard to computer while py2 / py3 installed Example (If only py2 or Py3, the following demonstrates the need py -3 replaced Python), presentation module pip usage. Regardless of any situation, I recommend using the -m parameter called pip module, rather than directly using pip.exe install the module.

# 虽然可以这样使用pip安装模块(以numpy为例)
PS D:\XufiveGit\wxgl> pip install numpy
 
# 但我建议这样使用pip
PS D:\XufiveGit\wxgl> py -3 -m pip install numpy
 
# 删除模块
PS D:\XufiveGit\wxgl> py -3 -m pip uninstall numpy
 
# 安装模块,指定版本
PS D:\XufiveGit\wxgl> py -3 -m pip install numpy=1.15.0
 
# 自主升级
PS D:\XufiveGit\wxgl> py -3 -m pip install --upgrade pip
 
# 显示已安装的模块
PS D:\XufiveGit\wxgl> py -3 -m pip list
Package           Version
----------------- -----------
-pencv-python     4.1.1
argh              0.26.2
attrs             19.1.0
Automat           0.7.0
basemap           1.2.0
beautifulsoup4    4.6.3
bleach            3.1.0
cefpython3        66.0
... ...

pip also be used to install a local whl file:

PS D:\PyPackage\py3> py -3 -m pip install .\basemap-1.2.0-cp37-cp37m-win_amd64.whl

6. publish your module installation package

  If you have a lot of projects, some will be used in your own modules, then, these modules make installation package, install it on your development environment, probably the best choice. Reference: "study notes: post your own python module installation package" In this regard there are detailed (https://blog.csdn.net/xufive/article/details/88640229).

Published 58 original articles · won praise 42 · views 110 000 +

Guess you like

Origin blog.csdn.net/m0_37251750/article/details/103798982