In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are the main differences between Python2 and Python3". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what are the main differences between Python2 and Python3".
Some major differences between Python2 and Python3.
The most frequently used statement when debugging a program is probably print. In Python2, print is a statement, while in Python3 it exists as a function. Some people may have doubts, but I clearly saw in Python2 that when a function is used:
# py2
Print ("hello") # equivalent print ("hello")
# py3
Print ("hello")
However, what you see is only the appearance. What is the difference between the above two expressions? The output is the same, but in essence, the former treats ("hello") as a whole, while the latter print () is a function that takes a string as an argument.
# py2
> print ("hello", "world")
('hello',' world')
# py3
> print ("hello", "world")
Hello world
This example is even more obvious, in py2, the print statement is followed by a tuple object, while in py3, the print function can accept multiple positional parameters. If you want to use print as a function in py2, you can import print_function in the future module
# py2
> print ("hello", "world")
('hello',' world')
> > >
> > from _ _ future__ import print_function
> print ("hello", "world")
Hello world coding
The default encoding of Python2 is asscii, which is one of the reasons why coding problems are often encountered in Python2. As for why asscii is used as the default encoding, the reason is that there is no Unicode when Python2 comes out. Python 3 defaults to UTF-8 as the default encoding, so you no longer need to write # coding=utf-8 at the top of the file.
# py2
> > sys.getdefaultencoding ()
'ascii'
# py3
> > sys.getdefaultencoding ()
'utf-8'
Many articles on the Internet say that the coding problem of Python2 can be solved by changing the default encoding format. In fact, this is a big hole, so don't do it.
String
Strings are one of the biggest changes, which minimizes coding problems. In Python2, there are two types of strings, one is unicode, the other is str, the former represents a text string and the latter represents a byte sequence, but there is no obvious boundary between the two, and the developer also feels confused and does not understand the cause of the coding error, but a strict distinction is made between the two in Python3, using str to represent the string and byte to represent the byte sequence Any data that needs to be written to text or transmitted over the network only receives a sequence of bytes, which prevents the problem of coding errors from the source.
True and False
True and False are two global variables (names) in Python2, which correspond to 1 and 0 respectively. Since they are variables, they can point to other objects, such as:
# py2
> True = False
> True
False
> True is False
True
> False = "x"
> False
'x'
> if False:
... Print (?
...
?
Obviously, the above code violates Python's design philosophy Explicit is better than implicit. Python3 fixes this flaw so that True and False become two keywords that always point to two fixed objects and are not allowed to be re-assigned.
# py3
> True = 1
File "", line 1
SyntaxError: can't assign to keyword iterator
Many of the built-in functions and methods that return list objects in Python2 are changed to return iterator-like objects in Python 3, because the lazy loading nature of iterators makes manipulating big data more efficient. The range and xrange functions in Python2 are merged into range, and if both 2 and 3 are compatible, you can do this:
Try:
Range = xrange
Except:
Pass
In addition, the dictionary object's dict.keys () and dict.values () methods no longer return a list, but as an iterator-like "view" object. Higher-order functions map, filter, and zip no longer return list objects. Yes, py2's iterator must implement the next method, and py3 has been changed to _ _ next__
Nolocal
We all know that in py2, you can declare a variable as a global variable with the keyword global in the function, but in nested functions, it is impossible to declare a variable as a non-local variable. Py3 adds the keyword nolcoal, which makes non-local variables possible.
Def func (): C = 1 def foo (): C = 12 foo () print (c) func () # 1
You can compare the output of the above two pieces of code
Def func (): C = 1 def foo (): nonlocal c c = 12 foo () print (count) func () # 12
In fact, many built-in modules have also made a lot of adjustments, the module organization in Python3 is clearer, the class is more advanced, and asynchronous IO is introduced. Write so much this time and continue next time.
The above is all the contents of the article "what are the main differences between Python2 and Python3". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.