首页 > 代码库 > python核心编程2第二章课后练习
python核心编程2第二章课后练习
2-1
print来显示变量的内容,仅用变量名时,输出的字符串使用单引号括起来的,这是为了让非字符串对象也能以字符串的方式显示在屏幕上,print语句使用str()函数显示对象,交互解释器调用repr()函数来显示对象
2-2
(a)运算1+2*4
(b)只会做运算不会输出
(c)运算未显示结果
(d)交互解释器输入一段语句后会返回语句结果
(e)print ‘1+2*4’
2-3
2-4
(a)
#!/usr/etc/env pythonstring =raw_input("please type the string:\n")print string
(b)
#!/usr/etc/env pythonstring =input("please type the string:\n")print stringprint type(string)
2-5
(a)
#!/usr/etc/env pythoni=0while i<=10: print i i+=1
(b)
#!/usr/etc/env pythonfor counter in range(11): print counter
2-6
#!/usr/etc/env pythonvalue=http://www.mamicode.com/input("please type the value:\n")if value>0: print "This number is positive"elif value<0: print "This number is negative"else: print "This number is zero"
2-7
#!/usr/etc/env python#-*- coding:utf-8 -*-string =raw_input("please type the string:\n")print "for循环完成逐字符显示字符串"for i in string: print iprint "while循环完成逐字符显示字符串"j=0while j<len(string): print string[j] j+=1
2-8
#!/usr/etc/env python#-*- coding:utf-8 -*-aList =[1,2,3,4,5]j=0print "for循环实现输出列表的和"for i in aList: print i j+=iprint jprint "while 循环实现输出列表的和"j=0i=0while i<len(aList): j+=aList[i] print aList[i] i+=1print j
2-9
#!/usr/etc/env python#-*- coding:utf-8 -*-aList =(1,3,3)j=0print "for循环实现输出列表的和"for i in aList: print i j+=icounter =float(len(aList))print "平均值为:",j/counterprint "while 循环实现输出列表的和"j=0i=0while i<len(aList): j+=aList[i] print aList[i] i+=1print jprint "平均值为:",j/counter
2-10
#!/usr/etc/env python#-*- coding:utf-8 -*-while True: i =input("please type the number:\n") if(0<i<100): print"success!" break else: pass
2-11
#!/usr/etc/env python#-*- coding:utf-8 -*-myList =[]i=0sum=0while i<5: counter=input("please enter the number:\n") myList+=[counter] i+=1while True: print""" (1)取五个数的和 (2)取五个数的平均值 (3)退出 """ choose=input("please enter your choose:\n") if choose==1: for j in range(len(myList)): sum+=myList[j] print sum elif choose==2: for j in range(len(myList)): sum+=myList[j] print sum/(len(myList)+1) elif choose==3: break
2-12
a.
获得当前模块的属性列表
[‘__builtins__‘, ‘__doc__‘, ‘__name__‘]
b.
dir
<built-in function dir>
会显示内建函数dir
c.
看见类型为内建函数方法
<type ‘builtin_function_or_method‘>
d.
2-13
a.
b.
c.
2-14
print (-2*4)+(3**2)
2-15
#!/usr/etc/env python#-*-coding:utf-8-*-number1 =input("请输入第一个数值!:\n")number2 =input("请输入第二个数值!:\n")number3 =input("请输入第三个数值!:\n")if number1>number2: number2,number1=number1,number2if number1>number3: number3,number1=number1,number3if number2>number3: number3,number2=number2,number3print number1,number2,number3number1,number2,number3=number3,number2,number1print number1,number2,number3
python核心编程2第二章课后练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。