01. Acquaintance Python

Acquaintance Python

Introduction to Python

Python's history

  1. Christmas 1989: Guido von Rossum began to write Python language compiler.
  2. February 1991: The first Python compiler (also interpreter) was born, which is implemented in C (later), you can call the C language library functions. In the earliest version, Python has provided support for the "class", "function", "exception handling" and other building blocks, as well as for lists, dictionaries and other core data types, supports module-based application to construct .
  3. January 1994: Python 1.0 released.
  4. 2000 October 16: Python 2.0 release adds complete garbage collection , provides Unicode support. At the same time, Python's entire development process more transparent, impact on community development progress gradually expanding ecosystem slowly began to form.
  5. 2008 December 3: Python 3.0 release, it is not fully compatible with previous Python code, but because there are many companies currently use Python 2.x version of the project and operation and maintenance, so many new Python 3.x characteristic was also later ported to Python 2.6 / 2.7 version.

The current version of Python 3.7.x we are using was released in 2018, Python version is divided into three sections, shaped like ABC. Where A represents the major version number, usually when the overall rewrite, or not backward-compatible changes occur, increased A; B when B represents an increase feature updates, the emergence of new features; C represents the minor changes (for example: Fixes a Bug), as long as the modification increases C. If you are interested in Python's history, you can read called "A Brief History of Python" blog.

Python's strengths and weaknesses

Python's advantages are many, simple can be summarized as follows.

  1. Simple and clear, do one thing only one way.
  2. Low learning curve compared with many other languages, Python is easier to use.
  3. Open source, has a strong community and ecosystem.
  4. Interpreted language, inherently platform portability.
  5. Of the two mainstream programming paradigm (object-oriented programming and functional programming) have provided support.
  6. Scalability and embeddable, for example, may be invoked in Python C / C ++ code.
  7. High degree of standardized codes, readable, there are codes for obsessive compulsive disorder and crowd.

Python shortcomings mainly in the following points.

  1. Slightly lower efficiency and therefore computationally intensive tasks can be written in C / C ++.
  2. Code can not be encrypted, but now many companies do not sell software sales but sales service, the issue will be weakened.
  3. Too many in the development framework that can be selected (such as Web framework there are more than 100), have a choice where there is an error.

Python applications

Currently the Python Web application development, cloud infrastructure, DevOps, network data collection (reptiles), data mining analytics, machine learning have a wide range of applications, and therefore also had a back-end Web development, data interface development, operation and maintenance of automation , automated testing, scientific computing and visualization, data analysis, quantitative trading, robot development, natural language processing, image recognition and a series of related posts.

Installing Python interpreter

Want to start Python programming journey, first have to install the Python interpreter environment on the computer for their own use, the following will install the official Python interpreter, for example, explain how to install the Python environment on different operating systems. The official Python interpreter is written in C language, and it is the most widely used Python interpreter, usually called CPython. In addition, Python interpreter as well as Java language version of Jython, IronPython C # language and PyPy, Brython, Pyston, etc., we do not introduce these contents, interested readers can inform themselves about.

Windows environment

May Python official website to download the Python Windows Installer (exe files), note that if you install Python 3.x in the Windows 7 environment, you need to install Service Pack 1 patch (the system can be installed automatically by the software tools patch feature to install), the installation process is recommended checking the "Add Python 3.x to PATH" (Python 3.x will add to the PATH environment variable) and choose a custom install, set "Optional features" interface will be the best. " pip "," tcl / tk " ," Python test suite " all other items on the check. It is strongly recommended to select a custom installation path and ensure that the path is not Chinese. Installation will be completed to see the "Setup was successful" prompt. If you run a Python program later, there is a problem because of the lack of some dynamic link library file which led to the Python interpreter can not work, can be resolved in accordance with the following method.

If you see api-ms-win-crt * .dll files are missing, you can refer to "api-ms-win-crt * .dll missing Cause Analysis and Solutions" treatment method of an article to explain or directly in Microsoft's official website to download Visual C ++ Redistributable for Visual Studio 2015 file repair; if the cause is due to some lack of dynamic link library file issue after updating Windows, DirectX, you can download a DirectX repair tool to repair.

Linux environment

Linux environment comes with Python 2.x version, but if you want to update to version 3.x, you can at the official Python site for installation to download and install the Python source code from source code to build the way, concrete steps are as follows (with CentOS for example).

  1. (Because there is no such dependencies may be deleted because the underlying dependencies fail during the installation source member) dependent libraries.
yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
  1. Download Python source code and unzip to the specified directory.
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
xz -d Python-3.7.6.tar.xz
tar -xvf Python-3.7.6.tar
  1. Switch to execute Python source code directory and the following command is executed and installed.
cd Python-3.7.6
./configure --prefix=/usr/local/python37 --enable-optimizations
make && make install
  1. Modify the file named .bash_profile in the user's home directory, and configure the PATH environment variable to take effect.
cd ~
vim .bash_profile
# ... 此处省略上面的代码 ...

export PATH=$PATH:/usr/local/python37/bin

# ... 此处省略下面的代码 ...
  1. Activated environment variables.
source .bash_profile

macOS environment

macOS also comes with Python 2.x version, it can Python's official website to install Python 3.x version of the installation file (pkg file) provides. After the default installation is complete, you can execute the terminal pythoncommand to start the 2.x version of the Python interpreter, start 3.x version of the Python interpreter needs to execute python3commands.

Run Python programs

Confirm the version of Python

This Windows command line prompt, type the following command.

python --version

Or type the following command in a terminal or macOS Linux system.

python3 --version

Of course, you can first enter python or python3 enter the interactive environment, and then execute the following code to check the version of Python.

import sys

print(sys.version_info)
print(sys.version)

Writing Python source code

You can use a text editor (recommended Sublime , Visual Studio Code and other advanced text editing tools) to write Python source code and save the file with a name as a suffix py, as shown in the code as follows.

print('hello, world!')

Run the program

Change to the directory where the source code and execute the following command to see whether or not a "hello, world!" On the screen.

python hello.py

or

python3 hello.py

Comments in the code

Note is an important part of the programming language used to explain the role of the code in the source code so as to enhance the readability and maintainability, of course, the source code may be run without the participation of code segments removed through annotations this is often used when debugging a program. Comments will be removed when entering the preprocessor with the source code or compiled, are not retained will not affect the results of the implementation of the program in object code.

  1. Single-line comments - part beginning with # and spaces
  2. Multi-line comments - three at the beginning of quotation marks, three closing quotes
"""
第一个Python程序 - hello, world!
向伟大的Dennis M. Ritchie先生致敬

Version: 0.1
Author: 骆昊
"""

print('hello, world!')
# print("你好,世界!")
print('你好', '世界')
print('hello', 'world', sep=', ', end='!')
print('goodbye, world', end='!\n')

Python development tools

IDLE - comes with integrated development tools

IDLE Python installation is carrying on integrated development tools, as shown in FIG. However, due to IDLE user experience is not so good so rarely used in the actual development.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-vj5txAuq-1581289679794) (./ res / python-idle.png)]

IPython - better interactive programming tools

IPython is based Python interactive interpreter. Compared to native Python interactive environment, IPython provides a more powerful editing and interactive features. Python by the package management tools mounted pip IPython and Jupyter, the specific operation is as follows.

pip install ipython

or

pip3 install ipython

After a successful installation, you can start IPython, shown below by the following ipython command.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-nFaQeXHz-1581289679794) (./ res / python-ipython.png)]

Sublime Text - Advanced Text Editor

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-3XvZSoow-1581289679795) (./ res / python-sublime.png)]

  • First, through the official website Text 2 Installation Sublime Sublime Text 3 or download the installer.

  • Installation package management tools.

    1. Ctrl + `shortcut key or selecting Show Console console Open the View menu, enter the following code.
    • Sublime 3
    import  urllib.request,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib.request.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())
    
    • Sublime 2
    import  urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp)ifnotos.path.exists(ipp)elseNone;urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read());print('Please restart Sublime Text to finish installation')
    
    1. Enter in your browser to download the installation package https://sublime.wbond.net/Package%20Control.sublime-package package management tools, and found under the installation directory Sublime directory named "Installed Packages" and just download the file this file is added into the next, and then restart Sublime Text to get.
  • Install the plugin. Open a command panel by the Package Control Preference menu or shortcut keys Ctrl + Shift + P, enter the Install Package panel can be found in the tools installed plug-ins, and then look for plug-ins required. We recommend that you install the following plug-ins:

    • SublimeCodeIntel - automatic code completion tool plug.
    • Emmet - front-end development code templates plugins.
    • Git - version control tool plug-ins.
    • Python PEP8 Autoformat - PEP8 automatic formatting specification plug.
    • ConvertToUTF8 - convert a local encoded as UTF-8.

Description: Actually Visual Studio Code may be a better choice, it does not need to spend money and provide a more complete and powerful features, interested readers can study on their own.

PyCharm - Python development Artifact

PyCharm installation, configuration and use of the "Fun PyCharm" were introduced, interested readers can choose to read.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-i2RZNpVd-1581289679795) (./ res / python-pycharm.png)]

Exercise

  1. Enter the following code in the Python interactive environments and see the results, try to see the content translated into Chinese.

    import this
    

    Description: Enter the above code in Python interactive environment can be seen in Tim Peter wrote "Zen of Python" , which tells the truth not just for Python, also applies to other programming languages.

  2. Learn to use the turtle to draw graphics on the screen.

    Description: turtle is a very interesting built-in Python modules, especially suitable for computer program design first experience of a small partner, it was originally part of the language Logo, Logo language is Wally Feurzig programming language and Seymour Papert in 1966 the invention.

    import turtle
    
    turtle.pensize(4)
    turtle.pencolor('red')
    
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    
    turtle.mainloop()
    

    Tip: The code provided in this chapter as well as drawing and painting pig Pecs flag code, interested readers, please own research.


I welcome the attention of the public number, reply keyword " Python ", there will be a gift both hands! ! ! I wish you a successful interview ! ! !

Published 95 original articles · won praise 0 · Views 3077

Guess you like

Origin blog.csdn.net/weixin_41818794/article/details/104243604