版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、2-1.变量,print和字符串格式化操作符。启动交互式解释器,给一些变量赋值(字符串,数值等)并通过输入变量名显示他们的值。再用print语句做同样的事。这两者有何区别?也尝试着使用字符串格式操作符%,多做几次,慢慢熟悉它。答案:对于一个字符串,在仅使用变量名时,输出的字符串是用单引号括起来了的。这是为了让非字符串对象也能以字符串的方式显示在屏幕上,即它显示的是该对象的字符串表示,而不仅仅是字符串本身。如果使用print命令,能使输出更加友好。2-2.程序输出。阅读下面的Python脚本。#!/usr/bin/env python1 + 2 * 4(a)你认为这段脚本是用来做什么的?(b)你
2、认为这段脚本会输出什么?(c)输入以上代码,并保存为脚本,然后运行它,它所做的与你的预期一样吗?为什么一样/不一样?(d)这段代码单独执行和在交互解释器中执行有何不同?试一下,然后写出结果。(e)如何改进这个脚本,以便它能和你想象的一样工作?答案:(a)计算(b)输出9(c)不一样,不会有输出(d)在交互解释器中可以输出9(e)需添加一个print,即#!/usr/bin/env pythonprint 1 + 2 * 42-3.数值和操作符。启动交互解释器,使用Python对两个数值(任意类型)进行加、减、乘、除运算。然后使用取余操作符来得到两个数相除的余数,最后使用乘方操作符求A数的B次方
3、。答案:当使用x/y形式进行除法运算时,如果x和y都是整形,那么运算的结果就是运算的整数部分。 print 10 / 33如果x和y中有一个是浮点数,那么会进行精确除法。 print 10 / 3.03.33333333333所谓地板除,采用x/y的形式,得到不大于结果的最大整数值,这个运算时与操作数无关的。 1/20 1.0/20.0 -1/2.0-1.02-4. 使用raw_input()函数得到用户输入。(a)创建一段脚本使用raw_input()内建函数从用户输入得到一个字符串,然后显示这个用户刚刚键入的字符串。(b)添加一段类似的代码,不过这次输入的是数值。将输入数据转换为一个数值对
4、象,(使用int()或其他数值转换函数)并将这个值显示给用户看(注意,如果你用的是早于1.5的版本,你需要使用string.ato*()函数执行这种转换)。答案:(a) a = raw_input(please input a string: )please input a string: hello world print ahello 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:
5、)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 102-6.条件判断。判断一个数是正数还是负数,或者是0。开始先用固定的数值,然后修改你的代码支持用户输入数值再进行判断。答案:a = int(raw_input(Plea
6、se 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 = 0whi
7、le i len(a):print ai,i = i + 12-8.循环和操作符。创建一个包含五个固定数值的列表或元组,输出他们的和。然后修改你的代码为接受用户输入数值。分别使用while和for循环实现。l = 1,2,3,4,5total = 0for i in l:total = total + iprint Total is,totall = 1,2,3,4,5i = 0total = 0while i len(l):total = total + lii = i + 1print Total is,total2-9.循环和操作符。创建一个包含五个固定数值的列表或元组,输出他们的平均值
8、。本练习的难点之一是通过除法得到平均值。你会发现整型除会截去小数,因此你必须使用浮点除以得到更精确的结果。float()内建函数可以帮助你实现这一功能。答案:total = 0for i in range(0, 5): print Please input number %d % (i + 1) a = float(raw_input() total = total + aprint The average is, total / 5-i = 0total = 0while i = 1 and a dir()_builtins_, _doc_, _name_, _package_ dir ty
9、pe(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_,
10、it will be used; otherwisenthe default dir() logic is used and returns:n for a module object: the modules attributes.n for a class object: its attributes, and recursively the attributesn of its bases.n for any other object: its attributes, its classs attributes, andn recursively the attributes of it
11、s classs base classes. print dir._doc_dir(object) - list of stringsIf called without an argument, return the names in the current scope.Else, return an alphabetized list of names comprising (some of) the attributesof the given object, and of attributes reachable from it.If the object supplies a meth
12、od named _dir_, it will be used; otherwisethe 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 attrib
13、utes 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
14、()函数。这是一种热键之外的另一种推出Python解释器的方式。Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) MSC v.1500 32 bit (Intel) on win32Type copyright, credits or license() for more information. dir()_builtins_, _doc_, _name_, _package_ import sys dir()_builtins_, _doc_, _name_, _package_, sys dir(sys)_display
15、hook_, _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_traceba
16、ck, 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
17、, 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.version2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) MSC v.15
18、00 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 客户信用等级评定标准
- 基于工作坊的劳技教学模式与成效分析
- 零售业行政管理岗位工作手册与技巧
- 旅游酒店物资采购经理面试问题与解答
- 零售连锁超市商品统一企业采购技巧
- 2026广东惠州市惠城区马安镇中心幼儿园招聘备考题库含完整答案详解(名师系列)
- 哈药集团股份有限公司2026届春季校园招聘备考题库附答案详解【黄金题型】
- 7α-Hydroxypregnenolone-生命科学试剂-MCE
- 2026江苏南京航空航天大学金城学院招聘备考题库(马克思主义学院)(模拟题)附答案详解
- 2026年节目冠名合同(1篇)
- 桥牌协会内部管理制度
- 2026重庆市南岸区消防救援支队消防文员招录2人笔试备考试题及答案解析
- 2026年山东省立第三医院初级岗位公开招聘人员(27人)笔试备考试题及答案解析
- 2026年滁州天长市大通镇预任制村干及村级后备干部储备库选拔28名笔试备考试题及答案解析
- 【新教材】人教PEP版(2024)四年级下册英语全册教案(含教学计划)
- 挤塑工艺培训课件
- 生成式AI赋能下的高中地理教师教育观念更新与教学实践研究教学研究课题报告
- 2025执业药师继续教育试题库及参考答案(完整版)
- 2026年医药领域腐败问题集中整治自查自纠报告与医院卫生院整治群众身边
- 江苏省公路水运工程项目安全生产条件核查表
- 2026年淮南职业技术学院单招职业倾向性测试题库附答案
评论
0/150
提交评论