使用逗号输出
#每个参数之间都插入了一个空格符
>>> print 'Age:',12
Age:12
把某件事件作为另一件事导入
#从模块导入函数的时候,通常可以使用
import somemodule
或者
from somemodule import somefunction
或者
from somemodule import somefunction,anotherfunction,yetanotherfunction
from somemodule import *
#解释器看作假
False None 0 "" () [] {}
条件执行和if语句
if name.endwith('Alice'):
print 'Hello,Mr.Alice'
elif name.startswitch('Bob'):
print'Hello,Mr.Bob'
else:
print 'Hello,Everyone'
# " == "相等运算符
>>> "foo" = "foo"
True
>>> "foo" = "bar"
False
#" is "同一性运算符
>>> x = y = [1,2,3]
>>> z= [1,2,3]
>>> x is y
>>> True
>>> x is z
>>> False
# " in " 成员资格运算符
>>> name = ' Alice'
>>> 's' in name
True
#断言
if not condition
crash program
>>> age = 10
>>> assert 0< age <10
>>> age = -1
>>> assert 0 < age < 100
报错
#while 循环
x = 1
while x <=100
print x
x+=1
# for循环
for word in num:
print word
循环遍历字典元素
d = {'x':1,'y':2,'z':3}
for key in d:
print key. 'corresponeds to'. d[key]
并行迭代:同时迭代两个序列
内建的zip函数就可以用来进行并行迭代,可以把两个序列“压缩”在一起,然后返回一个元组的列表
>>> zip(names,ages)
[('Alice,12),('Bob',13)]
break
continue