1. Introduction to Python
Python is a programming language written by the famous "Uncle Turtle" Guido van Rossum during Christmas 1989 to send a boring Christmas.
Advantages of Python
1, python positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs. The development efficiency is very high,
2, Python has a very powerful third-party library, basically you want to realize any function through the computer, the Python official library has the corresponding module to support, directly download the call, on the basis of the base library to develop again, greatly reduce the development cycle, avoid repeating the wheel
3, high-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
4. Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all of your Python programs can run on almost any system platform on the market without modification.
5, Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
6, the embeddable ———— you can embed python in your C + + program to provide scripting capabilities to your program users.
python drawbacks
1, running slowly, and the C program is very slow, because Python is an interpreted language, your code in the execution of a line of translation into the CPU can understand the machine code, the translation process is very time-consuming, so very slow. The C program is a machine code that compiles directly to the CPU before it runs, so it's very fast.
2, the code can not be encrypted. If you want to publish your Python program, actually publish the source code, which is different from the C language, C language does not have to publish the source code, only need to post the compiled machine code (that is, you are common on Windows Xxx.exe file) published. It is impossible to eject C code from the machine code, so there is no such problem in any compiled language, and the language of interpretation must be published.
Install Python
Linux systems:
无需安装,原装Python环境,If you bring your own 2.6, please update to 2.7 or 3.5, according to your own requirements, Ubuntu Python update fast, CentOS on the version of Python older
Windows system:
1、下载安装包
https:
//www
.python.org
/downloads/
2、安装
安装路径:自己定义,默认C盘
3、配置环境变量
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
如:原来的值;C:\python3,前面有分号出现如表明安装成功:
Python interpreter
When we write the Python code, we get a text file that contains the Python code for the .py extension. To run the code, you need the Python interpreter to execute the .py file, which is more common:
- The official version of Cpython Python, implemented in C, is the most widely used, and the Cpython implementation converts the source file (py file) into a bytecode file (PYc file) and then runs on the Python virtual machine.
- Jyhton Python's Java implementation, Jython will dynamically compile Python code into Java bytecode and run it on the JVM.
- IronPython Python's C # implementation, IronPython compiles Python code into C # bytecode and then runs on the CLR. (similar to Jython)
- Python, PyPy (special) Python implementation, compiles Python bytecode bytecode into machine code.
- Rubypython, Brython ...
Getting started with Python
The first Python program
Create a file under Linux called hello.py, and enter
>>> print ‘hello, world‘hello, world
By default, Python 3 source files are encoded in UTF-8 , and all strings are Unicode strings. Of course you can also specify a different encoding for the source file:
#-*-coding:cp-1252-*-
Python2 does not support Chinese, to display Chinese you must first define a character set:
# -*-coding:utf-8-*-
Line and indent
Python's most distinctive feature is the use of indentation to represent blocks of code, without the need for curly braces ({}).
The number of spaces to indent is variable, but the statement of the same code block must contain the same number of indentation spaces. Examples are as follows:
Username = input ("Username:") Password= Input ("Password:")ifUsername = ="Zyj" andPassword = ="123456": Print("welcome!")Else: Print("wrong username or password")
Otherwise you will be prompted: Indentationerror: (indentation error), the same level of code must remain consistent!!!
Multi-line statements
Python is usually a line to complete a statement, but if the statement is long, we can use a backslash (\) to implement a multiline statement, for example:
Total = Item_one + + item_three
In [], {}, or () a multiline statement, you do not need to use a backslash (\), for example:
Total = ['item_one'item_two'item_three ' , ' Item_four ' ' item_five ']
Variable
" Zyj " = nameprint"yzj"print(name,name2)
The above code first declares a variable name, a value of Zyj, and a variable value of name
Variable definition rules
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of a variable name cannot be a number
- The following keywords cannot be declared as variable names
- [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
Python3 Interpreter
On Linux/unix systems, the general default Python version is 2.x, and we can install python3.x in the/usr/local/python3 directory.
Once the installation is complete, we can add the path/usr/local/python3/bin to the environment variables of your Linux/unix operating system so that you can start Python3 by entering the following command through the shell terminal.
$ path= $PATH:/usr/local/python3/bin/python3 # Set environment variable $ python3--3.5.2
Content encoding
The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)
ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.
It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode
Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536, Note: Here is a minimum of 2 bytes, possibly more
UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...
Therefore, when the Python interpreter loads the code in the. py file, it encodes the content (the default ascill), if it is the following code:
Error: ASCII code cannot be expressed in Chinese
# !/usr/bin/env python Print " Hello, World
Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:
# !/usr/bin/env python # -*-coding:utf-8-*- Print " Hello, World "
Summarize:
Python2 does not support Chinese, to display Chinese you must first define a character set:
# -*-coding:utf-8-*-
Python3 's Notes
Single-line comment preceded by #, multiline comment plus three double quotation marks or three single quotes, Python in double quotes, single double quotation marks for string lines (used on the same line) are used incorrectly:
" My name is Zyj, I am years old,i like girl"
If you want to write this, you can use the following notation:
""" My name is Zyji am-years oldi like girl """ Print (Intro)
Explanation: three single or double quotes can annotate multiple lines, and can also represent a paragraph.
Summarize:
Single-line Comment: # commented content
Multiline Comment: "" "is commented content" "or" "commented content" "" "" Multiline Comment "" that can represent a multiline comment, or can represent a paragraph
Python first day