




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、2-1.變量, print 和字符串格式化操作符。啟動交互式解釋器,給一些變量賦值(字符串,數值等)并通過輸入變量名顯示他們的值。再用print 語句做同樣的事。這兩者有何區(qū)別?也嘗試著使用字符串格式操作符%,多做幾次,慢慢熟悉它。答案:對于一個字符串,在僅使用變量名時,輸出的字符串是用單引號括起來了的。這是為了讓非字符串對象也能以字符串的方式顯示在屏幕上,即它顯示的是該對象的字符串表示,而不僅僅是字符串本身。如果使用print 命令,能使輸出更加友好。2-2.程序輸出。閱讀下面的python 腳本。#!/usr/bin/envpython1 + 2 * 4(a)你認為這段腳本是用來做什么的?
2、(b)你認為這段腳本會輸出什么?(c)輸入以上代碼,并保存為腳本,然后運行它,它所做的與你的預期一樣嗎?為什么一樣/不一樣?(d)這段代碼單獨執(zhí)行和在交互解釋器中執(zhí)行有何不同?試一下,然后寫出結果。(e)如何改進這個腳本,以便它能和你想象的一樣工作?答案:(a)計算(b)輸出 9(c)不一樣,不會有輸出(d)在交互解釋器中可以輸出9(e)需添加一個print,即#!/usr/bin/envpythonprint 1 + 2 * 42-3.數值和操作符。 啟動交互解釋器,使用 python 對兩個數值 (任意類型) 進行加、 減、乘、除運算。然后使用取余操作符來得到兩個數相除的余數,最后使用乘方
3、操作符求a 數的 b次方。答案:當使用 x/y 形式進行除法運算時,如果x 和 y 都是整形,那么運算的結果就是運算的整數部分。print 10 / 33如果 x 和 y 中有一個是浮點數,那么會進行精確除法。print 10 / 3.03.33333333333所謂地板除,采用x/y 的形式,得到不大于結果的最大整數值,這個運算時與操作數無關的。1/201.0/20.0-1/2.0-1.02-4. 使用 raw_input() 函數得到用戶輸入。(a)創(chuàng)建一段腳本使用raw_input() 內建函數從用戶輸入得到一個字符串,然后顯示這個用戶剛剛鍵入的字符串。(b)添加一段類似的代碼,不過這次
4、輸入的是數值。將輸入數據轉換為一個數值對象,(使用 int() 或其他數值轉換函數)并將這個值顯示給用戶看(注意,如果你用的是早于1.5 的版本,你需要使用string.ato*() 函數執(zhí)行這種轉換)。答案:(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)和數字。分別使用while 和 for 創(chuàng)建一個循環(huán)。(a)寫一個while 循環(huán),輸出整型為010(要確保是010,而不是 09 或 110)。(b)做同( a)一樣的事,不過這次使用range()內建函數。答案:(a)i = 0while i for i in range(0,11):print i,0 1 2 3 45 6 7 8 9 102-6.條件判斷。判斷一個數是正數還是負數,或者是0。開始先用固定的數值,然后修改你的代碼支持用戶輸入數值再進行判斷。答案:
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)實現,然后再用for 循環(huá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)建一個包含五個固定數值的列表或元組,輸出他們的和。然后修改你的代碼為接受用戶輸入數值。分別使用while 和 for 循環(huá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、建一個包含五個固定數值的列表或元組,輸出他們的平均值。本練習的難點之一是通過除法得到平均值。你會發(fā)現整型除會截去小數,因此你必須使用浮點除以得到更精確的結果。float()內建函數可以幫助你實現這一功能。答案: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()函數。然后鍵入import sys以導入 sys模塊。再次執(zhí)行 dir()函數以確認sys模塊被正確導入。然后執(zhí)行dir(sys),你就可以看到sys模塊的所有屬性了。(b)顯示 sys模塊的版本號屬性及平臺變量。記住在屬性名前一定要加sys.,這表示這個屬性是 sys模塊的。 其中 version 變量保存著你使用的python 解釋器版本,platform 屬性則包含你運行 python 時使用的計算機平臺信息。(c)最后,調用sys.exit
14、()函數。這是一種熱鍵之外的另一種推出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 語句里的算術表達式,試著在這個表達式中添加合適的括號以便它能正常工作。答案:略2-15.元素排序。(a)讓用戶輸入三個數值并分別將它們報存到3 個不同的變量中。不使用列表或排序算法,自己寫代碼來對三個數由小到大排序。(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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 3.3 汽化和液化 說課稿 2025年初中人教版物理八年級上冊
- 開學心理講座心得體會
- 在戰(zhàn)友聚會上的致詞簡短
- 通信行業(yè)市場研究報告
- 市級體育總會章程范本
- 2025年高鎳锍項目建議書
- 《會計信息系統(tǒng)應用》課件 學習情境4 報表系統(tǒng)應用
- 《電子商務基礎》課件-話題1 電子商務概述
- 軟件開發(fā)項目居間協議模板
- 商業(yè)零售實體店數字化轉型與升級策略設計
- 小學教師專業(yè)發(fā)展與教學質量提升
- 大跨度空間網架結構分階段整體提升安裝技術研究與應用
- 注射用頭孢比羅酯鈉-臨床藥品應用解讀
- 農業(yè)領域的服務禮儀
- 大學生心理健康教育教程 課件 第二章 大學生自我意識
- 公證知識宣傳材料
- 聚酯生產技術 聚酯主要設備介紹
- 鈑金結構件點檢表
- 醫(yī)療安全(不良)事件匯總登記表(科室)
- 電子商務專升本考試(習題卷6)
- 鑄造企業(yè)采購流程及管理制度
評論
0/150
提交評論