Lecture 3

A. while loop

1. ? Need a loop variable

? ? – Initialized outside loop

? ? – Changes within loop

? ? – Test for termination depends on variable

? ?? Useful to think about a decrementing function

? ?– Maps set of program variables into an integer

? ?– When loop is entered, value is non-negative

? ?– When value is <= 0, loop terminates, and

? ?– Value is decreased every time through loop

? ?eg: abs(x) – ans**3 (smaller the solution, nearer we will done.)

2. different between branching program and looping program:

? ? Branching structures (conditionals) let us jump?to different pieces of code based on a test

? – Programs are constant time

? ? Looping structures (e.g., while) let us repeat?pieces of code until a condition is satisfied

? – Programs now take time that depends on values?of variables, as well as length of program

3. some command

a.

what does these stand for

b. If the command break is executed within a loop, it halts evaluation of the loop at that point and passes control to the next expression.

c. stop infinite loop : control C

B. for loop: ? ?for <identifier> in <sequence>:

? ? ? ? ? ? ? ? ? ? ? ? ? ?<code block>

1. some logics:

? Identifier bound to first value in sequence

? Code block executed

? Identifier bound to next value

? Code block executed

? Continues until sequence exhausted or a break?statement is executed

? To generate a sequence of integers, use

– range(n) = [0, 1, 2, 3, …, n-1]

– range(m,n) = [m, m+1, …, n-1]

PS: spaces in strings as characters too!

2. range function:


some examples

more info: https://docs.python.org/3/library/functions.html#range

3. in range, 'num' variable will assign new value. (In python you can?assign?new values to variables. In this case the for loop will iterate in the list [0, 1, 2, 3, 4], each time the for loop iterates from the list it will assign num a new value.)

4. 5%2=1 5/2=2 5.0/2=2.5

5. if no indent, continue execute following if?


6. Approximation Methods

? Suppose we now want to find the square root?of any non-negative number?

? Can’t guarantee exact answer, but just look?for something close enough

? Start with exhaustive enumeration

– Take small steps to generate guesses in order

– Check to see if close enough

?general method:

-Exhaustive enumeration

– Bisection search

– Newton-Raphson (for root finding)

7. Print on the same line: use comma

# 習題中出現(xiàn)的問題:

a. 多次重復的部分可以命名成變量

b. 盡量避免在循環(huán)判斷當中有重復惫皱。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖仔涩,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件扑馁,死亡現(xiàn)場離奇詭異蹲堂,居然都是意外死亡搁料,警方通過查閱死者的電腦和手機或详,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來郭计,“玉大人霸琴,你說我怎么就攤上這事≌焉欤” “怎么了梧乘?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長庐杨。 經常有香客問我选调,道長,這世上最難降的妖魔是什么灵份? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任仁堪,我火速辦了婚禮,結果婚禮上填渠,老公的妹妹穿的比我還像新娘弦聂。我一直安慰自己,他們只是感情好氛什,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布莺葫。 她就那樣靜靜地躺著,像睡著了一般枪眉。 火紅的嫁衣襯著肌膚如雪捺檬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天贸铜,我揣著相機與錄音堡纬,去河邊找鬼。 笑死萨脑,一個胖子當著我的面吹牛隐轩,可吹牛的內容都是我干的。 我是一名探鬼主播渤早,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼职车,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鹊杖?” 一聲冷哼從身側響起悴灵,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎骂蓖,沒想到半個月后积瞒,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡登下,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年茫孔,在試婚紗的時候發(fā)現(xiàn)自己被綠了叮喳。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡缰贝,死狀恐怖馍悟,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情剩晴,我是刑警寧澤锣咒,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站赞弥,受9級特大地震影響毅整,放射性物質發(fā)生泄漏。R本人自食惡果不足惜绽左,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一悼嫉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧妇菱,春花似錦承粤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至房交,卻和暖如春彻舰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背候味。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工刃唤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人白群。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓尚胞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親帜慢。 傳聞我的和親對象是個殘疾皇子笼裳,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,471評論 5 6
  • 人生的旅途是一次又一次選擇的疊加,就像十字路口一樣粱玲,有多個抉擇躬柬,任一選擇都沒有客觀上的好與壞,只是適合不適合自己而...
    櫻花夢落閱讀 217評論 0 0
  • 我終于到了成都抽减。 不知道什么時候開始喜歡瑜伽的允青,一開始只是單純的練,后來喜歡上了練瑜伽的人卵沉,再后來就是自己喜歡上了...
    二月周閱讀 189評論 0 0
  • 從事人力資源接近18年颠锉,喜歡學習法牲,喜歡整理 ,喜歡分享木柬,一直致力于個人成長突破方向皆串!很開心通過兩年的專業(yè)教練學習,...
    夏翚Shari閱讀 322評論 0 4
  • 今天來談談我的一個同事眉枕。她今年二十八九歲,兩個孩子的媽媽怜森,在我們公司做總務的工作速挑。 工作上是個比較粗心且隨便的人。...
    玲夏ling閱讀 123評論 0 0