《Python入门经典 以解决计算问题为导向的Python编程实践》Lesson1

原书是英文版的“The Practice of Computing Using Python”,以下是我自己对后面习题的解答。难免会有错误,希望大家告诉我,我们一起来完善~~


math.hypot()的参数为两个数字(x,y),返回x与y的欧氏距离。


习题 1

1.什么是程序?

程序应该是具有可读性的短文,它将在计算机上执行,从而解决某些问题。

2.Python是一种解释型语言。“解释”在上下文中是什么意思?

===============百度百科======================
计算机不能直接理解高级语言,只能直接理解机器语言,所以必须要把高级语言翻译成机器语言,计算机才能执行高级语言编写的程序。
翻译的方式有两种,一个是编译,一个是解释。两种方式只是翻译的时间不同。编译型语言写的程序执行之前,需要一个专门的编译过程,把程序编译成为机器语言的文件,比如exe文件,以后要运行的话就不用重新翻译了,直接使用编译的结果就行了(exe文件),因为翻译只做了一次,运行时不需要翻译,所以编译型语言的程序执行效率高,但也不能一概而论,部分解释型语言的解释器通过在运行时动态优化代码,甚至能够使解释型语言的性能超过编译型语言。
解释则不同,解释性语言的程序不需要编译,省了道工序,解释性语言在运行程序的时候才翻译,比如解释性basic语言,专门有一个解释器能够直接执行basic程序,每个语句都是执行的时候才翻译。这样解释性语言每执行一次就要翻译一次,效率比较低。解释是一句一句的翻译。
编译型与解释型,两者各有利弊。前者由于程序执行速度快,同等条件下对系统要求较低,因此像开发操作系统、大型应用程序、数据库系统等时都采用它,像C/C++、Pascal/Object Pascal(Delphi)等都是编译语言,而一些网页脚本、服务器脚本及辅助开发接口这样的对速度要求不高、对不同系统平台间的兼容性有一定要求的程序则通常使用解释性语言,如JavaScript、VBScript、Perl、Python、Ruby、MATLAB 等等。
但随着硬件的升级和设计思想的变革,编译型和解释型语言越来越笼统,主要体现在一些新兴的高级语言上,而解释型语言的自身特点也使得编译器厂商愿意花费更多成本来优化解释器,解释型语言性能超过编译型语言也是必然的。
==============================================

Python Shell解释器每次读取一行代码并逐行进行解释。这意味着可以在Python Shell中将代码一次性输入,然后每次只执行一条语句。“解释”就是翻译,执行的意思。

3.Python中的注释是什么?如何表示注释?注释的作用是什么?

出现在该行“#”符号后的任何内容都被忽略——注释。它可以增加程序的可读性。

4.Python中的命名空间是什么?

既是Python中名字列表及其关联的值。


5.提示输入一个数字,它加2,乘3,减6,再除3,最后得到的应该还是这个数。

#-*-coding:gbk-*-
#判断用户输入的是否是数字
def isNum(x):
	try:
		float(eval(x))
	except:
		print "Your input is not a number!"
		return False
	return True

while True:
	inputNumber=raw_input("Please input a number you like:")
	if isNum(inputNumber):
		break
inputNumber=eval(inputNumber)
if type(inputNumber)==type(1):
	inputNumber=int(inputNumber)
elif type(inputNumber)==type(1.1):
	inputNumber=float(inputNumber)

x1=inputNumber+2
print "The number plus 2 is",x1
x2=x1*3
print "Then multiply 3 is",x2
x3=x2-6
print "Then minus 6 is",x3
x4=x3/3
print "Finally divided by 6 is",x4
if x4==inputNumber:
	print "The final result is the number itself!"

6.童谣。。。

#-*-coding:gbk-*-
gotomarket=[
["man",1],
["wives","wife",7],
["bags","bag",7],
["cats","cat",7],
["kitties","kitty",7]
]
total=0
things=0
for i in range(len(gotomarket)):
	if i==0:
		print "There is a",gotomarket[i][0],"go to a market,"
		things+=1
	elif i==1:
		print "having",gotomarket[i][2],gotomarket[i][0],","
		things*=gotomarket[i][2]
	else:
		print "with each",gotomarket[i-1][1],"having",gotomarket[i][2],gotomarket[i][0],","
		things*=gotomarket[i][2]
	total+=things
print "There are",total,"things and people going to the market!"

7.用Turtle Graphics工具画个六角星

#-*-coding:gbk-*-
#六角形内角和720,一个角120,
#所以六角星一个角是60度,所以海龟要右转120度,走50米再左转60度
import turtle
for i in range(6):
	turtle.right(120)
	turtle.forward(50)
	turtle.left(60)
	turtle.forward(50)

8.关于空白:书本P29

9.
(a)8
(b)myInt+=3
10.2.0

11.表达式和语句:书本P28
语句的副作用:在执行语句的时候会发生一些变化。
例如,x=5,没有返回值,但是x确实被赋值了5.

12. 0

13.
(a)21
(b)30-(3**2)+(8/(3**2))*10

14.
(a)256
(b)256
(c)64
因为**运算符结合就是从右向左的【我之前还算错了。。。】
重写(c):(2*2)**3

15.
(a)浮点数
(b)浮点数类型的有意义;整数类型会丢失信息

16.
myInput=raw_input("Please input:")
print str(myInput)
print int(myInput)
print float(myInput)
字符串类型

17.
mynum=raw_input("Please input a number:")
mynum=int(mynum)
print 0 if mynum%2==0 else 1
18.
(2+3)*5
25
19.
1公顷=10 000平方米
1cm=0.01m
1升(公升)=0.001立方米
volumn_m3=10000*0.01
volumn_l=volumn_m3/0.001
100000.0
20.
and=4#SyntaxError
y=2/0#ZeroDivisionError
x=float("a")#ValueError
y=z+1#NameError
import math
x=math.sin()#TypeError
"abc"*"ab"#TypeError
21.
//
#取整除,返回商的整数部分
4/3.0=1.33333
4//3.0=1.0
x|y
#按位或
x^y
#按位异或
x&y
#按位与
~x
#按位取反
x<<y,x>>y
#x向左或向右移y位
22.

#-*-coding:gbk-*-
def Cal1(a,b,c):
	return a+b*c
def Cal2(a,b,c):
	return (a+b)*c
lst=[]
for i in range(-100,101):
	for j in range(-100,101):
		for k in range(-100,101):
			if Cal1(i,j,k)==Cal2(i,j,k) and i!=j and j!=k and i!=k:
				lst.append([i,j,k])
print lst
print len(lst)#79401个结果

23.{},[] 没有效

24.不行,不能对字符串类型数据使用乘法运算

25.

#-*-coding:gbk-*-
myTime=raw_input("please input a number btw 1 and 86400:")
def IfInt(k):
	try:
		int(k)
		return 1
	except:
		return 0
yes=IfInt(myTime)
while yes==0 or int(myTime)<1 or int(myTime)>86400:
	myTime=raw_input("please input a number btw 1 and 86400:")
	yes=IfInt(myTime)
x=myTime
myTime=int(myTime)
Sec=str(myTime%60)
myTime/=60
Min=str(myTime%60)
myTime/=60
Hour=str(myTime)
print x+"秒是"+str(Hour)+"小时"+str(Min)+"分钟"+str(Sec)+"秒."

26.
#-*-coding:gbk-*-
x=int(raw_input("item's number:"))
origin=x
n=6;c=5
if x%(n*c)!=0:
	page=x/(n*c)+1
	x=x%(n*c)
else:
	page=x/(n*c)#1
	x=n*c
if x%c!=0:
	row=x/c+1
	x=x%c
else:
	row=x/c
	x=c
if x%c!=0:
	col=x
else:
	col=c
#print (page-1)*n*c+(row-1)*c+col
print "Item "+str(origin)+" is on page "+str(page)+",row "+str(row)+",col "+str(col)+"."
 
27.和28.
#-*-coding:gbk-*-
#27
S=(x+y)*h/2.
#28
print 10000*5*12.5/100.
#result:6250.0
29.
#-*-coding:gbk-*-
r=6.378e6
m1=5.9742e24
G=6.67300e-11
X=raw_input("Please input the weight of Johson:")
X=float(X)
m=X
m2=X
F=G*m1*m2/(r**2)
print "F =",F
g=F/m
print "g =",g
30.
#-*-coding:gbk-*-
from datetime import datetime
date_now=datetime.now()
print "Today's date is:",date_now.strftime("%Y-%m-%d")
31.
#-*-coding:gbk-*-
import math
a,b,c=3,7,9
def GetAngle(x,y,z):
    res=(x**2+y**2-z**2)/float((2*x*y))
    return math.acos(res)/math.pi*180
A=GetAngle(b,c,a)
B=GetAngle(a,c,b)
C=180-(A+B)
print A,B,C
32.
#-*-coding:gbk-*-
a=raw_input("传球成功次数:")
b=raw_input("传球次数:")
c=raw_input("总传球码数:")
d=raw_input("传球达阵数:")
e=raw_input("拦截次数:")
C=(int(a)/float(b)*100-30)*0.05
Y=(float(c)/float(b)-3)*0.25
T=int(d)/float(b)*100*0.2
I=2.375-(int(e)/float(b)*100*0.25)
print "四分卫传球得分是",(C+Y+T+I)/6.*100
#a=324,b=461,c=3969,d=35,e=10
#result is 112.8
#具体算法原题目有误,
#详情参考网站:http://www.nfl.com/help/quarterbackratingformula
33.
#-*-coding:gbk-*-
def IfInt(raw1):
	try:
		int1=int(raw1)
		print "Your input is",int1
		return 1
	except:
		print "Your input is not a number!"
		return 0
yes=0
while yes==0:
	Raw1=raw_input("Please input a number:")
	yes=IfInt(Raw1)
34.
#-*-coding:gbk-*-
h=raw_input("Please input your height(m):")
w=raw_input("Please input your weight(kg):")
h=float(h)
w=float(w)
bmi=w/h**2
print "Your BMI is",bmi
h=raw_input("Please input your height(inch):")
w=raw_input("Please input your weight(pound):")
h=float(h)*0.0254
w=float(w)*0.45359237
bmi=w/h**2
print "Your BMI is",bmi


Note:Lesson1的编程项目和习题大同小异,故此略去。






  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值