PYTHON核心編程第二版第2章習(xí)題答案_第1頁
PYTHON核心編程第二版第2章習(xí)題答案_第2頁
PYTHON核心編程第二版第2章習(xí)題答案_第3頁
PYTHON核心編程第二版第2章習(xí)題答案_第4頁
PYTHON核心編程第二版第2章習(xí)題答案_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、2-1.變量, print 和字符串格式化操作符。啟動交互式解釋器,給一些變量賦值(字符串,數(shù)值等)并通過輸入變量名顯示他們的值。再用print 語句做同樣的事。這兩者有何區(qū)別?也嘗試著使用字符串格式操作符%,多做幾次,慢慢熟悉它。答案:對于一個字符串,在僅使用變量名時,輸出的字符串是用單引號括起來了的。這是為了讓非字符串對象也能以字符串的方式顯示在屏幕上,即它顯示的是該對象的字符串表示,而不僅僅是字符串本身。如果使用print 命令,能使輸出更加友好。2-2.程序輸出。閱讀下面的python 腳本。#!/usr/bin/envpython1 + 2 * 4(a)你認(rèn)為這段腳本是用來做什么的?

2、(b)你認(rèn)為這段腳本會輸出什么?(c)輸入以上代碼,并保存為腳本,然后運(yùn)行它,它所做的與你的預(yù)期一樣嗎?為什么一樣/不一樣?(d)這段代碼單獨(dú)執(zhí)行和在交互解釋器中執(zhí)行有何不同?試一下,然后寫出結(jié)果。(e)如何改進(jìn)這個腳本,以便它能和你想象的一樣工作?答案:(a)計算(b)輸出 9(c)不一樣,不會有輸出(d)在交互解釋器中可以輸出9(e)需添加一個print,即#!/usr/bin/envpythonprint 1 + 2 * 42-3.數(shù)值和操作符。 啟動交互解釋器,使用 python 對兩個數(shù)值 (任意類型) 進(jìn)行加、 減、乘、除運(yùn)算。然后使用取余操作符來得到兩個數(shù)相除的余數(shù),最后使用乘方

3、操作符求a 數(shù)的 b次方。答案:當(dāng)使用 x/y 形式進(jìn)行除法運(yùn)算時,如果x 和 y 都是整形,那么運(yùn)算的結(jié)果就是運(yùn)算的整數(shù)部分。print 10 / 33如果 x 和 y 中有一個是浮點(diǎn)數(shù),那么會進(jìn)行精確除法。print 10 / 3.03.33333333333所謂地板除,采用x/y 的形式,得到不大于結(jié)果的最大整數(shù)值,這個運(yùn)算時與操作數(shù)無關(guān)的。1/201.0/20.0-1/2.0-1.02-4. 使用 raw_input() 函數(shù)得到用戶輸入。(a)創(chuàng)建一段腳本使用raw_input() 內(nèi)建函數(shù)從用戶輸入得到一個字符串,然后顯示這個用戶剛剛鍵入的字符串。(b)添加一段類似的代碼,不過這次

4、輸入的是數(shù)值。將輸入數(shù)據(jù)轉(zhuǎn)換為一個數(shù)值對象,(使用 int() 或其他數(shù)值轉(zhuǎn)換函數(shù))并將這個值顯示給用戶看(注意,如果你用的是早于1.5 的版本,你需要使用string.ato*() 函數(shù)執(zhí)行這種轉(zhuǎn)換)。答案:(a)a = raw_input(please input a string: )please input a string: hello worldprint ahello world(b)a = raw_input(please input a number: )please input a number: 123print type(a)a = int(raw_input(plea

5、seinput a number: )please input a number: 123print type(a)2-5.循環(huán)和數(shù)字。分別使用while 和 for 創(chuàng)建一個循環(huán)。(a)寫一個while 循環(huán),輸出整型為010(要確保是010,而不是 09 或 110)。(b)做同( a)一樣的事,不過這次使用range()內(nèi)建函數(shù)。答案:(a)i = 0while i for i in range(0,11):print i,0 1 2 3 45 6 7 8 9 102-6.條件判斷。判斷一個數(shù)是正數(shù)還是負(fù)數(shù),或者是0。開始先用固定的數(shù)值,然后修改你的代碼支持用戶輸入數(shù)值再進(jìn)行判斷。答案:

6、a = int(raw_input(please input a number: )if a 0:print the number is positive.elif a 0:print the number is negative.else:print the number is zero.2-7.循環(huán)和字串。從用戶那里接受一個字符串輸入,然后逐字符顯示該字符串。先用while循環(huán)實(shí)現(xiàn),然后再用for 循環(huán)實(shí)現(xiàn)。for 循環(huán)a = raw_input(please input astring: )for i in a:print i,while 循環(huán)a = raw_input(please i

7、nput astring: )i = 0while i len(a):print ai,i = i + 12-8.循環(huán)和操作符。創(chuàng)建一個包含五個固定數(shù)值的列表或元組,輸出他們的和。然后修改你的代碼為接受用戶輸入數(shù)值。分別使用while 和 for 循環(huán)實(shí)現(xiàn)。l = 1,2,3,4,5total = 0for i in l:total = total + iprint total is,totall = 1,2,3,4,5i = 0total = 0while i len(l):total = total + lii = i + 1print total is,total2-9.循環(huán)和操作符。創(chuàng)

8、建一個包含五個固定數(shù)值的列表或元組,輸出他們的平均值。本練習(xí)的難點(diǎn)之一是通過除法得到平均值。你會發(fā)現(xiàn)整型除會截去小數(shù),因此你必須使用浮點(diǎn)除以得到更精確的結(jié)果。float()內(nèi)建函數(shù)可以幫助你實(shí)現(xiàn)這一功能。答案:total = 0for i in range(0, 5):print please input number %d %(i + 1)a= float(raw_input()total = total + aprint the averageis, total / 5-i = 0total = 0while i = 1 and adir()_builtins_, _doc_, _name

9、_, _package_dirtype(dir)dir._doc_dir(object)- list of stringsnnif called without an argument, return the names in the currentscope.nelse, return an alphabetized list of names comprising (some of) the attributesnofthegiven object, and of attributes reachable from it.nif the object supplies a method n

10、amed _dir_,it will be used; otherwisenthedefault dir() logic is used and returns:nfor a module object: themodules attributes.nfor a classobject:its attributes, and recursively the attributesnofits bases.nfor any other object: its attributes, its classsattributes, andnrecursively theattributes of its

11、 classsbase classes.print dir._doc_dir(object) - list of stringsif called without an argument, return the names in the current scope.else,return an alphabetized list of names comprising (some of) the attributesof the given object, and of attributes reachable from it.if the object supplies a method n

12、amed _dir_, it will be used; otherwisethe default dir() logic is used and returns:for a module object: the modules attributes.for a classobject:its attributes, and recursively the attributesof its bases.for any other object: its attributes, its classsattributes, andrecursively the attributes of its

13、classsbase classes.2-13.利用 dir()找出 sys模塊中更多的東西。(a)啟動 python 交互式解釋器,執(zhí)行dir()函數(shù)。然后鍵入import sys以導(dǎo)入 sys模塊。再次執(zhí)行 dir()函數(shù)以確認(rèn)sys模塊被正確導(dǎo)入。然后執(zhí)行dir(sys),你就可以看到sys模塊的所有屬性了。(b)顯示 sys模塊的版本號屬性及平臺變量。記住在屬性名前一定要加sys.,這表示這個屬性是 sys模塊的。 其中 version 變量保存著你使用的python 解釋器版本,platform 屬性則包含你運(yùn)行 python 時使用的計算機(jī)平臺信息。(c)最后,調(diào)用sys.exit

14、()函數(shù)。這是一種熱鍵之外的另一種推出python 解釋器的方式。python 2.7.11 (v2.7.11:6d1b6a68f775, dec5 2015, 20:32:19) mscv.1500 32 bit (intel) onwin32typecopyright, credits or license() for more information.dir()_builtins_, _doc_, _name_, _package_import sysdir()_builtins_, _doc_, _name_, _package_, sysdir(sys)_displayhook_,

15、_doc_, _excepthook_, _name_, _package_, _stderr_,_stdin_, _stdout_, _clear_type_cache, _current_frames, _getframe, _mercurial,api_version, argv, builtin_module_names,byteorder, call_tracing, callstats, copyright,displayhook, dllhandle, dont_write_bytecode,exc_clear, exc_info, exc_traceback,exc_type,

16、 exc_value, excepthook, exec_prefix, executable, exit, flags, float_info,float_repr_style,getcheckinterval, getdefaultencoding, getfilesystemencoding, getprofile,getrecursionlimit,getrefcount, getsizeof, gettrace, getwindowsversion, hexversion,long_info, maxint, maxsize, maxunicode, meta_path, modul

17、es, path, path_hooks,path_importer_cache,platform, prefix, py3kwarning, setcheckinterval, setprofile,setrecursionlimit, settrace, stderr, stdin, stdout, subversion, version, version_info,warnoptions, winversys.version2.7.11 (v2.7.11:6d1b6a68f775, dec5 2015, 20:32:19) msc v.150032 bit (intel)sys.exit()2-14.操作符優(yōu)先級和括號分組。重寫2.4 小節(jié)中 print 語句里的算術(shù)表達(dá)式,試著在這個表達(dá)式中添加合適的括號以便它能正常工作。答案:略2-15.元素排序。(a)讓用戶輸入三個數(shù)值并分別將它們報存到3 個不同的變量中。不使用列表或排序算法,自己寫代碼來對三個數(shù)由小到大排序。(b)修改( a)的解決方案,使之從大到小排序。答案:(a)a = int(raw_input(inputnumber a: )b = int(raw_input(inputnumber b: )c = int(raw_input(inputnumber c: )if

溫馨提示

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

最新文檔

評論

0/150

提交評論