Catalog
Chapter one :Python introduction
1.Python Introduce
1.1 brief introduction
1.2 Python2. Version and Python3.x Differences in versions
1.3 Python Advantages and disadvantages
1.4 Basic program format
1.5 Python Application scenarios of
Python It's a kind of interpreted , object-oriented Language . By Guido · Van rosum (Guido van Rossum) On 1989 The invention of ,1991 Officially announced in . Official website :www.python.org
Python The word is “ boa constrictor ” It means . But Uncle tortoise didn't get the name because he liked boa constrictor, but he was chasing a play : British TV comedy 《 Monti · Parson's Flying Circus 》
Programming languages are classified from the following aspects : Compiler and interpretive 、 Static language and dynamic language 、 Strong type definition language and weak type definition language .
What's the difference between compilation and interpretation ?
compiler It is to compile every statement of the source program into machine language 、 And save as binary file , In this way, the runtime computer can directly execute this program in machine language , fast ;
Interpreter When executing a program , One by one, it's interpreted as machine language for the computer to execute , So the running speed is not as fast as the compiled program .
This is because computers can't directly recognize and execute the statements we write , It can only understand machine language ( It's in binary form )
A compiled Explanatory mixed type CJavaScriptJavaC++PythonC#GoRuby SwiftPHP Object-CPerl PascalErlangA compiled
advantage : Compilers usually have precompiled procedures to optimize the code .
Explanatory
advantage : Good platform compatibility , Run in any environment , If the interpreter is installed ( virtual machine ), Flexible code modification
Python The inheritance of is multi - inheritance
Python2 Medium print It's a statement , A statement is an executable code . If the return value is also multiple , Then return a tuple .
Python3 Medium print For a function , It must be enclosed in brackets ; The function receives a return value from a parameter , Multiple parameters can be received .
#Python2
1 print 'Python', python_version()
2 print 'Hello, World!'
3 print('Hello, World!')
4 print "text", ; print 'print more text on the same line'
```
run result:
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line
```
#Python3
1 print('Python', python_version())
2 print('Hello, World!')
3 print("some text,", end="")
4 print(' print more text on the same line')
```
run result:
Python 3.4.1
Hello, World!
some text, print more text on the same line
```
Python 2 in , Integer division defaults to integer .Python 3 in , Integer division defaults to floating point .
Python 2:
>>>print("1/2",1/2)
('1/2',0) # A tuple is returned
Python 3:
>>>print("1/2",1/2)
1/2 0.5
Python 2 in , The default encoding is asscii ,asscii Coding is a coding system applied to Latin alphabets , Using Chinese in programs often leads to coding problems . stay Python 3 Have adopted the UTF-8 As default encoding ,UTF-8 Belong to Unicode code , Support for most languages , And variable length , Space saving . stay Python 3 No need to declare when writing code in # conding:utf-8
Python 2 The type of Chinese characters :
str : The sequence of bytes that have been encoded
Unicode : Text characters before encoding
Python 3 The type of Chinese characters :
str : Code of Unicode Text character
bytes : Sequence of bytes before encoding
Binary bytes Unicode character Python 2 str type Unicode type Python 3bytes type str typeIn order to avoid mistakes , stay Python 2 Should precede the text string with u.
Python 2 in range Return a list , xrange Returns an iterator .
Python 3 There is no xrange .range The method is equivalent to Python 2 Medium xrange Method , At the same time like map Functions, etc ,Python 3 Instead of returning a list, the function returns an iterator .
Python3 in input What you get is str;Python2 Of input What you get is int type ,Python2 Of raw_input What you get is str type ; Unify :Python3 In Chinese input and Python2 In Chinese row_input, All entered as str.
Fortunately, , stay Python 3 Storage of user input as a str Object problem . In order to avoid Python 2 Dangerous behavior of reading non string type in , We have to apply raw_input() Instead of .
python2 Medium StringIO and cStringIO Merge into python3 Medium io
python2 Medium pickle and cPickle Merge into python3 Medium pickle.
python2 Medium urllib、urllib2 and urlparse Merge into python3 Medium urllib
python2 Medium dict Class keys、values and items All return to list object ,iterkeys、itervalues and iteritems Returns the generator object .
python3 Removed from list、 Return only one generator object , Keep view only ( generator ), But the method name is :keys、values and items.
python2 The default class in is the legacy class , Need to explicitly inherit new classes (object) To create new classes .
python3 Completely remove legacy classes from , All classes are new , But you can still explicitly inherit object class .
python2 Import order of packages in : Standard library — Relative pouring ( The current directory )— Absolutely import (sys.path)
python3 Import order of packages in : Standard library — Absolutely import ( If you want to import relatively , Use from .moudel)
Python The design philosophy of “ grace ”、“ clear ”、“ Simple ”.
advantage
shortcoming :
1 . Appropriate spaces , Indentation problem
(1) The first line of logic is blank ( Spaces and tabs ) Used to determine the indentation level of logical lines , This is used to determine the grouping of statements
(2) The statement starts with the first column of the new row .
(3) The indentation style is uniform :
① Each indent level uses a single tab or four spaces (IDE The tab is automatically set to 4 A space );
②Python Indent instead of {} Represents a program block .
2 . Python Case sensitive
3 . notes
(1) Line notes
Add... Before each line of comment “#” Number . When the interpreter sees “#” , Ignore this line “#” Later
(2) Paragraph notes
Use three consecutive single quotes (```). When the interpreter sees (```), Will scan to the next (```), Then ignore the content between them .