ch12if测试和语法规则(python)
#基本例子
>>> if 1:
... print('true')
...
true
>>> if not 1:
... print('true')
... else:
... print('false')
...
false
#多路分支
>>> x = 'killer rabbit'
>>> if x == 'roger':
... print("how's jessica?")
... elif x == 'bugs':
... print("what's up doc?")
... else:
... print('Run away! Run away!')
...
Run away! Run away!
>>> choice = 'ham'
>>> print({'spam': 1.25, # A dictionary-based 'switch'?
... 'ham': 1.99, # Use has_key or get for default
... 'eggs': 0.99,
... 'bacon': 1.10}[choice])
1.99
#多路分支
>>> if choice == 'spam':
... print(1.25)
... elif choice == 'ham':
... print(1.99)
... elif choice == 'eggs':
... print(0.99)
... elif choice == 'bacon':
... print(1.10)
... else:
... print('Bad choice')
...
1.99
>>> branch = {'spam': 1.25,
... 'ham': 1.99,
... 'eggs': 0.99}
>>> print(branch.get('spam', 'Bad choice'))
1.25
>>> print(branch.get('bacon', 'Bad choice'))
Bad choice
#同上面的get函数的效果一样
>>> choice = 'bacon'
>>> if choice in branch:
... print(branch[choice])
... else:
... print('Bad choice')
...
Bad choice
#字典的值可以是函数
#程序代码块结构例子
x = 1
if x:
y = 2
if y:
print('block2')
print('block1')
print('block0')
#避免混合使用制表符和空格:混合使用在python3中会出错
x = 'SPAM' # Error: first line indented
if 'rubbery' in 'shrubbery':
print(x * 8)
x += 'NI' # Error: unexpected indentation
if x.endswith('NI'):
x *= 2
print(x) # Error: inconsistent indentation
x = 'SPAM'
if 'rubbery' in 'shrubbery':
print(x * 8)
x += 'NI'
if x.endswith('NI'):
x *= 2
print(x) # Prints "SPAMNISPAMNI"
#语句包含在() {} [] 中,则语句可以跨越多行,
L = ["Good",
"Bad",
"Ugly"] # Open pairs may span lines
#语句用反斜线结尾可以跨越多行,不建议使用,建议使用下面的方式
if a == b and c == d and \
d == e and f == g:
print('olde') # Backslashes allow continuations...
#
if (a == b and c == d and
d == e and e == f):
print('new') # But parentheses usually do too
x = 1 + 2 + 3 \ # Omitting the \ makes this very different...
+4
#分号让多个语句位于一行
x = 1; y = 2; print(x) # More than one simple statement
#三重引号
S = """
aaaa
bbbb
cccc"""
S = ('aaaa'
'bbbb' # Comments here are ignored
'cccc')
#复合语句的主体可以放到首行
if 1: print('hello') # Simple statement on header line
>>> 2 < 3, 3 < 2 # Less-than: return True or False (1 or 0)
(True, False)
#布尔and和or运算符会返回决定真值的那个对象
>>> 2 or 3, 3 or 2 # Return left operand if true
(2, 3) # Else, return right operand (true or false)
>>> [] or 3
3
>>> [] or {}
{}
>>> 2 and 3, 3 and 2 # Return left operand if false
(3, 2) # Else, return right operand (true or false)
>>> [] and {}
[]
>>> 3 and []
[]
#python的三元表达式
if X:
A = Y
else:
A = Z
#python的三元表达式,效果如上
A = Y if X else Z
>>> A = 't' if 'spam' else 'f' # Nonempty is true
>>> A
't'
>>> A = 't' if '' else 'f'
>>> A
'f'
A = ((X and Y) or Z)
A = [Z, Y][bool(X)]
>>> ['f', 't'][bool('')]
'f'
>>> ['f', 't'][bool('spam')]
't'
X = A or B or C or None
X = A or default
if f1() or f2(): ... #短路运算
tmp1, tmp2 = f1(), f2()
if tmp1 or tmp2: ...
当用类定义新的对象类型的时候,可以用__bool__或__len__方法指定其布尔特性。
>>> if 1:
... print('true')
...
true
>>> if not 1:
... print('true')
... else:
... print('false')
...
false
#多路分支
>>> x = 'killer rabbit'
>>> if x == 'roger':
... print("how's jessica?")
... elif x == 'bugs':
... print("what's up doc?")
... else:
... print('Run away! Run away!')
...
Run away! Run away!
>>> choice = 'ham'
>>> print({'spam': 1.25, # A dictionary-based 'switch'?
... 'ham': 1.99, # Use has_key or get for default
... 'eggs': 0.99,
... 'bacon': 1.10}[choice])
1.99
#多路分支
>>> if choice == 'spam':
... print(1.25)
... elif choice == 'ham':
... print(1.99)
... elif choice == 'eggs':
... print(0.99)
... elif choice == 'bacon':
... print(1.10)
... else:
... print('Bad choice')
...
1.99
>>> branch = {'spam': 1.25,
... 'ham': 1.99,
... 'eggs': 0.99}
>>> print(branch.get('spam', 'Bad choice'))
1.25
>>> print(branch.get('bacon', 'Bad choice'))
Bad choice
#同上面的get函数的效果一样
>>> choice = 'bacon'
>>> if choice in branch:
... print(branch[choice])
... else:
... print('Bad choice')
...
Bad choice
#字典的值可以是函数
#程序代码块结构例子
x = 1
if x:
y = 2
if y:
print('block2')
print('block1')
print('block0')
#避免混合使用制表符和空格:混合使用在python3中会出错
x = 'SPAM' # Error: first line indented
if 'rubbery' in 'shrubbery':
print(x * 8)
x += 'NI' # Error: unexpected indentation
if x.endswith('NI'):
x *= 2
print(x) # Error: inconsistent indentation
x = 'SPAM'
if 'rubbery' in 'shrubbery':
print(x * 8)
x += 'NI'
if x.endswith('NI'):
x *= 2
print(x) # Prints "SPAMNISPAMNI"
#语句包含在() {} [] 中,则语句可以跨越多行,
L = ["Good",
"Bad",
"Ugly"] # Open pairs may span lines
#语句用反斜线结尾可以跨越多行,不建议使用,建议使用下面的方式
if a == b and c == d and \
d == e and f == g:
print('olde') # Backslashes allow continuations...
#
if (a == b and c == d and
d == e and e == f):
print('new') # But parentheses usually do too
x = 1 + 2 + 3 \ # Omitting the \ makes this very different...
+4
#分号让多个语句位于一行
x = 1; y = 2; print(x) # More than one simple statement
#三重引号
S = """
aaaa
bbbb
cccc"""
S = ('aaaa'
'bbbb' # Comments here are ignored
'cccc')
#复合语句的主体可以放到首行
if 1: print('hello') # Simple statement on header line
>>> 2 < 3, 3 < 2 # Less-than: return True or False (1 or 0)
(True, False)
#布尔and和or运算符会返回决定真值的那个对象
>>> 2 or 3, 3 or 2 # Return left operand if true
(2, 3) # Else, return right operand (true or false)
>>> [] or 3
3
>>> [] or {}
{}
>>> 2 and 3, 3 and 2 # Return left operand if false
(3, 2) # Else, return right operand (true or false)
>>> [] and {}
[]
>>> 3 and []
[]
#python的三元表达式
if X:
A = Y
else:
A = Z
#python的三元表达式,效果如上
A = Y if X else Z
>>> A = 't' if 'spam' else 'f' # Nonempty is true
>>> A
't'
>>> A = 't' if '' else 'f'
>>> A
'f'
A = ((X and Y) or Z)
A = [Z, Y][bool(X)]
>>> ['f', 't'][bool('')]
'f'
>>> ['f', 't'][bool('spam')]
't'
X = A or B or C or None
X = A or default
if f1() or f2(): ... #短路运算
tmp1, tmp2 = f1(), f2()
if tmp1 or tmp2: ...
当用类定义新的对象类型的时候,可以用__bool__或__len__方法指定其布尔特性。
评论
发表评论