文件名称:Core_python_programming_素材
文件大小:327KB
文件格式:PDF
更新时间:2021-12-24 14:47:09
CorePython Python
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 PM1012 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))