python3.0 变化 跟2.x 相比

目标:使用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)
...

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值