Should I learn python2 or python3 now to learn python?

爱喝马黛茶的安东尼
Release: 2019-06-15 09:26:32
Original
3183 people have browsed it

Which version of python should I learn now? python2 or python3?

Related recommendations: "python Video"

Should I learn python2 or python3 now to learn python?

For those who are just starting to learn Python, they should learn the 3 series version directly , because according to Python's development plan, the 2 series version will no longer be supported in the future. Python is currently in the process of version conversion. However, because the 2 series version has a wide range of applications and a large number of historical legacy projects, Python version switching The plan was not smooth either (repeatedly postponed). Of course, for Python, a mild version switching policy is correct, otherwise it may cause a lot of compatibility problems.

Although the Python language has received widespread attention in recent years, and the upward trend is obvious, the Python language itself is not an emerging programming language. Python and Java are programming languages ​​​​that came out at the same time. The Java language "became famous at a young age", while the Python language is a "late bloomer". The Python language was mainly used in the field of web development in the early days. However, due to PHP and Java, Python did not receive widespread attention.

With the development of big data and artificial intelligence, the advantages of the Python language have been reflected. This is also an important reason why Python has received widespread attention and use. Therefore, the current popular direction of Python is focused on big data ( Analysis) and artificial intelligence related directions (machine learning, natural language processing, computer vision).

The direction of machine learning is currently a popular direction, and it is more convenient to use Python to implement algorithms, so developers are more willing to use Python. Machine learning is also one of the important methods of big data analysis (the other is statistical methods), so there are currently many practical applications of machine learning. When I was engaged in machine learning development in the early days, I had been using the Java language. Later, it became more convenient after switching to Python.

In addition to big data and artificial intelligence, Python currently also has certain applications in the embedded field. With the development of the Internet of Things, the development prospects of embedded development are also relatively broad.

The difference between python2 and python3

In addition to introducing import from future, it is also necessary to understand the difference between the two

print function: (Python3 print is a function and must be enclosed in parentheses; print in Python 2 is a class)

The print statement of Python 2 has been replaced by the print() function, which means that we must wrap what we want to print in parentheses objects in .

Python 2

print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
run result:
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line
Copy after login

Python 3

print('Python', python_version())
print('Hello, World!')
print("some text,", end="")
print(' print more text on the same line')
run result:
Python 3.4.1
Hello, World!
some text, print more text on the same line
Copy after login

Parse user input through input(): (Python3 The input obtained in Python2 is of type int, and the raw_input of Python2 is obtained as str type.) Unify: input is used in Python3, row_input is used in Python2, and the input is str

Lucky What's more, the problem of storing user input as a str object has been solved in Python 3. To avoid the dangerous behavior of reading non-string types in Python 2, we have to use raw_input() instead.

Python 2

Python 2.7.6

[GCC 4.0.1 (Apple Inc. build 5493)] on darwin

Type “ help”, “copyright”, “credits” or “license” for more information.

>>> my_input = input('enter a number: ')
enter a number: 123 
>>> type(my_input)
<type &#39;int&#39;> 
>>> my_input = raw_input(&#39;enter a number: &#39;)
enter a number: 123 
>>> type(my_input)
<type &#39;str&#39;>
Copy after login

Python 3

Python 3.4.1

[GCC 4.2.1 (Apple Inc. build 5577)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>> my_input = input(&#39;enter a number: &#39;)
enter a number: 123 
>>> type(my_input)
<class &#39;str&#39;>
Copy after login

Divisible: (doesn’t have much impact)( In Python3, / means true division, % means remainder, // means floor division (the result is rounded); in Python2, / means the result is obtained based on the decimal point of the divisor and dividend, // also means floor division) To unify: in Python3, / means true Division, % means remainder, //the result is rounded; in Python2, the decimal point / means true division, % means remainder, //the result is rounded

Python 2

print &#39;Python&#39;, python_version()
print &#39;3 / 2 =&#39;, 3 / 2
print &#39;3 // 2 =&#39;, 3 // 2
print &#39;3 / 2.0 =&#39;, 3 / 2.0
print &#39;3 // 2.0 =&#39;, 3 // 2.0
Copy after login

run result :

Python 2.7.6

3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
Copy after login

Python 3

print(&#39;Python&#39;, python_version())
print(&#39;3 / 2 =&#39;, 3 / 2)
print(&#39;3 // 2 =&#39;, 3 // 2)
print(&#39;3 / 2.0 =&#39;, 3 / 2.0)
print(&#39;3 // 2.0 =&#39;, 3 // 2.0)
Copy after login

run result:

Python 3.4.1

3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
Copy after login

xrange module:

In Python 3, range() is implemented like xrange() so that a dedicated xrange() function no longer exists (in Python 3 xrange( ) will throw a named exception).

The use of xrange() to create iterable objects is very popular in Python 2. For example: for loop or list/set/dictionary comprehension.

This behaves very much like a generator (ie. "lazy evaluation"). But this xrange-iterable is infinite, meaning you can traverse infinitely.

Due to its lazy evaluation, the xrange() function is faster than range() if you have to iterate over it just once (such as a for loop). However, rather than iterating once, it is not recommended that you iterate multiple times because the generator starts from scratch each time.

Comparison between python 2.4 and python 3.0

1. Print changes from statement to function

Original: print 1, 2 3

Changed to: print (1, 2 3)

2. Range and xrange

Original: range(0, 4) The result is the list [0,1,2,3]

Change For: list(range(0,4))

Original: xrange(0, 4) Suitable for variable control of for loop

改为:range(0,4)

三、字符串

原: 字符串以 8-bit 字符串存储

改为: 字符串以 16-bit Unicode 字符串存储

四、try except 语句的变化

原:

try:
          ......
     except    Exception, e :
         ......
Copy after login

改为

  try:
          ......
     except    Exception as e :
         ......
Copy after login

五、打开文件

原: file( ..... )

或 open(.....)

改为:

只能用 open(.....)

六、从键盘录入一个字符串

原: raw_input( "提示信息" )

改为: input( "提示信息" )

七、bytes 数据类型

A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256.

bytes 可以看成是“字节数组”对象,每个元素是 8-bit 的字节,取值范围 0~255。

由于在 python 3.0中字符串以 unicode 编码存储,当写入二进制文件时,字符串无法直接写入(或读取),必须以某种方式的编码为字节序列后,方可写入。

(一)字符串编码(encode) 为 bytes

例: s = "张三abc12"

b = s.encode( 编码方式)

# b 就是 bytes 类型的数据

# 常用的编码方式为 : "uft-16" , "utf-8", "gbk", "gb2312", "ascii" , "latin1" 等

# 注 : 当字符串不能编码为指定的“编码方式”时,会引发异常

(二) bytes 解码(decode)为字符串

s = "张三abc12"

b = s.encode( "gbk") # 字符串 s 编码为 gbk 格式的字节序列

s1 = b.decode("gbk") # 将字节序列 b以gbk格式 解码为字符串

# 说明,当字节序列不能以指定的编码格式解码时会引发异常

(三)使用方法举例

#coding=gbk
f = open("c:\\1234.txt", "wb")
s = "张三李四abcd1234"
# -------------------------------
# 在 python2.4 中我们可以这样写:
# f.write( s )
# 但在 python 3.0中会引发异常
# -------------------------------
b = s.encode("gbk")
f.write( b )
f.close()
input("?")
Copy after login

读取该文件的例子:

#coding=gbk
f = open("c:\\1234.txt", "rb")
f.seek(0,2) #定位至文件尾
n = f.tell() #读取文件的字节数
f.seek(0,0) #重新定位至文件开始处
b = f.read( n )
# ------------------------------
# 在 python 2.4 中 b 是字符串类型
# 要 python 3.0 中 b 是 bytes 类型
# 因此需要按指定的编码方式确码
# ------------------------------ 
s = b.decode("gbk")
print ( s )
# ------------------------------
# 在 python 2.4 中 可以写作 print s 或 print ( s )
# 要 python 3.0 中 必须写作 print ( s )
# ------------------------------ 
f.close()
input("?")
Copy after login

运行后应显示:

张三李四abcd1234

(四) bytes序列,一但形成,其内容是不可变的

例:

s="ABCD"
b=s.encode("gbk")
print b[0]       # 显示   65
b[0] = 66
Copy after login

执行该句,出现异常: 'bytes' object does not support item assignment

八、 chr( K ) 与 ord( c )

python 2.4.2以前

chr( K ) 将编码K 转为字符,K的范围是 0 ~ 255

ord( c ) 取单个字符的编码, 返回值的范围: 0 ~ 255

python 3.0

chr( K ) 将编码K 转为字符,K的范围是 0 ~ 65535

ord( c ) 取单个字符的编码, 返回值的范围: 0 ~ 65535

九、 除法运算符

python 2.4.2以前

10/3 结果为 3

python 3.0

10 / 3 结果为 3.3333333333333335

10 // 3 结果为 3

十、字节数组对象 --- 新增

(一) 初始化

  a = bytearray( 10 )
Copy after login

# a 是一个由十个字节组成的数组,其每个元素是一个字节,类型借用 int

# 此时,每个元素初始值为 0

(二) 字节数组 是可变的

a = bytearray( 10 )
a[0] = 25
Copy after login

# 可以用赋值语句更改其元素,但所赋的值必须在 0 ~ 255 之间

(三) 字节数组的切片仍是字节数组

(四) 字符串转化为字节数组

 #coding=gbk
     s ="你好"
     b = s.encode( "gbk")     # 先将字符串按某种“GBK”编码方式转化为 bytes
     c = bytearray( b )          #再将 bytes 转化为 字节数组
Copy after login

也可以写作

 c = bytearray( "你好", "gbk")
Copy after login

(五) 字节数组转化为字符串

 c = bytearray( 4 )
       c[0] = 65 ; c[1]=66; c[2]= 67; c[3]= 68
      s = c.decode( "gbk" )
       print ( s )
Copy after login

# 应显示: ABCD

(六) 字节数组可用于写入文本文件

#coding=gbk
f = open("c:\\1234.txt", "wb")
s = "张三李四abcd1234"
# -------------------------------
# 在 python2.4 中我们可以这样写:
# f.write( s )
# 但在 python 3.0中会引发异常
# -------------------------------
b = s.encode("gbk")
f.write( b )
c=bytearray( "王五","gbk")
f.write( c )
f.close()
input("?")
Copy after login

The above is the detailed content of Should I learn python2 or python3 now to learn python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!