[Python] -- first understanding python, first understanding python
Python InstallationWindows:
1. Download the installation package https://www.python.org/downloads/2?install the default installation path: C: \ python273. Configure the environment variable [Right-click the computer] -- "[attribute] --" [advanced system settings] -- "[advanced] --" [environment variable] -- "[in the second content find a line with the variable name Path in the box, double-click] --> [append the Python installation directory to the variable value, and separate it with;.] example: the original value; C: \ python27; remember that there is a semicolon before it.
Linux and Mac:
1 No installation required, original Python environment 2 3 ps: if it comes with 2.6, update to 2.7
Update python:
1 windows: 2 3 1. Uninstall and reinstall. 4 5 linux: 6 7 Linux yum dependency comes with Python. To prevent errors, the update here is actually to install another Python 8 9 to view the default Python version 10 python-V11 12 1. install gcc for compiling Python source code 13 yum install gcc14 2. Download the source code package, https://www.python.org/ftp/python/15 3, extract and enter the source code file 16 4, compile and install 17. /configure18 make all19 make install20 5. View version 21/usr/local/bin/python2.7-V22 6. Modify the default Python version 23 mv/usr/bin/python/usr/bin/ python2.624 ln-s/usr/local/bin/python2.7/usr/B In/python25 7. Prevent yum execution exceptions. Modify the Python version 26 vi/usr/bin/yum27 of yum to set the header #! /Usr/bin/python #! /Usr/bin/python2.6
View Code
Getting started with python
I. The first Python code:
Create the hello. py file in the/home/dev/directory. The content is as follows:
1 print("hello,world")
Run the hello. py file:python /home/dev/hello.py
The internal execution process of python is as follows:
Ii. Interpreter:
When running python/home/dev/hello. py in the previous step, it is clearly indicated that the hello. py script is executed by the python interpreter.
If you want to execute a python script like a shell script, for example:./hello.py
In the header of the hello. py file, specify the interpreter as follows:
1 #!/usr/bin/env python2 3 print("hello,world")
In this way, run :./hello.py
You can.
Ps: You must grant the "hello. py" execution permission before execution. chmod 755 hello. py
Iii. Content Encoding:When the python interpreter loads the code in the. py file, it will encode the content (ascill by default)
ASCII (American Standard Code for Information Interchange) is a computer coding system based on Latin letters. It is mainly used to display modern English and other Western European languages, it can only be expressed in 8 bits (one byte) at most, that is, 2 ** 8 = 256. Therefore, the ASCII code can only represent 256 symbols at most.
Obviously, ASCII codes cannot represent all types of characters and symbols in the world. Therefore, you need to create a new encoding that can represent all characters and symbols, that is, Unicode.
Unicode (unified code, universal code, Single Code) is a character encoding used on a computer. Unicode is generated to address the limitations of traditional character encoding schemes. It sets a uniform and unique binary encoding for each character in each language, it is specified that some characters and symbols are represented by at least 16 bits (2 bytes), that is, 2*16 = 65536,
Note: here we will talk about at least 2 bytes, maybe more
UTF-8 is the compression and optimization of Unicode encoding, which no longer uses at least 2 bytes, but classifies all characters and symbols: the content in the ascii code is saved in 1 byte, the European characters are saved in 2 bytes, and the East Asian characters are saved in 3 bytes...
Therefore, when the python interpreter loads the code in the. py file, it will encode the content (ascill by default)
Iv. Notes:
When watching: # commented content
Multi-line comment: "commented content """
V. pyc file:
When you run the Python code. py file, a file with the same name is automatically generated during execution. pyc file, which is the bytecode generated after the Python interpreter is compiled.
Ps: After the code is compiled, it can generate bytecode. After the bytecode is decompiled, the code can also be obtained.
Vi. variables:
1. Declare Variables
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 name = "wupeiqi"
The code above declares a variable named: name. The value of the variable name is: "wupeiqi"
The role of a variable: nickname, which represents the content stored in an address in the memory.
Rule for variable definition:
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of the variable name cannot be a number.
- The following keywords cannot be declared as variable names
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
2. Variable assignment:
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 name1 = "wupeiqi"5 name2 = "alex"