原書是英文版的“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
等等。
但隨着硬件的升級和設計思想的變革,編譯型和解釋型語言越來越籠統,主要體現在一些新興的高級語言上,而解釋型語言的自身特點也使得編譯器廠商願意花費更多成本來優化解釋器,解釋型語言性能超過編譯型語言也是必然的。
==============================================
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的編程項目和習題大同小異,故此略去。