ch10Python语句简介(python)
#################################################################
# NOTE: at this point in the code extraction process, I changed
# from using the published draft's PDF, to the Word doc files of
# the final draft I submitted to O'Reilly, in order to avoid
# losing the original whitespace. PDF cut-and-paste doesn't
# keep spaces (at least as created), and the 30-minute web
# search didn't turn up many good options (though pyPdf and
# ReportLab's PageCatcher look like good leads). Some tweaking
# was done to the book after the final draft, so if the code in
# remaining chapters' files is slightly out of synch with book,
# you'll want to edit the code manually.
#################################################################
赋值 a,b,c = 'good','vad','ugly'
调用 log.write('spam,ham')
打印调用 print('The Killer',joke)
if/elif/else
for/else
while/else
pass
break
continue
def
return
yield 生成器函数
global
nonlocal
import
from
class
try/except/finally
raise
assert 'X'>'Y','X too small'
with/as
del
def gen(n):
for i in n:
yield i*2
#python的缩进
#python的特殊情况:
a=1;b=2;print(a+b) 多个语句在一行,用分号隔开
在(),[],{}里面的一个语句可以横跨多行
mlist = [111,
222,
333]
X = (A+B+
C+D)
#可以放复合语句
if (A==1 and
B==2 and
C==3):
print("spam" * 3)
X = A + B + \
C + D #反斜线也可以跨越多行
特殊实例:位于一行
if x > y: print(x)
while True:
reply = input('Enter text:')
if reply == 'stop': break
print(reply.upper())
>>> reply = '20'
>>> reply ** 2
...error text omitted...
>>> int(reply) ** 2
400
while True:
reply = input('Enter text:')
if reply == 'stop':
break
print(int(reply) ** 2)
print('Bye')
>>> S = '123'
>>> T = 'xxx'
>>> S.isdigit(), T.isdigit()
(True, False)
while True:
reply = input('Enter text:')
if reply == 'stop':
break
elif not reply.isdigit():
print('Bad!' * 8)
else:
print(int(reply) ** 2)
print('Bye')
while True:
reply = input('Enter text:')
if reply == 'stop': break
try:
num = int(reply)
except:
print('Bad!' * 8)
else:
print(int(reply) ** 2)
print('Bye')
while True:
reply = input('Enter text:')
if reply == 'stop':
break
elif not reply.isdigit():
print('Bad!' * 8)
else:
num = int(reply)
if num < 20:
print('low')
else:
print(num ** 2)
print('Bye')
# NOTE: at this point in the code extraction process, I changed
# from using the published draft's PDF, to the Word doc files of
# the final draft I submitted to O'Reilly, in order to avoid
# losing the original whitespace. PDF cut-and-paste doesn't
# keep spaces (at least as created), and the 30-minute web
# search didn't turn up many good options (though pyPdf and
# ReportLab's PageCatcher look like good leads). Some tweaking
# was done to the book after the final draft, so if the code in
# remaining chapters' files is slightly out of synch with book,
# you'll want to edit the code manually.
#################################################################
赋值 a,b,c = 'good','vad','ugly'
调用 log.write('spam,ham')
打印调用 print('The Killer',joke)
if/elif/else
for/else
while/else
pass
break
continue
def
return
yield 生成器函数
global
nonlocal
import
from
class
try/except/finally
raise
assert 'X'>'Y','X too small'
with/as
del
def gen(n):
for i in n:
yield i*2
#python的缩进
#python的特殊情况:
a=1;b=2;print(a+b) 多个语句在一行,用分号隔开
在(),[],{}里面的一个语句可以横跨多行
mlist = [111,
222,
333]
X = (A+B+
C+D)
#可以放复合语句
if (A==1 and
B==2 and
C==3):
print("spam" * 3)
X = A + B + \
C + D #反斜线也可以跨越多行
特殊实例:位于一行
if x > y: print(x)
while True:
reply = input('Enter text:')
if reply == 'stop': break
print(reply.upper())
>>> reply = '20'
>>> reply ** 2
...error text omitted...
>>> int(reply) ** 2
400
while True:
reply = input('Enter text:')
if reply == 'stop':
break
print(int(reply) ** 2)
print('Bye')
>>> S = '123'
>>> T = 'xxx'
>>> S.isdigit(), T.isdigit()
(True, False)
while True:
reply = input('Enter text:')
if reply == 'stop':
break
elif not reply.isdigit():
print('Bad!' * 8)
else:
print(int(reply) ** 2)
print('Bye')
while True:
reply = input('Enter text:')
if reply == 'stop': break
try:
num = int(reply)
except:
print('Bad!' * 8)
else:
print(int(reply) ** 2)
print('Bye')
while True:
reply = input('Enter text:')
if reply == 'stop':
break
elif not reply.isdigit():
print('Bad!' * 8)
else:
num = int(reply)
if num < 20:
print('low')
else:
print(num ** 2)
print('Bye')
评论
发表评论