Python數(shù)學(xué)實(shí)驗(yàn)各章習(xí)題答案_第1頁
Python數(shù)學(xué)實(shí)驗(yàn)各章習(xí)題答案_第2頁
Python數(shù)學(xué)實(shí)驗(yàn)各章習(xí)題答案_第3頁
Python數(shù)學(xué)實(shí)驗(yàn)各章習(xí)題答案_第4頁
Python數(shù)學(xué)實(shí)驗(yàn)各章習(xí)題答案_第5頁
已閱讀5頁,還剩149頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

Python數(shù)學(xué)實(shí)驗(yàn)各章習(xí)題答案與提示

第1章習(xí)題答案

習(xí)題M

1.標(biāo)識(shí)符的第一個(gè)字符必須是字母表中的字母(大寫或小寫)或者一個(gè)下劃線.標(biāo)識(shí)符名稱的其他部分

可以由字母(大寫或小寫)、下劃線或數(shù)字(0-9)組成.標(biāo)識(shí)符名稱是對(duì)大小寫敏感的.例如,myname

和myName不是一個(gè)標(biāo)識(shí)符.注意前者中的小寫n和后者中的大寫N.常量:數(shù)值不變的;變量:數(shù)值會(huì)

變動(dòng)的量.

2.單引號(hào)表示的字符串里可包含雙引號(hào),當(dāng)然不能包含單引號(hào);雙引號(hào)表示的字符串里可以包含單引號(hào),字符

串都只能有一行;三個(gè)引號(hào)能包含多行字符串,同時(shí)常常出現(xiàn)在函數(shù)的聲明的下一行,來注釋函數(shù)的功能,

與眾不同的地方在于,這個(gè)注釋作為函數(shù)的一個(gè)默認(rèn)屬性,可以通過函數(shù)名._doc_來訪問.

3.異常是指因?yàn)槌绦驁?zhí)行過程中出錯(cuò)而在正??刂屏饕酝獠扇〉男袨?嚴(yán)格來說,語法錯(cuò)誤和邏輯錯(cuò)誤不屬于

異常,但有些語法錯(cuò)誤往往會(huì)導(dǎo)致異常,例如由于大小寫拼寫錯(cuò)誤而訪問不存在的對(duì)象,或者試圖訪問不

存在的文件,等等.

習(xí)題1-2

1.#縮進(jìn)對(duì)其

字符串列表元組字典

列表和字典字符串、數(shù)字、元組

習(xí)題1-3

1.代碼編輯窗口【Editor],在這里主要用于編寫腳本代碼;交互式shell【IPylhonconsole],主要用

于顯示腳本運(yùn)行結(jié)果,最重要的功能是與用戶進(jìn)行交互,用戶可以快速驗(yàn)證代碼運(yùn)行結(jié)果是否符合預(yù)期;

變量/幫助/繪圖【Variable/Help/Plots】,用戶可以在這里查看變量的詳細(xì)信息(數(shù)值、類型、結(jié)構(gòu)等),查

看函數(shù)的Help說明,顯示繪圖結(jié)果等.

2.Spyder功能主要有注釋功能、運(yùn)行程序功能、調(diào)試功能、Source操作功能等等.

總習(xí)題1

1.對(duì)錯(cuò)對(duì)對(duì)對(duì)對(duì)

2.當(dāng)列表增加或刪除元素時(shí),列表對(duì)象自動(dòng)進(jìn)行內(nèi)存擴(kuò)展或收縮,從而保證元素之間沒有縫隙,但這

涉及到列表元素的移動(dòng),效率較低,應(yīng)盡量從列表尾部進(jìn)行元素的增加與刪除操作以提高處理速度.

3.每個(gè)Python腳本在運(yùn)行時(shí)都有一個(gè)“name”屬性.如果腳本作為模塊被導(dǎo)入,則其“name”屬性的值被

自動(dòng)設(shè)置為模塊名;如果腳本獨(dú)立運(yùn)行,則其“name”屬性值被自動(dòng)設(shè)置為“main”.利用“name”屬性即可控制

Python程序的運(yùn)行方式.

-2-各章習(xí)題答案與提示

第2章習(xí)題答案

習(xí)題2-2

1

foriinrange(l,85):

if168%i==0:

j=168/i

ifi>jand(i+j)%2==0and(i-j)%2==0:

m=(i+j)/2

n=(i-j)/2

x=n*n-100

print(x)

score=int(input(,輸入分?jǐn)?shù):\rf))

ifscore>=90:

grade='A'

elifscore>=60:

grade=B

else:

grade=C

print(1%d屬于%s'%(score,grade))

3

importstring

s=inputC請(qǐng)輸入一個(gè)字符串:\n)

letters=0

space=0

digit=0

others=0

forcins:

ifc.isalpha():

letters+=1

elifc.isspace():

space+=1

elifc.isdigit():

digit+=1

else:

others+=1

print(fchar=%d,space=%d,digit=%d,others=%d'%(letters,space,digit,others))

習(xí)題2-3

1

1=[]

foriinrange(3):

各章習(xí)題答案與提示-3-

x=int(input(,integer:\n,))

l.append(x)

Lsort()

print(1)

2

a=[1,2,3]

b=a[:]

print(b)

3

importtime

myD={1:2:rb'}

forkey,valueindict.items(myD):

print(key,value)

time.sleep(l)

習(xí)題2-4

1

if_name—=='_main_

fromsysimportstdout

filename=raw_input('輸入文件名:\n')

fp=open(filename,"w")

ch=raw_inputC輸入字符串:\n')

whilech!='#':

fp.write(ch)

stdout.write(ch)

ch=raw_input(H)

fp.close()

2

if_name_=='_main_

fp=open(,test.txt,,'w,)

string=raw_input('pleaseinputastring:\n')

string=string.upper()

fp.write(string)

fp=open(,test.txt,,'r')

printfp.read()

fp.close()

3

if—name—=='—main—

importstring

fp=openCtestl.txf)

a=fp.read()

fp.close()

fp=open(,test2.txt,)

b=fp.read()

fp.close()

-4-各章習(xí)題答案與提示

fp=open(,test3.txt\'w,)

1=list(a+b)

l.sort()

s="

s=s.join(l)

fp.write(s)

fp.close()

習(xí)題2-5

1

importeasyguiasg

importrandom

g.msgboxC嗨,歡迎進(jìn)入第一個(gè)界面小游戲!)

secret=random.randint(1,10)

msg二”猜猜我心中的數(shù)字是什么(0?10):”

title="數(shù)字小游戲”

guess=egerbox(msg,title,lowerbound=0,upperbound=10)

whileTrue:

ifguess==secret:

g.msgboxC你是我肚里的蛔蟲嗎!,)

break

else:

ifguess>secret:

g.msgboxC猜大了呀,再試試,)

else:

g.msgboxf猜小了呀,再試試')

guess=egerbox(msg,title,lowerbound=0,upperbound=10)

g.msgboxC游戲結(jié)束,不玩啦!)

2

fromeasyguiimport*

msg二”輸入你的個(gè)人信息"

title="登錄界面”

fieldNames=[”賬號(hào)名*",“密碼*“,“地址“,”聯(lián)系電話*”,“郵箱地址”]

fieldValues=[]

fieldValues=multenterbox(msg,title,fieldNames)

while1:

iffieldValues==None:

break

errmsg=

foriinrange(len(fieldNames)):

if(fieldValues[i].strip()=="")and(fieldNames[i][-l]

errmsg=errmsg+(“'%s”是必填項(xiàng).\n\n'%fieldNames[i])

iferrmsg==

各章習(xí)題答案與提示-5-

break

fieldValues=multenterbox(errmsg,title,fieldNames,fieldValues)

print("個(gè)人信息:fieldValues)

總習(xí)題2

1

s=0

t=1

foriinrange(l,21):

t*=i

s+=t

prints

2.

ans=「Yes?No]

i=int(rawjnput(lnputanumber。0000?99999):'))

ifi<10000ori>99999:

print"InputError!'

else:

i=str(i)

flag=0

forjinrange(0,2):

flag=1

break

printans[flag]

3.

print'input10numbersplease:1

1=[]

foriinrange(10):

l.append(int(raw_input(,Inputanumber:*)))

foriinrange(9):

forjinrange(i+l,10):

ifl[jl<l[i]:

temp=l|jj

1U1=Ki]

l[ij=temp

print1

4.

1=[0,10,20,30,40,50]

print'Thesortedlistis:\l

ent=len(l)

n=int(raw_input(*Inputanumbe二'))

Lappend(n)

foriinrange(cnt):

ifn<l[i]:

forjinrange(cnt,i,-I):

1U]=1U-1]

-6-各章習(xí)題答案與提示

l[i]=n

break

printThenewsortedlistis:\l

num_list=[]

cou=0

foriinrange(l,5):

forjinrange。,5):

forkinrange(l,5):

ifi!=jandj!=kandk!=i:

res=i*100+j*10+k

num_list.append(res)

cou+=l

print(num_list,cou)

6.

fromtkinterimport*

fromtkinter.scroUedtextimportScrolledText

defload():

withopen(filename.get())asfile:

contents.delete('l.0",END)

contents.insert(INSERT,file.read())

defsave():

withopen(filename.get(),asfile:

file.write(contents.get('l.0\END))

top=Tk()

top.title(uSimpleEditor0)

contents=ScrolledText()

contents.pack(side=BOTTOM,expand=True,fill=BOTH)

filename=Entry()

filename.pack(side=LEFT,expand=True,fill=X)

Button(text='Open',command=load).pack(side=LEFT)

Button(text=,Save',command=save).pack(side=LEFT)

mainloop()

第3章習(xí)題答案

習(xí)題3-1

1.輸入

importmatplotlib.pyplotaspit#導(dǎo)入matplotlib.pyplot記作pit

plt.rcParams[,font.sans-serif]=[,SimHei,]#設(shè)置中文顯示

p!t.rcParams[,axes.unicode_minus,]=False#解決保存圖像是負(fù)號(hào)"而顯示為方塊的問題

x二「星期一:星期二;星期三;星期四:星期五:星期六?星期日1#輸入x軸信息

y=[10,40,60,80,50,40,10]#輸入y軸數(shù)據(jù)

plLbar(x,y)#繪制統(tǒng)計(jì)數(shù)據(jù)條形圖

各章習(xí)題答案與提示-7-

則輸出相應(yīng)的圖形.

2.輸入

importmalplotlib.pyplotaspit#導(dǎo)入matplotlib.pyploti己作pit

x二「北京?天津;上海?廣州?沈陽?成都口#輸入x軸信息

y=[10,40,80,30,70,20]#輸入y軸數(shù)據(jù)

plt.pie(y,labels=x)#繪制統(tǒng)計(jì)數(shù)據(jù)餅形圖,x作為標(biāo)簽

則輸出相應(yīng)的圖形.

3.輸入

importmatplotlib.pyplotaspit#導(dǎo)入matplotlib.pyplot記作pit

#輸入x軸信息

y=[1,4,5,7,8,5,4,1]#輸入y軸數(shù)據(jù)

plt.plot(x,y)#繪制統(tǒng)計(jì)數(shù)據(jù)線形圖

則輸出相應(yīng)的圖形.

4.輸入

importmatplotlib.pyplotaspit

fromnumpy.randomimportrandn

plt.plot(randn(l00).cumsum(),,k-')

plt.show()

則輸出相應(yīng)的圖形.

5.輸入

importpandasaspd

importmatplotlib.pyplotaspit

importnumpyasnp

plt.rcParams['font.sans-serif]=['SimHei']#設(shè)置中文顯示

plt.rcParams['axes.unicode_minus,]=False#解決保存圖像是負(fù)號(hào)而顯示為方塊的問題

df=pd.DataFrame(np.random.rand(20,4),columns=[,A,,,B,,,C',"D'])#產(chǎn)生數(shù)據(jù)

fig=df.plot(figsize=(12,4))#figsize:創(chuàng)建圖表窗口,設(shè)置窗口大小

plt.title('pandas繪圖示例)#圖名

plt.xlabelC這是X軸)#x軸標(biāo)簽

plt.ylabelC這是Y軸)#y軸標(biāo)簽

plt.legend(loc='upperright')#顯示圖例,loc表示位置,右上方

-8-各章習(xí)題答案與提示

plt.xlim([0,20])#x軸邊界,x的取值范圍

plt.ylim([0,1.5])#y軸邊界,y的取值范圍

plt.xticks(range(20))#設(shè)置x刻度

plt.yticks([0,0.2,0.4,0.6,0.8,1.0,1.2])#設(shè)置y刻度

fig.set_xticklabels(n%.1f'%iforiinrange(20))#x軸刻度標(biāo)簽

fig.set_jticklabels(n%.2f'%iforiin[0,0.2,0.4,0.6,0.8,1.0,1.2])#y軸刻度標(biāo)簽

則輸出相應(yīng)的圖形.

6.輸入

importpandasaspd

importnumpyasnp

s=pd.Series(np.random.randn(100).cumsum())

s.plot(linestyle=marker=color=Hru,grid=True)#直接用風(fēng)格樣式設(shè)置

#s.plot(style=',—.n,alpha=0.8,colormap='Reds_r')

df=pd.DataFrame(np.random.randn(100,6),columns=list(,ABCDEF')).cumsum()

df.plot(style=alpha=0.8,colormap='summer」')#透明度與顏色版

則輸出相應(yīng)的圖形.

7.輸入

importpandasaspd

importnumpyasnp

importmatplotlib.pyplotaspit

df=pd.DataFrame(np.random.randn(10,4))#隨機(jī)產(chǎn)生10行4列數(shù)據(jù)

df.plot(style=^-o*)#每一列數(shù)據(jù)繪制一圖形

plt.text(5,0.5;在這里\fontsize=24)#在點(diǎn)(5,0.5)設(shè)置文本

則輸出相應(yīng)的圖形.

8.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

figl=plt.figure(num=1,figsize=(8,6))

plt.plot(np.random.rand(50).cumsum(),,k-o,)

fig2=plt.figure(num=2,figsize=(8,6))

plt.plot(50-np.random.rand(50).cumsum(),'k—+')

各章習(xí)題答案與提示-9-

li=np.random.randint(0,50,10)#array()類型

print(li)

han=np.cumsum(li)

han1=np.random.randint(0,50,10).cumsum()

print(han)

print(hanl)

則輸出相應(yīng)的圖形.

9.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

importpandasaspd

fig=plt.figure(figsize=(10,6),facecolor='gray')

axl=fig.add_subpiot(2,2,l)

plt.plot(np.random.rand(50).cumsum(),'k—1)

plt.plot(np.random.randn(50),cumsum(),'b—1)

plt.plot(np.random.randn(50).cumsum(),'r—o')

plt.plot(np.random.randn(50).cumsum(),'g-+,)

ax2=fig.add_subplot(2,2,2)

ax2.hist(np.random.rand(50),alpha=0.5)

ax3=fig.add_subp!ot(2,2,3)

dfl=pd.DataFrame(np.random.rand(10,2),columns=['a,,'b,])

ax3.plot(dfl,alpha=0.5,linestyle=,—\marke^'o1)

ax4=fig.add_subplot(2,2,4)

df2=pd.DataFrame(np.random.rand(10,4),columns=['a\,b\,c,,'d,])

ax4.plot(df2,alpha=0.5,linestyle=,—,,marker=7)

則輸出相應(yīng)的圖形.

10.輸入

importmatplotlib.pyplotaspit

importpandasaspd

importnumpyasnp

-10-各章習(xí)題答案與提示

stacked:是否堆疊,默認(rèn)情況下,區(qū)域圖被堆疊

為了產(chǎn)生堆積面積圖,每列必須是正值或全部負(fù)值!

當(dāng)數(shù)據(jù)有NaN時(shí)候,自動(dòng)填充0,圖標(biāo)簽需要清洗掉缺失值

flI!I!

fig,axes=plt.subplots(2,1,figsize=(10,8))

dfl=pd.DataFrame(np.random.rand(10,5),coiumns=[,a','b\'c','d'/e*])

print(df1)

dfl.plot.area(colormap='Greens_r',alpha=0.5,ax=axes[0])

df2=pd.DataFrame(np.random.randn(10,4),columns=[,a,,K,*c',

print(df2)

df2.plot.area(stacked=False,colormap='Set2\alpha=0.5,ax=axes[l])

則輸出相應(yīng)的圖形.

11.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

fig,axes=plt.subplots(2,1,figsize=(10,7))

x=np.linspace(0,1,500)

yl=np.sin(4*np.pi*x)*np.exp(-2*x)

y2=-np.sin(4*np.pi*x)*np.exp(-2*x)

axes[0].fill(x,yl,T',alpha=0.8,label='yl')

axes[0].fill(x,y2,'g',alpha=0.4,label='y2')

#對(duì)函數(shù)與坐標(biāo)軸之間的區(qū)域進(jìn)行填充,使用fill函數(shù)

#也可寫成:yl,T,x,y2Jg;alpha=0.8)

x=np.linspace(0,4*np.pi,100)

yl=np.sin(x)

y2=np.cos(2*x)

axes[1].fill_between(x,yl,y2,color=,b',alpha=0.5,label=,area,)#填充兩個(gè)函數(shù)之間的區(qū)域

foriinrange(2):#添加圖例、格網(wǎng)

axes[ij.legend()

axes[i|.grid()

則輸出相應(yīng)的圖形.

各章習(xí)題答案與提示-II-

12.輸入

importmatplotlib.pyplotaspit

importseabornassns

data=[[l,2,3],[4,5,6],[7,8,9]]

print(data)

sns.violinplot(data=data)

plt.show()

則輸出相應(yīng)的圖形.

習(xí)題3-2

1.輸入

importturtleast

t.begin_fill()

t.color(*green')

t.forward(200)

t.left(120)

t.forward(200)

t.left(120)

t.forward(200)

t.end_fill()

t.hideturtle()

t.done()

則輸出相應(yīng)的圖形.

2.輸入

importturtleast

t.begin_fill()

t?color('blue1)

t,forward(200)

t.left(90)

t.forward(150)

t.left(90)

t?forward(200)

-12-各章習(xí)題答案與提示

t.left(90)

t?forward(150)

t?end_fill()

t.hideturtle()

t,done()

則輸出相應(yīng)的圖形.

3.輸入

importturtleast

t.begin_fill()

t.color「blue')

t.forward(200)

t.left(72)

t.forward(200)

t.left(72)

t?forward(200)

t.left(72)

t.forward(200)

t.left(72)

t.forward(200)

t.left(72)

t.end_fill()

t.hideturtle()

t,done()

則輸出相應(yīng)的圖形.

4.輸入

importturtleast

t.begin_fill()

t.penup()

t.goto(0,-200)

t.pendown()

t,color(1green1)

各章習(xí)題答案與提示-13-

t.pencolor(1red1)

t.pensize(4)

t?circle(200)

t,end_fill()

t,hideturtle()

t?done()

則輸出相應(yīng)的圖形.

5.輸入

importturtleast

t?speed(1)

t.begin_fill()

t.color('green*)

t.pencolor(1brown1)

t.pensize(2)

t.circle(200,90)

t.left(90)

t.circle(200,90)

t.left(135)

t.forward(280)

t.back(180)

t.left(30)

t.forward(70)

t.back(70)

t?right(30)

t,forward(90)

t?right(30)

t,forward(50)

t.back(50)

t.end_fill()

t.penup()

t.goto(0,0)

-14-各章習(xí)題答案與提示

t.pendown()

t.left(90)

t.begin_fill()

t,color(1green1)

t.pencolor(1brown1)

t.pensize(2)

t.circle(200,90)

t.left(90)

t.circle(200,90)

t?left(135)

t?forward(280)

t.back(180)

t.left(30)

t?forward(70)

t.back(70)

t,right(30)

t.forward(90)

t?right(30)

t?forward(50)

t.back(50)

t.end_fill()

t.penup()

t.goto(0,0)

t.pendown()

t,left(135)

t.pensize(5)

t,forward(60)

t?hideturtle()

t.done()

則輸出相應(yīng)的圖形.

6.輸入

各章習(xí)題答案與提示-15-

importturtleast

t.pencolor("yellow0)#定義畫筆顏色

t.fillcolor("red”)#繪制圖形的填充顏色

t.pensize(10)#設(shè)置畫筆的寬度

t.penup()#移動(dòng)時(shí)不繪制圖形,提起筆,用于另起一個(gè)地方繪制時(shí)用

t.begin_fill()#準(zhǔn)備開始填充圖形

t.goto(0,50)#將畫筆移動(dòng)到坐標(biāo)為(X,y)的位置

t.pendown()#移動(dòng)時(shí)繪制圖形,缺省時(shí)也為繪制

t.left(90)#逆時(shí)針移動(dòng)degree0

t.circle(100,180)#畫圓,半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓

t.circle(300,70)

t.left(39)

t.circle(300,70)

t.circle(100,180)

t?hideturtle()

t.end_fill()

t.pencolor(“pink”)

t.penup()

t.goto(0,0)

t.pendown()

t.penup()

t.goto(-200,-200)

t.pendown()

t.left(130)

t,forward(260)

t.penup()

t,forward(240)

t.pendown()

t.forward(80)

t?left(135)

t,forward(20)

-16-各章習(xí)題答案與提示

t.back(20)

t.left(90)

t?forward(20)

t.done()

則輸出相應(yīng)的圖形.

7.輸入

importturtleast

foriinrange(4):

t.forward(80)

t.left(90)

t?done()

則輸出相應(yīng)的圖形為邊長是80的小正方形.

8.輸入

importturtleast

defpic_fun(size,angle):

t.pensize(5)

t.pencolor('red')

t?speed(1)

forsizeinrange(10,100,10):

t,circle(size)

forangleinrange(15,360,15):

t.circle(angle)

pic_fun(10,15)

t?done()

則輸出相應(yīng)的圖形.

9.輸入

importnumpyasnp

importpylabaspl

importtime

frommatplotlibimportcm

defiter_point(c):

各章習(xí)題答案與提示-17-

z=c

foriinrange(1,100):

ifabs(z)>3:break

z=z*z+c

returni

defdraw_mandelbrot(cxzcyzd):

x0zxl,yO,yl=cx-dzcx+d,cy-d,cy+d

y,x=np.ogrid[yO:yl:200j,x0:xl:200j]

c=x+y*1j

start=time.time()

mandelbrot=np.frompyfunc(iter_pointz1,1)(c).astype(np.float)

print(ntime=n,time.time()-start)

pl.imshow(mandelbrotfcmap=cm.Blues_r,extent=[xO,xl,yO,yl])

pl?gca().set_axis_off()

x,y=0.27322626,0.595153338

pl?subplot(231)

draw_mandelbrot(-0.6,0,1.5)

foriinrange(2r7):

pl?subplot(230+i)

draw_mandelbrot(x,yA0.2**(i-1))

pl.subplots_adjust(0.02,0,0.88,1,0.01,0)

pl?show()

則輸出相應(yīng)的圖形.圖形為曼特布洛特集.

10.輸入

importturtleast

importnumpyasnp

t.pensize(5)

t.speed(l)

foriinnp.arange(10,300,20):

t.penup()

t.goto(-200,-l00+i)

-18-各章習(xí)題答案與提示

t.pendown()

t.forward(300)

i=i+l

t.left(-270)

forjinnp.arange(10,300,20):

t.penupO

t.goto(-200+j,-100)

t.pendown()

t.forward(300)

j=j+l

t.done()

則輸出相應(yīng)的圖形.圖形為網(wǎng)格線.

11.輸入

importturtleast

t.penupO

t.goto(0,210)

t.pendown()

t.fillcolor(',redH)

t.begin_fill()

t.circle(10,180)

t.circle(25,110)

t.left(50)

t.circle(60,45)

t.circle(20,170)

t.right(24)

t.fd(30)

t.left(lO)

t.circle(30,110)

t.fd(20)

t.left(40)

t.circle(90,70)

各章習(xí)題答案與提示-19-

t.circle(30,150)

t.right(30)

t.fd(15)

t.circle(80,90)

t.left(15)

t.fd(45)

t.right(165)

t.fd(20)

t.left(155)

t.circle(150,80)

t.left(50)

t.circle(150,90)

t.end_fill()

t.left(150)

t.circle(-90,70)

t.left(20)

t.circle(75,105)

t.setheading(60)

t.circle(80,98)

t.circle(-90,40)

t.left(180)

t.circle(90,40)

t.circle(-80,98)

t.setheading(-83)

t.penupO

t.goto(0,0)

t.pendown()

t.left(90)

t.speed(1)

t.begin_fill()

t.colorCgreen')

-20-各章習(xí)題答案與提示

t.pencolor(*brown')

t.pensize(2)

t.circle(200,90)

t.left(90)

t.circle(200,90)

t.left(135)

t.forward(280)

t.back(180)

t.left(30)

t.forward(70)

t.back(70)

t.right(30)

t.forward(90)

t.right(30)

t.forward(50)

t.back(50)

t.end_fill()

t.penup()

t.goto(0,0)

t.pendown()

t.left(HO)

t.begin_fill()

t.color('green')

t.pencolor('brown')

t.pensize(2)

t.circle(200,90)

t.left(90)

t.circle(200,90)

t.left(135)

t.forward(280)

t.back(180)

各章習(xí)題答案與提示-21-

t.left(30)

t.forward(70)

t.back(70)

t.right(30)

t.forward(90)

t.right(30)

t.forward(50)

t.back(50)

t.end_fill()

t.penupO

t.goto(0,0)

t.pendown()

t.left(135)

t.pensize(5)

t.forward(60)

t.hideturtle()

t.done()

則輸出相應(yīng)的圖形.圖形是一朵花及兩片葉子

12.輸入

importturtleast

t.penupO

t.goto(100,-100)

t.pendown()

t.dot(100;yellow')

t.penupO

t.goto(155,-150)

t.pendown()

foriinrange(50):

Lleft(lOO)

t.forward(60)

t.left(10)

-22-各章習(xí)題答案與提示

t.forward(60)

t.hideturtle()

t.done()

則輸出相應(yīng)的圖形.

總習(xí)題3

1.輸入

importmatplotlib.pyplotaspit

importmatplotlib

matplotlib.rcParamsfTont.family']='SimHei,

x二「一月「二月/三月「四月「五月「六月「七月「八月「九月「十月丁H^一月丁十二月1

y=[13.2,15.7,17.4,12.6,19.7,22.6,20.2,18.3,16.2,15,12.1,8.6]

plt.bar(x,height=y,widlh=0.5,)

plt.xticks(x,x,rotation=30)#這里是調(diào)節(jié)橫坐標(biāo)的傾斜度,rotation是度數(shù)

plt.show()

則輸出相應(yīng)的圖形.

2.輸入

importmatplotlib.pyplotaspit

plt.rcParams['font.sans-serif]=[,SimHei']#用來正常顯示中文標(biāo)簽

x二「一月「二月/三月于四月「五月「六月丁七月于八月「九月于十月丁[^一月「十二月1

y=[13.2,15.7,17.4,12.6,19.7,22.6,20.2,18.3,16.2,15,12.1,8.6]

labels=x

sizes=y

plt.pie(sizes,labels=labels,autopct='%1.1f%%',shadow二False,startangle=150)

pit.title("餅圖示例1-12月份數(shù)據(jù)”)

plt.axisC^qual')#該行代碼使餅圖長寬相等

plt.show()

則輸出相應(yīng)的圖形.

3.輸入

importturtleast

t.bgcolor("white")

myname=t.textinput("輸入你的姓名“,"你的名字?”)

各章習(xí)題答案與提示-23-

colors=["red","yellow","purple","blue","green","pink”]

forxinrange(lOO):

t.pencolor(colors[x%6])

t.penupO

t.forward(x*6)

t.pendown()

t.write(myname,font=("華文行楷”,int((x+6)/6),"bold"))

t.left(92)

t.done()

則輸出相應(yīng)的圖形.

4.略.

第4章習(xí)題答案

習(xí)題4-1

1.輸入

importnumpyasnp

importmatplotlib.pyplotaspit

importsympyassp

n=sp.Symbol('n;integer=True)

xn=2+l/n**2

1=sp.limit(xn,n,'oo')

printC%s極限的值:%s*%(str(xn),str(l)))

n=np.arange(1,50,1)

xn=2+1/n**2

plt.title('2+l/n**2‘)

plt.scatter(n,xn)

plt.axis('on')

plt.show()

則輸出相應(yīng)的結(jié)果為:

2+n**(-2)極限的值:2

圖形如圖4.21所示:

-24-各章習(xí)題答案與提示

2.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

importsympyassp

x=sp.SymbolCx*)

y=x*(sp.sqrt(x**2+1)-x)

x=sp.iimit(y,x/oo*)

print(,%s極限的值:%s*%(str(y),str(x)))

x=np.arange(O,10,0.01)

y=x*(np.sqrt(x**2+1)-x)

plt.title('y=x*(sp.sqrt(x**2+l)-x)')

plt.plot(x,y)

plt.show()

則輸出相應(yīng)的結(jié)果為:

x*(-x+sqrt(x**2+1))極限的值:1/2

圖形如圖4.22所示:

各章習(xí)題答案與提示-25-

3.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

importsympyassp

x=sp.Symbol('x')

y=sp.sin(x)/x

x=sp.limit(y,x,0)

print(,%s極限的值:%s*%(str(y),str(x)))

x=np.arange(-3,3,0.01)

y=np.sin(x)/x

plt.title('y=sin(x)/x')

plt.plot(x,y)

ax=plt.gca()

ax.spineslYight'J.seCcolo^'none')

ax.spines『top'].set_color('none')

ax.spines『bottom'].set_position(('data:0))

ax.spines『left'].set_position(('data',0))

plt.axisC'equal')

plt.show()

則輸出相應(yīng)的結(jié)果為:

sin(x)/x極限的值:1

圖形如圖4.23所示:

-26-各章習(xí)題答案與提示

習(xí)題4-2

1.輸入

importsympyassp

x=sp.Symbol('x')

y=f(x**2+x)

d=sp.diff(y,x,l)

printC原參數(shù)方程導(dǎo)數(shù)結(jié)果為:%s,%d)

則輸出相應(yīng)的結(jié)果為:

原參數(shù)方程導(dǎo)數(shù)結(jié)果為:(2*x+l)*Subs(Derivative(f(_xi_l),(_xi_l,)?(x**2+x,))

2.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

importsympyassp

a=sp.Symbol('a')

t=sp.Symbol('t')

x=a*(sp.cos(t)**3)

y=a*(sp.sin(t)**3)

d=sp.diff(y,t)/sp.diff(x,t)

print。原參數(shù)方程導(dǎo)數(shù)結(jié)果為:%s,%d)

d=sp.simplify(d)

prinlf原參數(shù)方程導(dǎo)數(shù)化簡為:%s,%d)

a=l

各章習(xí)題答案與提示-27-

t=np.arange(0,2*np.pi,0.01)

x=a*(np.cos⑴**3)

y=a*(np.sin(t)**3)

plt.plot(x,y)

plt.title('x=a(cos⑴**3),y=a(sin(t)**3)')

ax=plt.gca()

ax.spines['right'].set_color('none')

ax.spines「lop'].set_color('none')

ax.spines['bottom'].set_position((,data,,0))

ax.spines['left'].set_position(('data',0))

plt.axis('equar)

plt.show()

則輸出相應(yīng)的結(jié)果為:

原參數(shù)方程導(dǎo)數(shù)結(jié)果為:-sin(t)/cos(t);

原參數(shù)方程導(dǎo)數(shù)化簡為:-tan⑴

函數(shù)圖形如圖4.24所示:

圖4.24

3.輸入

importmatplotlib.pyplotaspit

importnumpyasnp

importsympyassp

frommpl_toolkits.mplot3dimportAxes3D

x,y=sp.symbols('xy')

-28-各章習(xí)題答案與提示

z=sp.exp(y)+x*y-sp.exp(1)

d="sp.diff(z,x)/sp.diff(z,y)

prinlC原方程導(dǎo)數(shù)為:%s,%d)

x=np.arange(-3,3,0.05)

y=np.arange(-3,3,0.05)

x,y=np.meshgrid(x,y)

z=np.exp(y)+x*y-np.exp(1)

ax=Axes3D(plt.flgure())

plt.rcParams['font.sans-serif]=['SimHei1]

plt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論