python3.0 变化 跟2.x 相比
2009-05-25 16:50
246 查看
目标:使用python写出正确语法格式的程序,并让它能高效的跑起来。python的确是一个很好的编写脚本的语言,本身自带了很多好的结构。
1.8//5 output 1 8/5 output 1.6 新增加了//运算符,老实说不怎么喜欢这个改变,个人更倾向于int(8/5)来获得8//5的计算
2.In interactive mode,可以通过_得到上次的屏幕输出
3.print 变成一个函数 print()
4.一些list操作:
>>> # Replace some items:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # Remove some:
... a[0:2] = []
>>> a
[123, 1234]
>>> # Insert some:
... a[1:1] = [’bletch’, ’xyzzy’]
>>> a
[123, ’bletch’, ’xyzzy’, 1234]
>>> # Insert (a copy of) itself at the beginning
>>> a[:0] = a
>>> a
[123, ’bletch’, ’xyzzy’, 1234, 123, ’bletch’, ’xyzzy’, 1234]
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]
5.The keyword end can be used to avoid the newline after the output, or end the output with a different string:
print(b, end=’ ’)
6.range()返回的是一个可以iterable的对象,这个对象并没有展开为一个list,这样可以节省空间。这些对象可以通过一个迭代器iterator来进行遍历,比如for语句,list()则是另一个iterator
7.Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list(with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement。有些时候这个else语句的确还是蛮有用的,省去了其他语言需要设置一个flag的代价。
8.*操作符用来unpack list 或者tuple:
>>> args = [3, 6]
>>> list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
9.**操作符用来unpack dictionary:
>>> def parrot(voltage, state=’a stiff’, action=’voom’):
... print("-- This parrot wouldn’t", action, end=’ ’)
... print("if you put", voltage, "volts through it.", end=’ ’)
... print("E’s", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin’ demised", "action": "VOOM"}
>>> parrot(**d)
10.list comprehension:
>>> [3*x for x in vec if x > 3]
11.创建一个空的set,需要使用set()
12.dict()可以从list直接创建一个dictionary,但是要求list的每一项都是一个key:value的tuple:
>>> dict([(’sape’, 4139), (’guido’, 4127), (’jack’, 4098)])
{’sape’: 4139, ’jack’: 4098, ’guido’: 4127}
13.dict comprehensions:{x: x**2 for x in (2, 4, 6)}
14.another dict参数:dict(sape=4139, guido=4127, jack=4098)
15.loop through a sequence,使用enumerate()可以同时取得position index和value:
>>> for i, v in enumerate([’tic’, ’tac’, ’toe’]):
16.zip()函数:
>>> questions = [’name’, ’quest’, ’favorite color’]
>>> answers = [’lancelot’, ’the holy grail’, ’blue’]
>>> for q, a in zip(questions, answers):
... print(’What is your {0}? It is {1}.’.format(q, a))
17.import imp; imp.reload(modulename)可以重新加载模块
18.from module important * 可以引入模块中的所有方法和变量,除了以_开头的
19.The module compileall can create .pyc files (or .pyo files when -O is used) for all modules in a directory.
20.the new built-in vars() function, which returns a dictionary containing all local variables
21.print()函数提供{key:option}格式化的方式,还有string的format()来取代以前的%操作,虽然%操作被保留但是最终还是会从语言中被删除,所以以后还是不要再使用。
22.except Exception as e 以前是 except Exception,e,感觉现在的语法更容易接受。
23.The with statement allows objects like files to be used in a way that ensures they are always cleaned up promptly
and correctly.
with open("myfile.txt") as f:
for line in f:
print(line)
After the statement is executed, the file f is always closed, even if a problem was encountered while processing
the lines.
一个很奇怪的语法,没弄懂什么意思。
24.关于scope和namespace,写了一长段的东西,看了半天,还不如看下面的一个例子:
def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment:", spam)
do_nonlocal()
print("After nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam)
scope_test()
print("In global scope:", spam)
output is :
after local assignment: test spam
after nonlocal assignment:nonlocal spam
after global assignment:nonlocal spam
In global scope:global spam
这里的函数里面又定义函数第一次看到,fresh。
25.yield 语句可以帮助创建iterator,它会记录上次访问数据的位置,并会自动产生StopIteration:
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
>>> for char in reverse(’golf’):
... print(char)
...
1.8//5 output 1 8/5 output 1.6 新增加了//运算符,老实说不怎么喜欢这个改变,个人更倾向于int(8/5)来获得8//5的计算
2.In interactive mode,可以通过_得到上次的屏幕输出
3.print 变成一个函数 print()
4.一些list操作:
>>> # Replace some items:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # Remove some:
... a[0:2] = []
>>> a
[123, 1234]
>>> # Insert some:
... a[1:1] = [’bletch’, ’xyzzy’]
>>> a
[123, ’bletch’, ’xyzzy’, 1234]
>>> # Insert (a copy of) itself at the beginning
>>> a[:0] = a
>>> a
[123, ’bletch’, ’xyzzy’, 1234, 123, ’bletch’, ’xyzzy’, 1234]
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]
5.The keyword end can be used to avoid the newline after the output, or end the output with a different string:
print(b, end=’ ’)
6.range()返回的是一个可以iterable的对象,这个对象并没有展开为一个list,这样可以节省空间。这些对象可以通过一个迭代器iterator来进行遍历,比如for语句,list()则是另一个iterator
7.Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list(with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement。有些时候这个else语句的确还是蛮有用的,省去了其他语言需要设置一个flag的代价。
8.*操作符用来unpack list 或者tuple:
>>> args = [3, 6]
>>> list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
9.**操作符用来unpack dictionary:
>>> def parrot(voltage, state=’a stiff’, action=’voom’):
... print("-- This parrot wouldn’t", action, end=’ ’)
... print("if you put", voltage, "volts through it.", end=’ ’)
... print("E’s", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin’ demised", "action": "VOOM"}
>>> parrot(**d)
10.list comprehension:
>>> [3*x for x in vec if x > 3]
11.创建一个空的set,需要使用set()
12.dict()可以从list直接创建一个dictionary,但是要求list的每一项都是一个key:value的tuple:
>>> dict([(’sape’, 4139), (’guido’, 4127), (’jack’, 4098)])
{’sape’: 4139, ’jack’: 4098, ’guido’: 4127}
13.dict comprehensions:{x: x**2 for x in (2, 4, 6)}
14.another dict参数:dict(sape=4139, guido=4127, jack=4098)
15.loop through a sequence,使用enumerate()可以同时取得position index和value:
>>> for i, v in enumerate([’tic’, ’tac’, ’toe’]):
16.zip()函数:
>>> questions = [’name’, ’quest’, ’favorite color’]
>>> answers = [’lancelot’, ’the holy grail’, ’blue’]
>>> for q, a in zip(questions, answers):
... print(’What is your {0}? It is {1}.’.format(q, a))
17.import imp; imp.reload(modulename)可以重新加载模块
18.from module important * 可以引入模块中的所有方法和变量,除了以_开头的
19.The module compileall can create .pyc files (or .pyo files when -O is used) for all modules in a directory.
20.the new built-in vars() function, which returns a dictionary containing all local variables
21.print()函数提供{key:option}格式化的方式,还有string的format()来取代以前的%操作,虽然%操作被保留但是最终还是会从语言中被删除,所以以后还是不要再使用。
22.except Exception as e 以前是 except Exception,e,感觉现在的语法更容易接受。
23.The with statement allows objects like files to be used in a way that ensures they are always cleaned up promptly
and correctly.
with open("myfile.txt") as f:
for line in f:
print(line)
After the statement is executed, the file f is always closed, even if a problem was encountered while processing
the lines.
一个很奇怪的语法,没弄懂什么意思。
24.关于scope和namespace,写了一长段的东西,看了半天,还不如看下面的一个例子:
def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment:", spam)
do_nonlocal()
print("After nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam)
scope_test()
print("In global scope:", spam)
output is :
after local assignment: test spam
after nonlocal assignment:nonlocal spam
after global assignment:nonlocal spam
In global scope:global spam
这里的函数里面又定义函数第一次看到,fresh。
25.yield 语句可以帮助创建iterator,它会记录上次访问数据的位置,并会自动产生StopIteration:
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
>>> for char in reverse(’golf’):
... print(char)
...
相关文章推荐
- Python 3.0 beta 1 变化大,更简洁、更统一
- 相比于python2.6,python3.0的新特性。
- resin3.1.3相比于resin3.0的变化
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Python3.6新特性2 Python2.7新特性Python 2.7的新特性 - 牛皮糖NewPtone - 博客园.html Python 3_x 新特性及10大变化_python_脚本之家.htm
- [转]Python3.0与2.6变化
- Python2.4+ 与 Python3.0+ 主要变化或新增内容
- Python 3.0 面向对象
- python opencv-3.0 SIFT/SURF 特征提取与匹配
- Python3.0 PyCharm控制台 中文乱码
- python3.0语言简介及开发环境搭建
- Python2.6和Python3.0中八进制数字表示区别
- Facebook开源时间序列内存数据库Beringei,追求极致压缩率——如果是int根据大多数时间序列中的值与相邻数据点相比并没有显著的变化,只要使用XOR将当前值与先前值进行比较,然后存储发生变化的比特。最终,该算法将整个数据集至少压缩了90%
- Python for Windows——监控Windows某个目录下文件的变化
- cocos2dx-3.0 python创建项目报错
- Cocos2d-x 3.0 回调函数的变化 .
- commands moudle on python will replace with subprocess on py3.0
- Retrofit 2.0 最新版本beta3.0变化
- 在Python 3.0中持久化数据至文件中,使用pickle
- 使用python3.0 编写C/S程序,初识wangpython的网络编程