PYTHON核心编程第二版第2章习题答案_第1页
PYTHON核心编程第二版第2章习题答案_第2页
PYTHON核心编程第二版第2章习题答案_第3页
PYTHON核心编程第二版第2章习题答案_第4页
PYTHON核心编程第二版第2章习题答案_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

2-1.变量,print 和字符串格式化操作符。启动交互式解释器,给一些变量赋值(字符串,数 值等)并通过输入变量名显示他们的值。再用 print 语句做同样的事。这两者有何区别?也 尝试着使用字符串格式操作符%,多做几次,慢慢熟悉它。 答案: 对于一个字符串,在仅使用变量名时,输出的字符串是用单引号括起来了的。这是为了让非 字符串对象也能以字符串的方式显示在屏幕上, 即它显示的是该对象的字符串表示, 而不仅 仅是字符串本身。如果使用 print 命令,能使输出更加友好。 2-2.程序输出。阅读下面的 Python 脚本。 #!/usr/bin/env python 1 + 2 * 4 (a)你认为这段脚本是用来做什么的? (b)你认为这段脚本会输出什么? (c)输入以上代码,并保存为脚本,然后运行它,它所做的与你的预期一样吗?为什么一 样/不一样? (d)这段代码单独执行和在交互解释器中执行有何不同?试一下,然后写出结果。 (e)如何改进这个脚本,以便它能和你想象的一样工作? 答案: (a)计算 (b)输出 9 (c)不一样,不会有输出 (d)在交互解释器中可以输出 9 (e)需添加一个 print,即 #!/usr/bin/env python print 1 + 2 * 4 2-3.数值和操作符。启动交互解释器,使用 Python 对两个数值(任意类型)进行加、减、乘、 除运算。然后使用取余操作符来得到两个数相除的余数,最后使用乘方操作符求 A 数的 B 次方。 答案: 当使用 x/y 形式进行除法运算时,如果 x 和 y 都是整形,那么运算的结果就是运算的整数部 分。 print 10 / 3 3 如果 x 和 y 中有一个是浮点数,那么会进行精确除法。 print 10 / 3.0 3.33333333333 所谓地板除,采用 x/y 的形式,得到不大于结果的最大整数值,这个运算时与操作数无关 的。 1/2 0 1.0/2 0.0 -1/2.0 -1.0 2-4. 使用 raw_input()函数得到用户输入。 (a)创建一段脚本使用 raw_input()内建函数从用户输入得到一个字符串,然后显示这个用 户刚刚键入的字符串。 (b)添加一段类似的代码,不过这次输入的是数值。将输入数据转换为一个数值对象, (使 用 int()或其他数值转换函数)并将这个值显示给用户看(注意,如果你用的是早于 1.5 的版 本,你需要使用 string.ato*()函数执行这种转换)。 答案: (a) a = raw_input(please input a string: ) please input a string: hello world print a hello world (b) a = raw_input(please input a number: ) please input a number: 123 print type(a) a = int(raw_input(please input a number: ) please input a number: 123 print type(a) 2-5.循环和数字。分别使用 while 和 for 创建一个循环。 (a)写一个 while 循环,输出整型为 010(要确保是 010,而不是 09 或 110)。 (b)做同(a)一样的事,不过这次使用 range()内建函数。 答案: (a) i = 0 while i for i in range(0,11): print i, 0 1 2 3 4 5 6 7 8 9 10 2-6.条件判断。判断一个数是正数还是负数,或者是 0。开始先用固定的数值,然后修改你 的代码支持用户输入数值再进行判断。 答案: a = int(raw_input(Please input a number: ) if a 0: print The number is Positive. elif a 0: print The number is negative. else: print The number is Zero. 2-7.循环和字串。从用户那里接受一个字符串输入,然后逐字符显示该字符串。先用 while 循环实现,然后再用 for 循环实现。 for 循环 a = raw_input(please input a string: ) for i in a: print i, while 循环 a = raw_input(please input a string: ) i = 0 while i len(a): print ai, i = i + 1 2-8.循环和操作符。创建一个包含五个固定数值的列表或元组,输出他们的和。然后修改你 的代码为接受用户输入数值。分别使用 while 和 for 循环实现。 l = 1,2,3,4,5 total = 0 for i in l: total = total + i print Total is,total l = 1,2,3,4,5 i = 0 total = 0 while i dir() _builtins_, _doc_, _name_, _package_ dir type(dir) dir._doc_ dir(object) - list of stringsnnIf called without an argument, return the names in the current scope.nElse, return an alphabetized list of names comprising (some of) the attributesnof the given object, and of attributes reachable from it.nIf the object supplies a method named _dir_, it will be used; otherwisenthe default dir() logic is used and returns:nfor a module object: the modules attributes.nfor a class object:its attributes, and recursively the attributesnof its bases.nfor any other object: its attributes, its classs attributes, andnrecursively the attributes of its classs base classes. print dir._doc_ dir(object) - list of strings If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. If the object supplies a method named _dir_, it will be used; otherwise the default dir() logic is used and returns: for a module object: the modules attributes. for a class object:its attributes, and recursively the attributes of its bases. for any other object: its attributes, its classs attributes, and recursively the attributes of its classs base classes. 2-13.利用 dir()找出 sys 模块中更多的东西。 (a)启动 Python 交互式解释器,执行 dir()函数。然后键入 import sys 以导入 sys 模块。再 次执行 dir()函数以确认 sys 模块被正确导入。然后执行 dir(sys),你就可以看到 sys 模块的所 有属性了。 (b)显示 sys 模块的版本号属性及平台变量。记住在属性名前一定要加 sys.,这表示这个属 性是 sys 模块的。其中 version 变量保存着你使用的 Python 解释器版本, platform 属性则包 含你运行 Python 时使用的计算机平台信息。 (c)最后,调用 sys.exit()函数。这是一种热键之外的另一种推出 Python 解释器的方式。 Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec5 2015, 20:32:19) MSC v.1500 32 bit (Intel) on win32 Type copyright, credits or license() for more information. dir() _builtins_, _doc_, _name_, _package_ import sys dir() _builtins_, _doc_, _name_, _package_, sys dir(sys) _displayhook_, _doc_, _excepthook_, _name_, _package_, _stderr_, _stdin_, _stdout_, _clear_type_cache, _current_frames, _getframe, _mercurial, api_version, argv, builtin_module_names, byteorder, call_tracing, callstats, copyright, displayhook, dllhandle, dont_write_bytecode, exc_clear, exc_info, exc_traceback, exc_type, exc_value, excepthook, exec_prefix, executable, exit, flags, float_info, float_repr_style, getcheckinterval, getdefaultencoding, getfilesystemencoding, getprofile, getrecursionlimit, getrefcount, getsizeof, gettrace, getwindowsversion, hexversion, long_info, maxint, maxsize, maxunicode, meta_path, modules, path, path_hooks, path_importer_cache, platform, prefix, py3kwarning, setcheckinterval, setprofile, setrecursionlimit, settrace, stderr, stdin, stdout, subversion, version, version_info, warnoptions, winver sys.version 2.7.11 (v2.7.11:6d1b6a68f775, Dec5 2015, 20:32:19) MSC v.1500 32 bit (Intel) sys.exit() 2-14.操作符优先级和括号分组。重写 2.4 小节中 print 语句里的算术表达式,试着在这个表 达式中添加合适的括号以便它能正常工作。 答案: 略 2-15.元素排序。 (a) 让用户输入三个数值并分别将它们报存到 3 个不同的变量中。 不使用列表或排序算法, 自己写代码来对三个数由小到大排序。 (b)修改(a)的解决方案,使之从大到小排序。 答案: (a) a = int(raw_input(input number a: ) b = int(raw_input(input number b: ) c = int(raw_input(input number c: ) if a b: a,b = b,a if a c: a,c =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论