Python核心编程第二版附录A

VIP免费
2025-03-03 1 0 336.13KB 10 页 5.9玖币
侵权投诉
1011
Answers to Selected Exercises
Chapter 2
5. Loops and numbers
a)
i = 0
while
i < 11:
i += 1
b)
for
i
in
range(11):
pass
6. Conditionals
n = int(raw_input('enter a number: '))
if
n < 0:
print
'negative'
elif
n > 0:
print
'positive'
else
:
print
'zero'
Chun_AppA.fm Page 1011 Wednesday, August 23, 2006 5:19 PM
1012 Appendix A
7. Loops and strings
s = raw_input('enter a string: ')
for
eachChar
in
s:
print
eachChar # (does not print index)
or
for
i
in
range(len(s)):
print
i, s[i]
or
i = 0
slen = len(s)
while
i < slen:
print
i, s[i]
or
for
i, x
in
enumerate(s):
print
i, x
8. Loops and operators
subtot = 0
for
i
in
range(5):
subtot += int(raw_input('enter a number: '))
print
subtot
or
# uses sum() BIF and generator expressions
print
sum(int(raw_input('enter a number: '))
for
i
in
range(5))
Chapter 3
4. Statements
Use
;
5. Statements
Use
\
(unless part of a comma-separated sequence in which case
\
is optional)
7. Identifiers
40XL
number
$saving$
symbol
print
keyword
0x40L
number
big-daddy
symbol
2hot2touch
number
thisIsn'tAVar
symbol
if
keyword
counter-1
symbol
Chun_AppA.fm Page 1012 Wednesday, August 23, 2006 5:19 PM
摘要:

1011AnswerstoSelectedExercisesChapter25.Loopsandnumbersa)i=0whilei<11:i+=1b)foriinrange(11):pass6.Conditionalsn=int(raw_input('enteranumber:'))ifn<0:print'negative'elifn>0:print'positive'else:print'zero'Chun_AppA.fmPage1011Wednesday,August23,20065:19PM1012AppendixA7.Loopsandstringss=raw_input('enter...

展开>> 收起<<
Python核心编程第二版附录A.pdf

共10页,预览2页

还剩页未读, 继续阅读

声明:本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。玖贝云文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知玖贝云文库,我们立即给予删除!
分类:计算机 价格:5.9玖币 属性:10 页 大小:336.13KB 格式:PDF 时间:2025-03-03

开通VIP享超值会员特权

  • 多端同步记录
  • 高速下载文档
  • 免费文档工具
  • 分享文档赚钱
  • 每日登录抽奖
  • 优质衍生服务
/ 10
客服
关注