Python3 or Python2? Examples explain the differences between the two

mob604756fb8908 2021-07-20 04:34:54
python3 python python2 python examples


original text :Key differences between Python 2 and 3: How to navigate change

compile : Open source in China (oschina2013)

translator :OSC- Darwin

If you need to reprint, please indicate the above information in the text

After each programming language releases an update , Big changes happen between major versions . In this paper ,Vinodh Kumar This is explained by an example Python 2 and Python 3 Some of the big differences between , To help explain the changes in language .

This tutorial is mainly about :


expression


Print Options


Unequal operation


Range


Automatic migration


Performance issues


Major internal transaction changes

1、 expression

stay Python 2 In order to get the evaluation expression , You can type it :

But in Python 3 in , You can type it :

therefore , Whatever we type in , Values are assigned to 2 and 3 The variables in the x. When in Python 2 Input in 2*6 when , The result will be 12, This is the evaluation value .

however , When in Python 3 When running the same program in , The result is a string value . under these circumstances , It looks like a string format 2*6.

that , How do we get the evaluation expression ? Now? , We have to use one called eval An expression or function of . When you write before typing eval when , It converts the expression to a calculated value .

Specific expression examples :

stay Python 2 in :

Then the output :

stay Python 3 in :

Then the output :

We can see clearly that , The difference between them is very small .

2、Print Options

stay Python 2 in ,print It's a statement that doesn't need parentheses . stay Python 3 in ,print It's a function , Values need to be enclosed in brackets .

Python 2

Input :

Will be output :

Python 3

Input :

Will be output :

3、Unequal operation

When we're in Python 2 Use in Unequal Operator , We need to use more than > Or less than < Symbol . however , stay Python 3 in , There is a general operator . Exclamatory mark ! And the equal sign = Used to indicate whether values are equal .

Python 2 -<> The operator represents inequality
Python 3 -! The operator represents inequality

Python 2

Input :

Will be output :

Python 3

Input :

Will be output :

4、Range

Range Used to generate a list of numbers , Usually used for iterations for loop .

Here you can see X be equal to Range 10. When we look at variables X when , It returns the list type . This means that Python 2 in ,Range Is the type of list . When I write X after , Get a list of objects , Here is :0 1 2 3 4 5 6 7 8 9.

Now let's turn to Python 3, When we write X be equal to Range 5, This value is assigned to the variable X; When we look at variables X The type of , It will return one Range Object itself . This means that Python 3 in ,Range It's a scope object itself .

Python 2

Input :

Will be output :

Python 3

Input :

Will be output :

5、 Automatic migration

that , How do we automate scripts to take code from Python 2 Move to 3?

ad locum , We can use Python Medium Add 2 Numbers To test .

**
Python 2**

Input :

Will be output :

So here we see that it can be passed on the command line 2to3 Convert to Python 3 Code .

Python Provided their own tools , be known as 2to3.py, It runs a bunch of scripts to put your Python 2 Code conversion to 3. Although it's not perfect , But on the whole it's done very well . After converting any code , You can fix any problem manually .

6、 Performance issues

Most performance issues have been fixed in this upgrade ! When comparing benchmarks between two versions , The difference is almost negligible .

**
7、 Some major internal changes **

Python 2


print Function brackets are optional .


Use u As a prefix string to generate unicode character string .


Integer division always returns an integer -5/2=2.


Raw_input() Read string .


input() Evaluate the data read .


generator .next().

Python 3:


print Function brackets are required .


The default is string unicode.


Integer division can lead to floating -5/2=2.5.


Raw_input() Unavailable .


Input always reads string .


Next (generator).


Py2 to py3 Utilities .


Dictionary .keys() and .values() The view returned is not a list .


You can no longer use comparison operators in unnatural comparisons .
for example ,None < None Will lead to TypeError Instead of going back false.


Percentage is not recommended (%) String formatting operators , Use .format() Function or connection .

版权声明
本文为[mob604756fb8908]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/05/20210530163035341e.html

  1. 【推荐】常见的Python数据可视化库
  2. 基于Python3.3+Selenium3.0框架实战Web自动化测试实战(百度网站实测项目)
  3. 【django轻量级框架】用Mysql的各种项目响应速度慢?一招解决!
  4. 【django轻量级框架】Django项目导入css,js,images等静态文件
  5. 【django轻量级框架】HTML上传文件拦截到本地
  6. 【深度学习入门到精通系列】留一交叉验证法Python实现(看不懂你来打我~!)
  7. Python机器学习笔记:sklearn库的学习
  8. python【Opencv计算机视觉库】opencv模块cv2常用函数用法(全)
  9. python基础练习(七)
  10. python基础练习(八)
  11. python基础练习(九)
  12. python基础练习(十)
  13. python新增练习(一)
  14. python新增练习(二)
  15. python新增练习(三)
  16. Python中的排序sorted(d.items(), key=lambda x: x[1])
  17. python 判断字母大小写
  18. python绘制科赫雪花(递归)
  19. 解决cmd命令查看python版本“python不是内部命令或外部命令,也不是可执行程序解决方案”的问题
  20. Python Numpy介绍
  21. python —pandas库常用函数
  22. Python应用matplotlib绘图简介
  23. Python matplotlib高级绘图详解
  24. 入门训练 Fibonacci数列-python实现
  25. Python -二维数组定义
  26. python二进制相加
  27. Python文本处理:解析json格式的数据
  28. 查看python安装路径
  29. Python编程之计算生态
  30. Python-turtle标准库知识小结(python绘图工具)
  31. Python-time标准库知识小结
  32. Python-random标准库知识小结
  33. python安装第三方库的三种方法
  34. python程序的控制结构
  35. Python程序的函数和代码复用
  36. python之组合数据类型
  37. python【力扣LeetCode算法题库】300 最长上升子序列(动态规划)
  38. python【力扣LeetCode算法题库】695- 岛屿的最大面积(深搜)
  39. python【力扣LeetCode算法题库】面试题 01.06-字符串压缩
  40. python【力扣LeetCode算法题库】1160-拼写单词
  41. python【力扣LeetCode算法题库】836- 矩形重叠
  42. python【力扣LeetCode算法题库】409-最长回文串(数学 计数器)
  43. python【力扣LeetCode算法题库】面试题40- 最小的k个数
  44. python【力扣LeetCode算法题库】945- 使数组唯一的最小增量
  45. python【力扣LeetCode算法题库】365- 水壶问题(裴蜀等式)
  46. python【力扣LeetCode算法题库】876- 链表的中间结点
  47. python【力扣LeetCode算法题库】面试题 17.16- 按摩师(DP)
  48. python【力扣LeetCode算法题库】892-三维形体的表面积
  49. python【力扣LeetCode算法题库】999-车的可用捕获量(DFS)
  50. python【力扣LeetCode算法题库】914. 卡牌分组(reduce & collections.Counter)
  51. python【力扣LeetCode算法题库】820- 单词的压缩编码
  52. python【力扣LeetCode算法题库】1162- 地图分析(BFS)
  53. python【力扣LeetCode算法题库】面试题62- 圆圈中最后剩下的数字(约瑟夫环)
  54. python【力扣LeetCode算法题库】912- 排序数组
  55. python【力扣LeetCode算法题库】1111- 有效括号的嵌套深度
  56. python【力扣LeetCode算法题库】289- 生命游戏
  57. python【力扣LeetCode算法题库】12- 整数转罗马数字(打表 模拟)
  58. python【数据结构与算法】内置函数enumerate(枚举) 函数(看不懂你来打我)
  59. python【力扣LeetCode算法题库】13- 罗马数字转整数
  60. python【数据结构与算法】内置函数 zip() 函数(看不懂你来打我)