Objective
Keep learning attitude, learning a dynamic language is actually a long time to be ready to do things, was still tangled in Python and Ruby. Now not just to learn python, but also to think about what to do with it, these follow-up, because look at the python2.x books. Python 3.7 is used. So let's start by documenting the difference between the two, which is limited to the basics.
The difference between python3.x and 2.x 1.print
The print statement is gone and replaced by the print () function.
PY 2.x:
print "wyl" #statement
print ("wyl")
py 3.x:
>> print ("wyl")
wyl
>>> print ‘wyl’
SyntaxError: Missing parentheses in call to ‘print‘. Did you mean print (‘wyl‘)?
2.Unicode
Python 2 has ASCII str () type, unicode () is separate, not byte type.
Now, in Python 3, we finally have Unicode (utf-8) strings, and a byte class: byte and bytearrays.
Since Python3.X source files use utf-8 encoding by default, this makes the following code legal:
py 2.x
>>> str = ‘Wang Yanling is so handsome’
>>> str
‘Xe6 \ x88 \ x91 \ xe7 \ x88 \ xb1 \ xe5 \ x8c \ x97 \ xe4 \ xba \ xac \ xe5 \ xa4 \ xa9 \ xe5 \ xae \ x89 \ xe9 \ x97 \ xa8’
py 3.x
>>> str = ‘Wang Yanling is so handsome’
>>> str
"Wang Yanling is so handsome"
>>>
3. Division
The division in Python has two operators, / and //
First of all / division:
In python 2.x / division is similar to most languages we are familiar with, such as Java and C #, rounded up, and the decimal part is completely ignored.
In Python 3.x / division is no longer done, for division between integers, the result will also be a floating point number.
py 2.x
>>> 1/2
0
>>>
py 3.x
>>> 1/2
0.5
>>>
The second kind of // division:
This division is called floor division, and py2.x is the same as py3.x
py2.x py3.x
>>> 1 // 2
0
>>>
4. Abnormal
In py3.x we now use as as a keyword instead of ‘,’
So all exceptions inherited from BaseException and removed StardardError
Removed sequence behavior and .message properties of exception classes
Use raise Exception (args) instead of raise Exception, args syntax
Syntax changes to catch exceptions, as keyword is introduced to identify exception instances
py3.0:
>>> try:
1/0
except Exception, err:
print err
SyntaxError: invalid syntax
>>>
#Correct writing
>>> try:
1/0
except Exception as err:
print (err)
division by zero
>>>
5.xrange
xrange () was renamed to range (). To get a list using range (), you must call it explicitly:
py 2.x
>>> range (1,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
py3.x
>>> range (10)
range (0, 10)
>>>
py3.x If you want to use range (), you can combine list
>>> list (range (10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
6. Octal
Python octal can not start with 0 but start with 0o
py 3.x
>>> 0101
SyntaxError: invalid token
>>> 0o101
65
>>>
7. Inequality operator
Unequal operation py3.x only "! =", "<>" Has been removed
py3.x
>>> 1! = 3
True
>>> 1 <> 3
SyntaxError: invalid syntax
>>>
8. Removed the repr expression ''
py3.x
>>> s = `wyl`
SyntaxError: invalid syntax
>>> s = ‘wyl’
>>> repr (s)
"‘ Wyl ’"
9. Multiple modules were renamed (according to PEP8)
Old name new name
_winreg winreg
ConfigParser configparser
copy_reg copyreg
Queue queue
SocketServer socketserver
repr reprlib
The StringIO module is now incorporated into the new io module. New, md5, gopherlib and other modules were deleted.
Python 2.6 already supports the new io module.
httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib are merged into http package.
The exec statement was canceled, leaving only the exec () function. Python 2.6 already supports the exec () function.
10. Data type
py 3.x
1> Long is removed
2> The bytes type is added, which corresponds to the octet string of version 2.X
3> The dict.keys (), .items and .values () methods return iterators, and the previous iterkeys () and other functions are all discarded. Also removed is dict.has_key (), replace it with in.
11. Object-oriented
1> Introduction of abstract base classes (Abstraact Base Classes, ABCs)
2> The container class and iterator class are ABCs, so the types in cellections module are much more than Py2.x
>>> import collections
>>> print (‘\ n‘.join (dir (collections)))
AsyncGenerator
AsyncIterable
AsyncIterator
Awaitable
ByteString
Callable
ChainMap
Collection
Container
Coroutine
Counter
Generator
Hashable
ItemsView
Iterable
Iterator
KeysView
Mapping
MappingView
MutableMapping
MutableSequence
MutableSet
OrderedDict
Reversible
Sequence
Set
Sized
UserDict
UserList
UserString
ValuesView
_Link
_OrderedDictItemsView
_OrderedDictKeysView
_OrderedDictValuesView
__all__
__builtins__
__cached__
__doc__
__file__
__loader__
__name__
__package__
__path__
__spec__
_chain
_collections_abc
_count_elements
_eq
_heapq
_iskeyword
_itemgetter
_nt_itemgetters
_proxy
_recursive_repr
_repeat
_starmap
_sys
abc
defaultdict
deque
namedtuple
>>>
View Code
3> The iterator's next () method is renamed to __next __ (), and the built-in function next () is added to call the iterator's __next __ () method
4> Two decorators, @abstractmethod and @abstractproperty, are added to make it easier to write abstract methods (properties).
12. Module
1> The cPickle module has been removed, and the pickle module can be used instead. Eventually we will have a transparent and efficient module.
2> removed imageop module
3> Removed audiodev, Bastion, bsddb185, exceptions, linuxaudiodev, md5, MimeWriter, mimify, popen2, rexec, sets, sha, stringold, strop, sunaudiodev, timing and xmllib modules
4> Removed bsddb module (posted separately, available from http://www.jcea.es/programacion/pybsddb.htm
5> Removed the new module
6> The os.tmpnam () and os.tmpfile () functions were moved to the tmpfile module
7> The tokenize module now works with bytes. The main entry point is no longer generate_tokens, but tokenize.tokenize ()
The difference between Python 3.x and 2.x