1. Introduction to Python
Python was born in 1989, founder Guido van Rossum (Guido van Rossum). Python is a language between C and Shell, full-featured, easy to learn, easy to use, and extensible.
?
The following is the latest Tiobe leaderboard (Https://www.tiobe.com/tiobe-index)
?
Python is now primarily used for Web development (Django Framework, Tornado Framework, Flask,bottle), network programming (support for highly concurrent twisted Network frameworks), crawlers, cloud computing, artificial intelligence, game development, financial analysis, and operational development, Now Python is one of the development languages that every OPS engineer must master ~ ~
2. Compiled language and interpretive language
A compiled language requires a dedicated compiler that compiles code into machine code for a specific platform (operating system), and then a link process that connects the machine code of each module to a dependent library to generate an executable file.
?
Advantage : The compiler is able to optimize the code during the precompilation process. Compilation takes only one time, and the resulting executables can run independently of the language environment on a particular platform and run efficiently.
cons : porting problems between different platforms requires compiling multiple executables based on different platforms. Once the compilation is complete, the entire module will be recompiled if it needs to be modified.
?
Representative languages are C, C + +, object-c .....
interpreted Language
The interpreted language does not need to be compiled in advance, and is interpreted by the interpreter on a row-by-line basis and then run.
?
Pros : Good platform compatibility, as long as there are interpreters, the same code can be run on different platforms
cons : Runs slower than a compiled language.
?
Representative languages are Python, Ruby, PHP, JavaScript, Erlang
?
Tip:Java is a mixed language, and Java needs to be compiled before it can be run, but after compiling it is not compiled into machine language like the c,c++ language, Instead of being compiled into platform-independent bytecode. class files, bytecode files cannot be run directly on the operating system and need to be translated by a Java virtual machine into a local machine code to execute, and the process is to read an instruction, then translate, finally execute, and translate the process.
Python is an interpreted language , and when the Python program is executed, the Python interpreter first converts the source code into bytecode, which is then executed by the Python interpreter, which is a process that runs each time. Compared to the compiled language, each execution is more than the compilation, the link process, the speed is slightly slow, but it is easier to code porting, and do not worry about the compilation of the program, library link loading problems ~
?
Python provides a way to compile a Python program into a bytecode store and run the bytecode file directly at run time, for efficiency reasons. Python program generally run, the resulting bytecode will not be preserved, only when import Py file imports, will produce PYC bytecode file, and at run time, replace the original file directly run ~
3. Python Interpreter
There are many kinds of Python interpreters, specifically categorized as follows:
- Cpython
The official version of Python, also the most commonly used version, uses the C language implementation, the CPython implementation converts the source file (py file) into a bytecode file (PYc file), and then executes the bytecode file.
- Jyhton
Python Java implementation, Jython will dynamically compile Python code into Java bytecode, and then run on the JVM.
- IronPython
In Python's C # implementation, IronPython compiles Python code into C # bytecode and then runs on the CLR. (similar to Jython)
- PyPy (Special)
Python implements Python, which compiles Python bytecode bytecode into machine code.
- Rubypython, Brython ...
? 4, Python2 and Python3
The 3 version of Python, often referred to as Python 3000. This is a large upgrade relative to earlier versions of Python. In order not to take too much of a burden, Python 3.0 did not consider backwards compatibility when designing. Early Python versions of the program were not able to perform properly on Python 3.0.
Python 2.6, as a transitional version, basically uses the syntax and library of Python 2.x, taking into account the migration to Python 3.0, allowing the use of some of the syntax and functions of Python 3.0.
Overall Python3 than 2.x made a lot of improvements, if the development of new projects, priority to open the rate of use of Python3 (python2.x will eventually be replaced)
The specific differences between python2.x and python3.x can be found in: http://www.runoob.com/python/python-2x-3x.html
5. Installation of Python installation on Windows
Website address: https://www.python.org/downloads/windows/
Installation process follow the prompts ~ ~
Test whether the installation was successful
Win + R (run)--input cmd, enter python at the command line, carriage return, enter the interactive environment to indicate successful installation (not the latest version installed here)
Installation on Linux (CentOS7 for example)
python2.7.5 is installed by default on CentOS7, to use python3.x, install another python (do not delete the original Python,yum relies on the default installation of Python)
Download Python3
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Install dependent environments
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-develyum install gcc gcc-c++ -y # 编译需要的环境
Compiling and installing Python3
mkdir -p /usr/local/python3tar xf Python-3.6.6.tar.xzcd Python-3.6.6./configure --prefix=/usr/local/python3make && make install
If the installation succeeds, the following information is displayed:
..............Looking in links: /tmp/tmp8k7hc304Collecting setuptoolsCollecting pipInstalling collected packages: setuptools, pipSuccessfully installed pip-10.0.1 setuptools-39.0.1
This means that the compiler installs Python-3.6.6, will automatically help us install Setuptools, Pip (The source package contains the two modules), you can directly use Pip to install Python third-party modules, such as installation of Ipython:
pip3 install ipython
If you want to install PIP for the default Python (python2.7) on CENTOS7, you can install it directly using Yum:
yum install -y python-pip
Adding environment variables for Python3
vim /etc/profile.d/python3.shPYTHON3_HOME=/usr/local/python3export PATH=$PATH:${PYTHON3_HOME}/bin编辑完运行脚本,使环境变量生效source /etc/profile.d/python3.sh
Enter Python3 on the command line to enter the Python interactive environment
[[email protected] ~]# python3Python 3.6.6 (default, Jul 19 2018, 10:19:19)[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linuxType "help", "copyright", "credits" or "license" for more information.
When installing the Ipython module above, you can see that the download speed is very slow, because the downloaded third-party library from the official source of Python: https://pypi.org/pyp, first download to local, and then unpack the installation. In China, it is recommended to use the source of watercress: http://pypi.douban.com/simple/
When installing a module using PIP, specify the image source to use:
pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple ipython
It is troublesome to write like this every time, and can be written in the configuration file:
mkdir ~/.pip # 家目录下创建 .pip 目录 或者 .config 目录(不存在创建)vim ~/.pip/pip.conf添加如下内容:[global]timeout = 60index-url = https://pypi.doubanio.com/simpleTip:如果使用http链接,需要指定trusted-host参数[global]timeout = 60index-url = http://pypi.douban.com/simpletrusted-host = pypi.douban.com
The above is the configuration file pip.conf on the Linux storage path
mac下存放路径:$HOME/Library/Application Support/pip/pip.conf或者$HOME/.pip/pip.confwindows下存放路径(配置文件名称为pip.ini):%APPDATA%\pip\pip.ini或者%HOME%\pip\pip.ini
6. python File header
In a Linux environment, there are two common ways to execute Python scripts:
1) Python filename.py
2)./filename.py
The first method has already specified the use of the Python interpreter to execute the script, and the second method does not specify that the interpreter needs to be specified in the header of the script file:
#!/usr/bin/env python # PATH环境变量 中的第一个 python 来执行脚本或#!/usr/bin/python # 使用绝对路径指定python解释器位置
There is usually a line in the head of the Python script that tells the Python interpreter to interpret the Py file in Utf-8 encoding.
# -*- coding: utf-8 -*-
In Python2, if the program contains Chinese characters, you need to add this line. In Python3, the default encoding is Utf-8, and this line does not need to be added.
Statements that declare Python file encodings must be placed on the first or second line of the Py file, supported in 3 formats:
1)# coding=<encoding name>2)# -*- coding: <encoding name> -*-3)# vim: set fileencoding=<encoding name> :
Tip: The second way is usually used ~ ~
7. Comments in Python
In order to increase the readability of the code, it is common to add comments in the code, single-line comments with #, multi-line comments can be three pairs of double quotes "" "" ", the example is as follows:
class Abc(): """ 多行注释 多行注释 多行注释 """ def __init__(self): pass def say_hello(self): # 单行注释 # 单行注释 return ‘hello‘
?
Python Basics Overview