好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

现在学python该学python2还是python3?

python现在该学哪个版本?python2还是python3?

相关推荐:《python视频》

对于刚开始学习Python的人来说,应该直接学习3系列版本,因为按照Python的发展规划,未来将不再支持2系列版本,目前Python也正处在版本转换的过程中,但是由于2系列版本有广泛的应用和大量的历史遗留项目,所以Python的版本切换计划也并不顺利(一再推迟)。当然,对于Python来说,温和的版本切换政策是正确的,否则有可能会带来大量的兼容性问题。

Python语言虽然在近几年得到了广泛的关注,而且上升趋势明显,但是Python语言本身并不是一门新兴的编程语言,Python与Java是同一时期面世的编程语言,只不过Java语言“年少成名”,而Python语言则属于“大器晚成”。Python语言早期主要应用于Web开发领域,但是由于PHP和Java的原因,Python并没有得到广泛的重视。

随着大数据和人工智能的发展,Python语言的优势才得到了体现,这也是Python得到广泛关注和使用的重要原因,所以当前Python比较热门的方向就集中在大数据(分析)和人工智能相关方向(机器学习、自然语言处理、计算机视觉)。

机器学习方向是目前比较热门的方向,而且采用Python来完成算法实现是比较方便的,所以开发人员更愿意采用Python。机器学习同时也是大数据分析的重要方式之一(另一种是统计学方式),所以目前机器学习的落地应用也比较多。我在早期从事机器学习开发的时候一直在使用Java语言,后来改用Python之后确实要更加方便一些。

除了大数据和人工智能方向之外,目前Python在嵌入式领域也有一定的应用,随着物联网的发展,嵌入式开发的发展前景也比较广阔。

python2和python3的区别

除了引入import from future,了解一下两者的区别也是很必要的

print函数:(Python3中print为一个函数,必须用括号括起来;Python2中print为class)

Python 2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打印在小括号中的对象。

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 

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 

[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 'int'> 
>>> my_input = raw_input('enter a number: ')
enter a number: 123 
>>> type(my_input)
<type 'str'> 

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

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

>>> my_input = input('enter a number: ')
enter a number: 123 
>>> type(my_input)
<class 'str'> 
print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0 
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0 
print('Python', python_version())
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0) 
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0 

改为: print ( 1, 2+3 )

二、range 与 xrange

原 : range( 0, 4 ) 结果 是 列表 [0,1,2,3 ]

改为:list( range(0,4) )

原 : xrange( 0, 4 ) 适用于 for 循环的变量控制

改为:range(0,4)

三、字符串

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

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

四、try except 语句的变化

原:

try:
          ......
     except    Exception, e :
         ...... 

改为

  try:
          ......
     except    Exception as e :
         ...... 

五、打开文件

原: 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("?") 

读取该文件的例子:

#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("?") 

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

例:

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

(二) 字节数组 是可变的

a = bytearray( 10 )
a[0] = 25 

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

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

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

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

也可以写作

 c = bytearray( "你好", "gbk") 

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

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

# 应显示: 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("?") 

以上就是现在学python该学python2还是python3?的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于现在学python该学python2还是python3?的详细内容...

  阅读:44次