Example to explain the main differences between Python2 and Python3


Every programming language changes significantly between major versions after the release of an update. In this article, Vinodh Kumar explains some of the major differences between Python 2 and Python 3 with examples to help illustrate the changes in the language.

This tutorial focuses on the following.

  • expression (math.)
  • Print Options
  • Unequal operation
  • Range
  • automatic migration
  • Performance issues
  • Major internal service changes

1. Expressions

(located) at Python 2 in order to obtain the calculation expression (math.), You would type:

X=raw_input("entersomevalues)

But in Python 3, you would type.

X=input("entersomevalues")

Thus, whatever we enter, the values are assigned to the variables x in 2 and 3. When you type 2*6 in Python 2, the result will be 12, which is the evaluation value.

However, when the same program is run in Python 3, the result is a string value. In this case, it looks like 2*6 in string format.

or so, How we get assessments expression (math.) this (Cantonese)? present ., We have to use a file called eval of expression (math.) or function。 When you write before you enter eval time, It will bring expression (math.) Convert to calculated value。

x=eval(input("entersomevalues"))=12

Examples of specific expressions.

In Python 2.

name=input("Whatisyourname?")print("Hello,%s."%name)

Then output.

In Python 3.

name=input("Whatisyourname?")print("Hello,%s."%name)

Then output.

We can clearly see that there is very little difference between them.

2、Print option

In Python 2, print is a statement that does not require parentheses. In Python 3, print is a function and the value needs to be enclosed in parentheses.

Python 2

Input.

print"helloworld"

Will output.

Python 3

Input.

1!=1.0print(False)

Will output.

3、 Unequal operation

When we are in the Python 2 use in Unequal operator time, We need to use a value greater than > or less than < symbolic。 nevertheless,(located) at Python 3 in, There is a generic operator。 exclamation mark ! (punct.) ! and equal sign = Used to indicate whether values are equal。

Python 2 -<> Operators indicate inequality Python 3 -! Operators indicate inequality

Python 2

Input.

1<>1.0print"False"

Will output.

Python 3

Input.

1!=1.0print(False)1!=1.0print(False)

4、Range

Range is used to generate a list of numbers, usually for iterating through a for loop.

Here you can see that X is equal to Range 10. When we check the variable X, it returns the list type. This means that in Python 2, Range is the type of a list. After I write X, I get a list of objects, here: 0 1 2 3 4 5 6 7 8 9.

Now let's move to Python 3, where when we write X equals Range 5, this value is assigned to the variable X; when we check the type of the variable X, it returns a Range object itself. This means that in Python 3, a Range is a range object itself.

Python 2

Input.

printrange(0,10,1)

Will output.

Python 3

Input.

print(list(range(10)))

Will output.

5、 automatic migration

So, how do we automate the execution of scripts to move code from Python 2 to 3?

Here, we can use Add 2 Numbers in Python for testing.

Python 2

Input.

n1=1n2=2add=float(n1)+float(n2)print'sumof{0}and{1}is{2}'.format(n1,n2,add)

Will output.

Now using the 2to3 migration, we can convert the code above.

Input.

n1=1n2=2add=float(n1)+float(n2)print('sumof{0}and{1}is{2}'.format(n1,n2,add))

Will output.

So here we see that it can be converted to Python 3 code on the command line via 2to3.

Python provides its own tool called 2to3.py, which runs a bunch of scripts to convert your Python 2 code to 3. It's not perfect, but it does a great job overall. After converting any code, you can fix any issues manually.

6. Performance issues

Most performance issues have been fixed in this update! When comparing the benchmarks between the two versions, the differences are almost negligible.

7. Some major internal service changes

Python 2

  • The print function brackets are optional.
  • Use u as a prefix string to generate unicode strings.
  • Integer division always returns the integer -5/2=2.
  • Raw_input() Reads a string.
  • input() Evaluates the read data.
  • generator .next()。

Python 3:

  • The print function brackets are required.
  • The default is the string unicode.
  • Integer division may result in a float of -5/2=2.5.
  • Raw_input() is not available.
  • The input always reads the string.
  • Next (generator)。
  • Py2 to py3 utilities.
  • Dictionary.keys() harmony .values() Returns a view, not a list。
  • The comparison operator can no longer be used in unnatural comparisons. for example,None < None would trigger TypeError Instead of returning false。
  • The percentage (%) string formatting operator is not recommended; use the .format() function or concatenation.

Compiled from: Key differences between Python 2 and 3: How to navigate change


Recommended>>
1、Robots make Grubs new fine dining restaurant
2、The first thing you need to do is to get a good idea of what youre getting into
3、What does this big surge in EOS which is approaching the main online network say
4、Subroutine calling mindfulness in manual programming for traditional hierarchical machining
5、A roundup of the best papers on deep learning 2017

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号