python核心编程-习题-第二章

python

PS:PDF在线地址:http://bcmi.sjtu.edu.cn/~zhaohai/ptm2012/data/Python-kernel.programming.v2.pdf

2-1  变量,print 和字符串格式化运算符。启动交互式解释器,给一些变量赋值(字符串,数值等等)并通过输入变量名显示它们的值。再用 print 语句做同样的事。这二者有何区别? 也尝试着使用字符串格式运算符%,多做几次,慢慢熟悉。

# -*- coding:utf-8 -*-

>>>a = 123

>>>b = \'123\'

>>>a

123

>>>b

\'123\'

>>>print a

123

>>>print b

123

>>>print \'尽管print语句输出的a和b是一样的,但他们仍然不一样,a是%d,是整型;b是%s,是字符串。\' % (a,b)

尽管print语句输出的a和b是一样的,但他们仍然不一样,a是123,是整型;b是123,是字符串。

 

可见,在交互式解释器中,通过输入变量名称显示值是直接显示各变量包含变量类型的值,即字符串带有字符串的标志\'\'引号;通过print语句,则字符串引号\'\'被去掉。

2-2  程序输出

(a)  这段脚本用来计算 1 + 2 * 4 的值

(b)这段脚本保存后再运行不会输出任何项

(c)保存为脚本运行后,没有任何输出,与预想的一样。究其原因,是因为交互式解释器和脚本运行的不同

(d)单独执行这段代码,不会有任何输出;在交互式解释器中输入 1 + 2 * 4 ,会输出"9".如下:

#!/usr/bin/env python

1 + 2 * 4

(e)使用print语句即可

2-3  略

2-4  (a)  

1 # -*- coding:utf-8 -*-

2 string = raw_input("Please enter something:")

3 print string

输出:

Please enter something:Python is number one!

Python is number one!

(b)

# -*- coding:utf-8 -*-

string = raw_input("Please enter a number:")

print "Your enter is %d" % int(string)

输出:

Please enter a number:123

Your enter is 123

 2-5  (a)

i = 0

while i <= 10:

print i

i += 1

(b)

for i in range(11):

print i

2-6  (1)

# -*- coding:utf-8 -*-

num = 10

if num > 0:

print u"它是正数!"

elif num < 0:

print u"它是负数!"

else:

print u"它是0"

(2)

# -*- coding:utf-8 -*-

num = int(raw_input("输入一个数字:".decode("utf-8").encode("gbk")))

if num > 0:

print u"它是正数!"

elif num < 0:

print u"它是负数!"

else:

print u"它是0"

2-7  (1) while

strings = raw_input("Please enter something:")

i = 0

while i < len(strings):

print strings[i],\'(%d)\' % i

i += 1

(2)  for

strings = raw_input("Please enter something:")

for i,string in enumerate(strings):

print string,"(%d)" % i

2-8  

(1)  

list1 = [123,45,678,91,124]

i = 0

sum = 0

while i < len(list1):

sum = sum + list1[i]

i += 1

print sum

sum = 0

for num in list1:

sum = sum + num

print sum

(2)

num_one = int(raw_input("Please enter the first number:"))

num_two = int(raw_input("Please enter the second numebr:"))

num_three = int(raw_input("Please enter the third number:"))

num_four = int(raw_input("Please enter the fourth number:"))

num_five = int(raw_input("Please enter the fifth number:"))

list1 = [num_one,num_two,num_three,num_four,num_five]

i = 0

sum = 0

while i < len(list1):

sum = sum + list1[i]

i += 1

print sum

sum = 0

for num in list1:

sum = sum + num

print sum

2-9  

number = [12,345,67,890,123]

sum = 0

for num in number:

sum = sum + num

average = float(sum) / 5

print "%f is the average of these five numbers." % average

2-10  

while True:

num = int(raw_input("Please enter a number bewteen 1 and 100:"))

if num >= 1 and num <= 100:

print "Well Done.You done it."

break

else:

print "Man,you are wrong!Please learn to read the prompt!!!"

continue

2-11  

# -*- coding:utf-8 -*-

def calc_sum():

global sum

sum = 0

i = 0

while i < 5:

num = int(raw_input("请依次输入五个数字:".decode("utf-8").encode("gbk")))

sum = sum + num

i += 1

return sum

while True:

choice = raw_input("1求和,2求平均数,X退出,请输入你的选择:".decode("utf-8").encode("gbk"))

if choice == \'X\':

print u"程序将退出"

break

elif choice.isdigit() and int(choice) == 1:
#isdigit()用来判断一个对象是否是数字,是数字返回True,否则返回False。如123.isdigit()返回结果True

print u"计算五个数的和"

calc_sum()

print u"这五个数的和是:",sum

elif choice.isdigit() and int(choice) == 2:

print u"计算五个数的平均值"

calc_sum()

print u"这五个数的平均值是:",float(sum) / 5

else:

print u"请重新输入"

2-12

(a)  dir()会显示所有全局变量

(b)   dir不加括号返回<built-in function dir>,对dir的基础介绍

(c)   type(dir)返回<type \'builtin_function_or_method\'>,意思是内建的函数或方法

2-13

   

2-14

原语句:

print -2 * 4 + 3 ** 2

改写:

print -2 * 4 + (3 ** 2)

2-15/2-16    存疑,待补

 

以上是 python核心编程-习题-第二章 的全部内容, 来源链接: utcz.com/z/388064.html

回到顶部