Acquaintance PythonDay1

Day01 - acquaintance Python

Introduction to Python

Python's history

Christmas 1989: Guido von Rossum began to write Python language compiler.

February 1991: The first Python compiler (also interpreter) was born, it is implemented in C language (later reappeared other implementations of Java and versions of Jython and IronPython C # implementation, as well as PyPy, Brython, Pyston etc. ), you can call the C language library functions. In the earliest version, Python has provided support for the "category", "function", "Exception Handling" like building blocks, while providing a "List" and "dictionary" and other core data types, while supporting module basis the expansion of the system.

January 1994: Python 1.0 released.

2000 October 16: Python 2.0 release adds achieve a complete garbage collection, provides support for Unicode. At the same time, Python's entire development process more transparent, impact on community development progress gradually expanding ecosystem slowly began to form.

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 a small change (such as a Bug fixes ), as long as the modification increases C. If you are interested in Python's history, you can view the blog article entitled "A Brief History of Python" is.

Python's strengths and weaknesses

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

Simple and clear, do one thing only one way.

Low learning curve compared with many other languages, Python is easier to use.

Open source, has a strong community and ecosystem.

Interpreted language, inherently platform portability.

Supports two mainstream programming paradigm (object-oriented programming and functional programming) have provided support.

Scalability and embeddable can be called C / C ++ code can also call Python in C / C ++ in.

High degree of standardized codes, readable, there are codes for obsessive compulsive disorder and crowd.

Python shortcomings mainly in the following points.

Slightly lower efficiency and therefore computationally intensive tasks can be written in C / C ++.

Code can not be encrypted, but now many companies are not selling software, but selling services, the issue will be diluted.

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

Python is currently in the field of cloud infrastructure, DevOps, web crawler development, data analysis, mining, machine learning, etc. 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, and scientific computing visualization, data analysis, quantitative trading, robot development, a series of image recognition and processing jobs.

Build a programming environment

Windows environment

Can be downloaded to Python Windows Installer (exe files) in Python official website, it should be noted that if the installation need to install Service Pack 1 patch package in the Windows 7 environment (can automatically install system patches through a number of tools software features to install ), the installation process is recommended by checking the "Add Python 3.6 to PATH" (Python 3.6 added to the PATH environment variable) and choose a custom install, set "Optional Features" interface is best to "pip", "tcl / tk", "Python test suite" all other items on the check. It is strongly recommended to use a custom installation path and ensure that the path is not Chinese. Installation will be completed to see the "Setup was successful" prompt, but when you start Python environment may be missing because of some dynamic link library file which led to the Python interpreter can not run common problem is api-ms-win-crt *. dll missing and update DirectX cause some dynamic link library file is missing after the former can be processed with reference to "api-ms-win-crt * .dll missing cause analysis and solutions" approach a text to explain or directly in Microsoft's official website to download Visual C ++ Redistributable for Visual Studio 2015 to repair the file, which you can download a repair tool to repair DirectX.

Linux environment

Linux environment comes with Python 2.x version, but if you want to update to version 3.x can be installed in the official Python Web site to download the source code and install Python source code is structured by the way, concrete steps are as follows .

(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

Download Python source code and unzip to the specified directory.

wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

xz -d Python 3.7.3.tar.xz

tar -xvf Python 3.7.3.tar

Switch to execute Python source code directory and the following command is executed and installed.

cd Python-3.7.3

./configure --prefix=/usr/local/python37 --enable-optimizations

make && make install

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

# ... the code is omitted here above ...

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

# ... the code is omitted here below ...

source .bash_profile

MacOS environment

MacOS also comes with a version of Python 2.x, 3.x version can be installed via the installation file (pkg file) Python's official website. After the default installation is completed, it can be executed by a terminal in order to start the python version 2.x Python interpreter, version 3.x can start by executing python3 Python command interpreter.

From a terminal program running Python

Confirm the version of Python

Terminal type the following command or command line prompt.

python --version

Of course, you can first enter into the python 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 write Python source code with a text editor (recommended Sublime, Atom, TextMate, VSCode and other advanced text editing tools) and name it saved as hello.py, 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

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.

Single-line comments - part beginning with # and spaces

Multi-line comments - three at the beginning of quotation marks, three closing quotes

"""

First Python Program - hello, world!

To pay tribute to the great Mr. Dennis M. Ritchie

Version: 0.1

Author: Muni

"""

print('hello, world!')

# Print ( "Hello, world!")

print ( 'hello', 'world')

print('hello', 'world', sep=', ', end='!')

print('goodbye, world', end='!\n')

Other tools introduced

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.


IPython - better interactive programming tools

IPython is based Python interactive interpreter. Compared to native Python Shell, 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 jupyter

or

python -m pip install ipython jupyter

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


Of course, we can also operate interactively in a browser window running through the project called notebook Jupyter.

jupyter notebook


anaconda - a one-stop scientific data artifact

Anaconda refers to is an open source Python release, which contains conda, Python and more than 180 scientific packages and their dependencies. Because it contains a large number of scientific package, download the file Anaconda relatively large (approximately 531 MB), if only need some packages, or need to conserve bandwidth or storage space can also be used Miniconda this small release (and only contain conda Python). Data for the study of science is, anaconda is absolutely artifact, easy to install, and support the anaconda install software [for example ipython previously mentioned, jupyter notebook, and even scientific data and other software R] describes a very valuable the only problem now is that Tsinghua mirror service has been closed, transnational download will be slower

Sublime - text editor Artifact


First, you can install or Sublime 2 Sublime 3 through the official website to download the installer.

Installation package management tools. 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')

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.

PyCharm - Python development Artifact

PyCharm install, configure and use we will introduce later.


Exercise

Check the following code results in a Python interactive environment, and the content translated into Chinese.

import this

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

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

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 ()

————————————————

Disclaimer: This article is the original article CSDN bloggers "MuNian123", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.

Original link: https: //blog.csdn.net/qq_42370150/article/details/103540159

Published 232 original articles · won praise 93 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42370150/article/details/104916942