语句 | 角色 | 例子 |
---|---|---|
赋值 | 创建引用值 | a, b, c = ‘good’, ‘bad’, ‘ugly’ |
调用 | 执行函数 | log.write(“spam, ham”) |
if/elif/else/ | 选择动作 | if “python” in text : print(text) |
for/else | 序列迭代 | for x in mylist: print(x) |
while/else | 一般循环 | while x > y: print(‘hello’) |
pass | 空占位符 | while True: pass |
break | 循环退出 | while True: if exittest(): break |
continue | 循环继续 | while True: if skiptest(): continue |
def | 函数和方法 | def f(a, b, c = 1, *d): print(a+b+c+d[0]) |
return | 函数结果 | def f(a, b, c=1, *d): return a+b+c+d[0] |
yield | 生成器函数 | def gen(n): for i in n: yield i*2 |
python允许使用分号来作为语句结束,但一般不用,一般只有一行挤进多个语句时使用。
a = 1; b = 2
print(a);
a = [1, 2, 3, 4];
一般为了实现跨行,用一对括号把语句括起来就行了。
a = (1, 2,
3, 4)
b = (1 + 2
+ 3 + 4)
if ( 1 == 1 and
2 == 2 and
3 == 3):
print('hello')
一个老的规则也允许我们跨行,就是在语句后面加’’,但我们一般不使用这样的规则,因为它显得脆弱。
x = 1 + 2\
+ 3 + 4
print(x)
while True:
a = input('Enter text: ')
if a == 'stop':
break
try:
num = int(a)
except:
print('Bad!'*4)
else:
print(int(a) ** 2)
print('Bye')
这里的else
是跟着try
的,而不是跟着if
语句。
赋值语句的特性如下:
运算 | 解释 |
---|---|
spam = ‘SPAM’ | 基本形式 |
spam, bam = ‘SPAM’, ‘BAM’ | 元组赋值运算 |
[spam, bam] = [‘SPAM’, ‘BAM’] | 列表赋值运算 |
a, b, c, d = ‘spam’ | 序列赋值运算 |
a, *b = ‘spam’ | 扩展的序列解包 |
a = b = ‘spam’ | 多目标赋值 |
spam += 4 | 增强赋值运算 |
序列赋值运算 在最新的python版本中,元组和列表赋值语句已经统一为序列赋值语句的实例,就是按对应位置赋值。
增强赋值运算 是在原来的对象上操作,而不是对象副本,可以更快操作。
一个带有单个星号的名称,可以在赋值目标中使用,以指定对于序列的一个更为通用的匹配————一个列表赋给了带星号的名称。
>>> a, *b = 1, 2, 3
>>> a, *b
(1, 2, 3)
>>> a, b
(1, [2, 3])
而带星名称在中间时,会引用中间位置的对象形成的列表
>>> a, *b, c = 1, 2, 3, 4, 5
>>> a, b, c
(1, [2, 3, 4], 5)
下面是元组使用的注意事项:
>>> a = [1, 2]; b = [3, 4];
>>> t = (a, b)
>>> t[0][0] = 10
>>> a
[10, 2]
边界情况
如果没有多余的值,则带星号的名称引用空列表对象;而如果有多个带星号,则出错。
>>> a, *b, c, d, e = 1, 2, 3, 4
>>> b
[]
>>> *a, b, *c, d = 1, 2, 3, 4
File "" , line 1
SyntaxError: two starred expressions in assignment
>>> a, *b = 1,2
>>> b
[2]
>>> a = b = c = 'spam'
>>> a, b, c
('spam', 'spam', 'spam')
如果把对象设为初始可变对象,则可能同步改变变量的引用值
>>> a = b = c = []
>>> a.append(1)
>>> a, b, c
([1], [1], [1])
X += Y
X *= Y
X &= Y
...
增强赋值语句的优点:
>>> L = [1, 2, 3]
>>> L += [5, 6]
这里L会自动调用extend()
来优化,而不是L = L + [5, 6]
这样执行两次操作。
python3.0的保留字:
finally is lambda nonlocal global
assert except try in raise
yield True False None
python3.x的print函数
print([object, ...][, sep' '][, end='\n'][, file=sys.stdout])
sep
表示在每两个对象之间插入字符串默认为' '
,end
表示在句末增加字符串,默认为'\n'
。而file
指定发送到的文件、标准流或其他类似文件的对象。
>>> print('hello', end='')
hello>>> print()
>>> print(open('data').read())
hello world!LLLhi! hello world!RRRhi!
>>> s = ('aaaa'
... 'bbbb'
... 'cccc')
>>> s
'aaaabbbbcccc'
>>> s = """aaaa
... bbbb
... cccc"""
>>> s
'aaaa\nbbbb\ncccc'
>>> print(s)
aaaa
bbbb
cccc
None
都认作假True
或False
and
和or
会返回操作对象、False
或True
>>> if None:
... print('None is True')
... else:
... print('None is False')
...
None is False
空对象[]
、{}
和()
是False
>>> if []:
... print('[] is True')
... else:
... print('[] is False')
...
[] is False
and和or返回对象
>>> [] or {}
{}
>>> [] or 2
2
>>> () and 1
()
>>> {} and -0.1
{}
>>> X = 1; Y = 2; Z = 3;
>>> A = Y if X else Z
>>> A
2
>>> X = 0
>>> A = Y if X else Z
>>> A
3
用于生成列表时
>>> [v if v % 2 == 0 else v+3 for v in [1, 2, 3]]
[4, 2, 6]