码迷,mamicode.com
首页 > 编程语言 > 详细

python核心编程2第二章课后练习

时间:2014-09-24 11:18:06      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   for   div   

2-1

print来显示变量的内容,仅用变量名时,输出的字符串使用单引号括起来的,这是为了让非字符串对象也能以字符串的方式显示在屏幕上,print语句使用str()函数显示对象,交互解释器调用repr()函数来显示对象

2-2
(a)运算1+2*4
(b)只会做运算不会输出
(c)运算未显示结果
(d)交互解释器输入一段语句后会返回语句结果
(e)print ‘1+2*4’
bubuko.com,布布扣 bubuko.com,布布扣
2-3
bubuko.com,布布扣 bubuko.com,布布扣
2-4
(a)
#!/usr/etc/env python
string =raw_input("please type the string:\n")
print string
bubuko.com,布布扣
bubuko.com,布布扣
(b)
#!/usr/etc/env python
string =input("please type the string:\n")
print string
print type(string)

bubuko.com,布布扣

bubuko.com,布布扣
2-5
(a)
#!/usr/etc/env python
i=0
while i<=10:
        print i
        i+=1

bubuko.com,布布扣

bubuko.com,布布扣
(b)
#!/usr/etc/env python
for counter in range(11):
        print counter

bubuko.com,布布扣

bubuko.com,布布扣
2-6
#!/usr/etc/env python
value=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"

bubuko.com,布布扣

bubuko.com,布布扣
2-7
#!/usr/etc/env python
#-*- coding:utf-8 -*-
string =raw_input("please type the string:\n")
print "for循环完成逐字符显示字符串"
for i in string:
        print i
print "while循环完成逐字符显示字符串"
j=0
while j<len(string):
        print string[j]
        j+=1

bubuko.com,布布扣

bubuko.com,布布扣
2-8
#!/usr/etc/env python
#-*- coding:utf-8 -*-
aList =[1,2,3,4,5]
j=0
print "for循环实现输出列表的和"
for i in aList:
        print i
        j+=i
print j
print "while 循环实现输出列表的和"
j=0
i=0
while i<len(aList):
        j+=aList[i]
        print aList[i]
        i+=1
print j

bubuko.com,布布扣

bubuko.com,布布扣
2-9
#!/usr/etc/env python
#-*- coding:utf-8 -*-
aList =(1,3,3)
j=0
print "for循环实现输出列表的和"
for i in aList:
        print i
        j+=i
counter =float(len(aList))
print "平均值为:",j/counter
print "while 循环实现输出列表的和"
j=0
i=0
while i<len(aList):
        j+=aList[i]
        print aList[i]
        i+=1
print j
print "平均值为:",j/counter

bubuko.com,布布扣

bubuko.com,布布扣
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

bubuko.com,布布扣

bubuko.com,布布扣 
2-11
#!/usr/etc/env python
#-*- coding:utf-8 -*-
myList =[]
i=0
sum=0
while i<5:
        counter=input("please enter the number:\n")
        myList+=[counter]
        i+=1
while 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

bubuko.com,布布扣

bubuko.com,布布扣
2-12
a.
获得当前模块的属性列表
[‘__builtins__‘, ‘__doc__‘, ‘__name__‘]
b.
dir
<built-in function dir>
会显示内建函数dir
c.
看见类型为内建函数方法
<type ‘builtin_function_or_method‘>
d.
bubuko.com,布布扣 bubuko.com,布布扣
2-13
a.
bubuko.com,布布扣 bubuko.com,布布扣
b.
bubuko.com,布布扣 bubuko.com,布布扣
c.
 bubuko.com,布布扣
bubuko.com,布布扣
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,number2
if number1>number3:
        number3,number1=number1,number3
if number2>number3:
        number3,number2=number2,number3
print number1,number2,number3
number1,number2,number3=number3,number2,number1
print number1,number2,number3

bubuko.com,布布扣

bubuko.com,布布扣

python核心编程2第二章课后练习

标签:style   blog   http   color   io   os   使用   for   div   

原文地址:http://www.cnblogs.com/Fly9527/p/3989780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!