目标:使用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)
...
分享到:
相关推荐
This edition also incorporates notes about prominent enhancements in the imminent Python 3.1 release, which is intended to subsume Python 3.0 (in this book, Python 3.0 generally refers to the language...
《PyPI官网下载的tencentcloud-sdk-python-rkp-3.0.343.tar.gz:深入了解腾讯云Python SDK》 在Python编程中,我们经常需要利用各种第三方库来扩展功能,提高开发效率。PyPI(Python Package Index)是Python官方的...
What’s New In Python 3.0 Common Stumbling Blocks Print Is A Function Views And Iterators Instead Of Lists Ordering Comparisons Integers Text Vs. Data Instead Of Unicode Vs. 8-bit Overview Of ...
该脚本将创建一个 release-notes.txt 文件,其中包含自 Jenkins 上次升级后的提交(默认)或拉取请求(您可以选择升级级别) 提交日志取自 GitHub API,因此您甚至可以使用浅克隆来创建发行说明。 最后,您不仅...
ERPNext 代码获得 GNU 通用公共许可证 (v3) 许可,文档获得知识共享 (CC-BY-SA-3.0) 许可,版权归 Web Notes Technologies Pvt Ltd (Web Notes) 和贡献者所有。 标志和商标 品牌名称 ERPNext 和徽标是 Web Notes ...