Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Which is better, Python2 or Python3?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "which is better to choose between Python2 and Python3". In daily operation, I believe many people have doubts about which good question to choose between Python2 and Python3. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "which is better to choose between Python2 and Python3?" Next, please follow the editor to study!

1. Print statement

Print in python 2 is a statement, which means that you can directly follow what you want to print, no matter what you want to output, directly after the print keyword (Note: if you want print to be used as a function in Python 2, you can import print_function in the future module)

Python 2

In python 3, print () is a function, which means that it can only be called with parentheses. Like other functions, print () requires what you want to output as an argument to it.

Python 3

2. Raw_input () and input () input functions

Python 2 has two global functions that are used on the command line to request user input. The first is called input (), which waits for the user to enter a python expression (and then returns the result); the second is called raw_input (), which returns the value entered by the user

Value = raw_input ("input values")

Use input () in python 3

Value = input ("input values")

3. Differences in using super ()

Python 2: must display write the base class in the parameter

Python 3: you can call without parameters instead of passing parameters to super ().

4. Encoding and string

Python 2: the default encoding ascii,unicode type represents a string sequence, and the str type represents a byte sequence

Python 3: default encoding utf-8,str type represents string sequence, byte type represents byte sequence (Note: because the source file uses utf-8 encoding by default, it is legal to name variables in Chinese, but it is not recommended)

5. True and False

Python 2:True and False are two global variables in Python 2, which can be assigned or other operations. The initial values are 1 and 0, respectively, which can be modified.

Python 3:True or False immutable

6. Range and xrange

In python 2, there is range: range ([start,] stop [, step]), which returns a list based on the range specified by start and stop and the step size set by step; the use of xrange and range is exactly the same, but a generator is returned

There is only range in python 3, and range in python 3 is equivalent to xrange in python 2. A generator is returned. To get a list using range (), you must explicitly call

List (range (10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

7. Package

Package must have an init method in python 2

There can be no init method in python 3

8. Iterator

Python 2: there are many methods that return lists, such as range (), dict.keys (), dict.values () methods of dictionary objects, map (), filter (), zip () etc... And the iterator must implement the next method

Python 3: change the method to return the list to return the iterator object, and have built-in next, so you don't have to implement the next method specifically

9. The result of division

In python 2, the result of division takes an integer (Note: if you want the result to be decimal, you need to import division in the future module)

Python 2

In python 3, the result of division / contains decimals, using / / only integers

Python 3

10. Long long integer

Python 2 has int and long types. And the maximum value of int type cannot exceed sys.maxint. You can define a long integer by appending an L to the end of the number, which is obviously larger than the range of numbers represented by the int type

Python 3 has only one integer type, int, which in most cases is similar to the long integer in python2

11. Comparison operator

Python 2 support is equivalent to! =

Python 3 only supports! =, no longer supports

twelve。 List parsing of tuples

In python 2, if you need to write a list parsing that traverses tuples, you don't need to put parentheses around the tuple values

Python 2: [i for i in 1, 10]

In python 3, parentheses are required

Python 3: [i for i in (1m 10)]

At this point, the study of "which is better between Python2 and Python3" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report