Python3学习 - 第一节

从今天开始,打算系统的学习一遍python3。有感兴趣的可以跟我一起学,每周至少更新四次。

1. Python的3.0版本

Python 3.0 final was released on December 3rd, 2008.Python 3.0 (a.k.a. "Python 3000" or "Py3k") is a new version of the language that is incompatible with the 2.x line of releases. The language is mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Also, the standard library has been reorganized in a few prominent places.

Python的3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。

2. Python2.x与3​​.x版本区别

a. print 函数

print语句没有了,取而代之的是print()函数。

原:print   5

改为:print ( 5 )

b. Unicode

Python 2 有 ASCII str() 类型,unicode() 是单独的,不是 byte 类型。现在, 在 Python 3,我们最终有了 Unicode (utf-8) 字符串,以及一个字节类:byte 和 bytearrays。

c. try except 语句的变化

原:try:          

                 ......    

        except    Exception, e :        

                ......

改为:try:         

                 ......    

            except    Exception as e :        

                  ......

d. range 与 xrange

在 Python 2 中 xrange() 创建迭代对象的用法是非常流行的。比如: for 循环或者是列表/集合/字典推导式。

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

改为:range(0, 4)

e. 不等运算符

Python 2.x中不等于有两种写法 != 和 <>

Python 3.x中去掉了<>, 只有!=一种写法

f. 去掉了repr表达式``

Python 2.x 中反引号``相当于repr函数的作用

Python 3.x 中去掉了``这种写法,只允许使用repr函数

g. 从键盘录入一个字符串

原:raw_input( "请输入一个整型:" ) 

改为: input( "请输入一个整型:" )

h. 打开文件

原:file( ..... )或open(.....)

改为:只能用open(.....)

这些大概是常用到的,还有很多其他的区别,可以自己了解一下,后面用到还会再说。

你可能感兴趣的:(Python3学习 - 第一节)