Compiled
Advantages: The compiler usually has a pre-compilation process to optimize the code. Because compilation is done only once, no compilation is required at runtime, so the execution of programs in compiled languages is efficient. Can run independently of the locale.
Disadvantages: If you need to modify after compilation, you need to recompile the entire module. When compiling, the machine code is generated according to the corresponding operating environment, and there will be problems in porting between different operating systems. Different executable files need to be compiled according to the operating system environment.
Interpretive
Advantages: good platform compatibility, can run in any environment, provided that the interpreter (virtual machine) is installed. Flexible, you can modify it directly when you modify the code, and you can quickly deploy it without maintenance.
Disadvantages: explain it every time you run it, and the performance is not as good as a compiled language.
Note: The Java code is said to be compiled and run everywhere, because the Java agent needs to be compiled into bytecode (an intermediate format of the code), and then the bytecode is run everywhere, and each time it needs to be handed over to the jvm to be compiled into a machine Instructions can only be executed after execution, so java is mixed
Some people have questions. Python code is first compiled into bytecode and then passed to the python interpreter for execution. Isn't python a hybrid? of course not
1. First of all, you need to know that the pyc bytecode file will only be generated when importing the py file. This bytecode file can be directly executed instead of the source file.
2. But each time the py file is executed, the bytecode generated will not be preserved, that is, each time the py file is executed, it will have to go through it again: py file-> byte code-> python interpreter -> Machine code, each time is a process of re-interpretation and implementation.
3.4 Why Choose Python
It ’s a fucking question, have you all watched TMD so much, and still TMD asks such questions?
C and Python, Java, C #, etc.
C language: The code is compiled into machine code, and the machine code is directly executed on the processor, and each instruction controls the work of the CPU
Other languages: The code is compiled into bytecode, the virtual machine executes the bytecode and converts it to machine code, and then executes it on the processor
CPython interpreter is developed by C language
使用 For use: Python's class library is complete and simple to use. If you want to achieve the same function, Python 10 lines of code can solve, C may need 100 lines or more.
For speed: Compared to C, Python is extremely slow.
Python and Java, C #, etc.
使用 For use: Linux original Python, other languages do not; the above languages have very rich class library support
For speed: Python may be slightly inferior in speed
1. Speed is never the key
For most applications, you don't need to run this fast, because users don't feel it at all. For example, to develop a network application that downloads MP3, the running time of the C program takes 0.001 seconds, and the running time of the Python program takes 0.1 seconds, which is 100 times slower. However, because the network is slower, you need to wait 1 second. Feel the difference between 1.001 seconds and 1.1 seconds? This is similar to the fact that F1 cars and ordinary taxis drive on the Beijing Third Ring Road. Although F1 cars theoretically reach a speed of 400 kilometers per hour, because the traffic jam on the Third Ring Road is only 20 kilometers per hour, so as a passenger, you feel the speed It's always 20 kilometers.
2.Encryption is not the key
For compiled languages such as C, you can send the compiled code to the user, and it is almost impossible for the user to reverse the solution, which is equivalent to the built-in encryption function. An interpreted language such as python can only publish source code to users, but in fact this is not a problem. The mainstream idea is that software is open source. Don't try to blame your software for its great commercial value.
3.5 Types of Python Interpreters
Python is a language. Code written in this language needs to be interpreted and executed by the Python interpreter. The interpreters are classified as follows:
Cpython (most commonly used version)
The official version of Python, implemented in C, is the most widely used. The CPython implementation converts source files (py files) into bytecode files (pyc files), and then runs on the Python virtual machine.
Jyhton
Java implementation of Python. Jython will dynamically compile Python code into Java bytecode and then run it on the JVM.
IronPython
C # implementation of Python. IronPython compiles Python code into C # bytecode and then runs it on the CLR. (Similar to Jython)
PyPy (special)
Python is implemented by Python. Python bytecode and bytecode are compiled into machine code.
RubyPython, Brython ...
Except for PyPy above, other Python correspondences and execution processes are as follows:
PyPy, based on Python, further processes Python's bytecode to improve execution speed!
3.6 History of the Python interpreter
In 1989, in order to pass the Christmas holiday, Guido started writing a compiler in Python. The name Python comes from Guido's beloved TV series Monty Python ’s Flying Circus. He hoped that this new language called Python would meet his ideals: to create a comprehensive, easy-to-learn, and extensible language between C and shell.
In 1991, the first Python compiler was born. It is implemented in C and can call C library files. From its birth, Python has: classes, functions, exception handling, core data types including tables and dictionaries, and module-based extension systems.
Granddaddy of Python web frameworks, Zope 1 was released in 1999
Python 1.0-January 1994 added lambda, map, filter and reduce.
Python 2.0-October 16, 2000, added memory reclamation mechanism, forming the basis of the current Python language framework
Python 2.4-November 30, 2004, the most popular web framework Django was born in the same year
Python 2.5-September 19, 2006
Python 2.6-October 1, 2008
Python 2.7-July 3, 2010
In November 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be no 2.8 release as users were expected to move to Python 3.4+ as soon as possible
Python 3.0-December 3, 2008
Python 3.1-June 27, 2009
Python 3.2-February 20, 2011
Python 3.3-September 29, 2012
Python 3.4-March 16, 2014
Python 3.5-September 13, 2015
Four python environment
Note: Setting of environment variables
windows:
1 2 3 4 5 6 7 1. Download the installation package https://www.python.org/downloads/ 2. Install the default installation path: C: \ python27 3. Configure the environment variables [right-click computer]-> [property] -> [Advanced System Settings]-> [Advanced]-> [Environment Variables]-> [Find a line with the variable name Path in the second content box, double-click]-> [Python installation directory and The Scripts directory under the installation directory is added to the variable value, and used; split] For example: the original value; C: \ python27, remember that there is a semicolon in front
linux:
1 2 3 No installation required, original Python environment ps: If it comes with 2.6, please update to 2.7
Update Python
windows:
1 Uninstall and reinstall
linux:
Linux's yum depends on its own Python. To prevent errors, the update here is to install another Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 View the default Python version python -V 1. Install gcc to compile Python source code yum install gcc 2. Download the source package, https: // www .python.org / ftp / python / 3. Unzip and enter the source file 4. Compile and install ./configure make all make install 5. View the version /usr/local/bin/python2.7 -V 6. Modify the default Python version mv / usr / bin / python /usr/bin/python2.6 ln -s /usr/local/bin/python2.7 / usr / bin / python 7. Prevent abnormal execution of yum, modify the version of Python used by yum vi / usr / bin / yum Modify the head #! / usr / bin / python to #! / usr / bin / python2.6 Five Python Primer 5.1 First Python Code
1. Interpreter: debug code immediately, code cannot be saved permanently
2. File: Save the code permanently
Create a new file hello.py in the D: \ python_test \ directory, and write the code as follows
The internal Python execution process is as follows:
When executing python D: \ python_test \ hello.py in the previous step, it was explicitly stated that the hello.py script was executed by the python interpreter.
On the Linux platform, if you want to execute a python script similar to a shell script, for example: ./hello.py, you need to specify the interpreter at the head of the hello.py file, as follows:
#! / usr / bin / env python #This line is only valid for linux
print (‘hello world’)
ps: hello.py needs to be executed before execution, chmod 755 hello.py
5.2 File header #! / Usr / bin / env python #-*-coding: utf-8-*-5.3 Comments
When the line looks: # commented content
Multi-line comments: "" "Annotated content" ""
5.4 Execute script to pass parameters
Python has a large number of modules, which makes developing Python programs very concise. The class library includes three:
Modules provided inside Python
Industry open source modules
Modules developed by programmers
Python provides a sys module internally, where sys.argv is used to capture the parameters passed in when executing a python script
5.5 Understanding pyc files
When executing Python code, if other .py files are imported, a .pyc file with the same name is automatically generated during execution. This file is the bytecode generated by the Python interpreter after compilation.
ps: The code can be compiled to generate bytecode; the bytecode can also be obtained by decompiling.
6 Variables 6.1 Why are there variables?
The essence of program execution is a series of state changes. The core of the two variables is change, and one is quantity. ‘Change’ corresponds to the change of the program, and ‘quantity’ is a measure that reflects a certain state.
For example, the initial level of a character in a game: level = 1, upgraded after a period of time: level = 10
6.2 Variable declaration and reference
#! / usr / bin / env python
name = ‘egon’ #Declaration of variable
name #Reference the value of a variable by its name
print (name) #Reference and print the value corresponding to the variable name name, ie ‘egon’
6.3 Identifier command specifications:
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 ',' from ',' global ',' if ',' import ',' in ',' is', 'lambda', 'not', 'or', 'pass',' print ' , 'raise', 'return', 'try', 'while', 'with', 'yield']
6.4 Assignment of variables
#! / usr / bin / env python
#The level of a game character has been increased from 1 to 2.
level = 1
level = 2
#The password of a game account was changed from '123' to '456'
passwd = ‘123’
passwd = ‘456’
#A person ’s name has ‘lhf’ changed to ‘egon’
name = ‘lhf’
name = ‘egon’
Seven input and output
Input is unified in python3, raw_input in python2 is equivalent to python3's input, and python2 also has input
1.res = input ("python3:")
2.res = raw_input ("python2:")
3.res = raw_input ("python2:")
No matter what input is received, 1,2 is stored as a string and assigned to res, while 3 means that whatever type the user enters, the type is assigned to res
#! / usr / bin / env python
name = input (‘Please enter a username:‘)
print (name)
carried out
C: \ Users \ Administrator> python D: \ python_test \ hello.py
Please enter your username: egon
egon
======================================================= ===============================
When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
#! / usr / bin / env python
import getpass
password = getpass.getpass (‘Please enter password:’)
print (password)
Execution (cannot be executed in pycharm, it needs to be executed in the terminal)
Note: ~ Example: ~ 5 = -6 Explanation: Multiply the binary number by +1 and multiply by -1, ie ~ x =-(x + 1),-(101 + 1) = -110
Bitwise reversal can only be used in front of numbers. So writing 3 + ~ 5 can get the result -3, and writing 3 ~ 5 is wrong.
5. Logical operations:
and annotation:
In Python, and and or perform Boolean logic calculations, as you would expect, but they do not return a Boolean value; instead, they return one of the values they actually compare.
Evaluates expression values from left to right in a Boolean context. If all values in the Boolean context are true, then and returns the last value.
If a value in the Boolean context is false, and returns the first false value
or annotation:
When using or, values are evaluated from left to right in a Boolean context, just like and. If a value is true, or returns the value immediately
If all values are false, or returns the last false value
Note that or evaluates expressions in a Boolean context until the first true value is found, then the remaining comparison values are ignored
and-or combined:
Combining the two previous grammars, just inference.
To improve program readability, it is best to use parentheses, for example:
(1 and ‘x’) or ‘y’
6. Member operations:
7. Identity calculation
8. Operator priority: top to bottom, priority from high to low
Nine flow control 9.1 conditional statements
1 single branch
2 Multi-branch
Requirement 1. User login verification
#! / usr / bin / env python
name = input (‘Please enter a username:‘)
password = input (‘Please enter your password:’)
if name == ‘egon’ and password == ‘123’:
print (‘egon login success’)
else:
print (‘Wrong username or password’)
Requirement two: output its permissions based on user input
#! / usr / bin / env python
#Print its permissions based on user input
‘‘ ‘
egon-> super administrator
tom-> general administrator
jack, rain-> business executive
Other-> General user
‘‘ ‘
name = input (‘Please enter a username:‘)
if name == ‘egon’:
print (‘Super Admin’)
elif name == ‘tom’:
print (‘general administrator’)
elif name == ‘jack’ or name == ‘rain’:
print (‘Business Supervisor’)
else:
print (‘common user’)
9.2 Loop statements
while loop
1. Basic cycle
while conditions:
# Loop body
# If the condition is true, the loop body is executed
# If the condition is false, then the loop body is not executed
Break
break is used to exit the loop of this layer
while True:
print "123"
break
print "456"
3.continue
continue is used to exit this cycle and continue the next cycle
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.