2-2-5 練習:變量和賦值運算符

練習:賦值和修改變量

現在該你來使用變量了呜投。這道練習的注釋(以 # 開頭的行)提供了創(chuàng)建和修改變量的說明。請在每條注釋后面根據說明寫一行代碼激率。

注意姻成,這段代碼使用了科學記數法來定義很大的數字也祠。4.445e8 等于 4.445 * 10 ** 8昙楚,也就是 444500000.0

# The current volume of a water reservoir (in cubic metres)
reservoir_volume = 4.445e8
# The amount of rainfall from a storm (in cubic metres)
rainfall = 5e6

# decrease the rainfall variable by 10% to account for runoff
rainfall = 5e6*0.9

# add the rainfall variable to the reservoir_volume variable
reservoir_volume = reservoir_volume + rainfall

# increase reservoir_volume by 5% to account for stormwater that flows
# into the reservoir in the days following the storm
reservoir_volume = reservoir_volume*1.05

# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume = reservoir_volume*0.95

# subtract 2.5e5 cubic metres from reservoir_volume to account for water
# that's piped to arid regions.
reservoir_volume = reservoir_volume - 2.5e5

# print the new value of the reservoir_volume variable
print(reservoir_volume)

更改變量

更改變量會如何影響到用該變量定義的另一個變量诈嘿?我們來看一個示例堪旧。

這是關于山景城人口和人口密度的原始數據。

>>> mv_population = 74728
>>> mv_area = 11.995
>>> mv_density = mv_population/mv_area

現在我們重新定義 mv_population 變量:

注意:后續(xù)代碼跟在上面三行代碼后面奖亚,而非重新開始)

>>> mv_population = 75000

習題 2/3

思考一下上方的代碼淳梦,下面的表達式輸出會是什么?

>>> print(int(mv_density))    

  • <input type="radio" readonly="" class="index-module--radio--J18uO" id="ureact-radio-bpz7dzls" value="on" style="box-sizing: border-box; margin: 0px 0.5em 0px 0px; font-family: inherit; font-size: inherit; line-height: inherit; overflow: visible; padding: 0px; left: -999em; outline: none; position: absolute;"> <label for="ureact-radio-bpz7dzls" class="index-module--label--2OxfH" style="box-sizing: inherit; display: flex; font-weight: 400; user-select: none; align-items: center; color: rgb(46, 61, 73); font-size: 14px; padding-left: 1.5em; vertical-align: middle; cursor: pointer;">6252.60525219</label>

  • <input type="radio" readonly="" class="index-module--radio--J18uO" id="ureact-radio-3i2z60yj" value="on" style="box-sizing: border-box; margin: 0px 0.5em 0px 0px; font-family: inherit; font-size: inherit; line-height: inherit; overflow: visible; padding: 0px; left: -999em; outline: none; position: absolute;"> <label for="ureact-radio-3i2z60yj" class="index-module--label--2OxfH" style="box-sizing: inherit; display: flex; font-weight: 400; user-select: none; align-items: center; color: rgb(46, 61, 73); font-size: 14px; padding-left: 1.5em; vertical-align: middle; cursor: pointer;">6229.92913714</label>

  • <input type="radio" readonly="" class="index-module--radio--J18uO" id="ureact-radio-ldqbn5y7" value="on" style="box-sizing: border-box; margin: 0px 0.5em 0px 0px; font-family: inherit; font-size: inherit; line-height: inherit; overflow: visible; padding: 0px; left: -999em; outline: none; position: absolute;"> <label for="ureact-radio-ldqbn5y7" class="index-module--label--2OxfH" style="box-sizing: inherit; display: flex; font-weight: 400; user-select: none; align-items: center; color: rgb(46, 61, 73); font-size: 14px; padding-left: 1.5em; vertical-align: middle; cursor: pointer;">6229</label>

  • 6246.92823723

  • <input type="radio" readonly="" class="index-module--radio--J18uO" id="ureact-radio-4i3xr4a3" value="on" style="box-sizing: border-box; margin: 0px 0.5em 0px 0px; font-family: inherit; font-size: inherit; line-height: inherit; overflow: visible; padding: 0px; left: -999em; outline: none; position: absolute;"> <label for="ureact-radio-4i3xr4a3" class="index-module--label--2OxfH" style="box-sizing: inherit; display: flex; font-weight: 400; user-select: none; align-items: center; color: rgb(46, 61, 73); font-size: 14px; padding-left: 1.5em; vertical-align: middle; cursor: pointer;">以上都不對昔字。</label>

習題 3/3

這是美國的州列表爆袍,按照加入聯邦的日期排序。假設你想為特拉華州創(chuàng)建一個變量并賦一個值作郭,表示它首先加入了聯邦陨囊。以下哪些項是有效的 Python 變量名和賦值?

  • <input type="checkbox" readonly="" class="index-module--checkbox--3fDuX" id="ureact-checkbox-00b5byoq" value="on" style="box-sizing: border-box; margin: 0px 0.5em 0px 0px; font-family: inherit; font-size: inherit; line-height: inherit; overflow: visible; padding: 0px; flex: 0 0 auto; height: 16px; outline: none; width: 16px; cursor: pointer;"> <label for="ureact-checkbox-00b5byoq" class="index-module--label--2YzyD" style="box-sizing: inherit; display: inline-block; font-weight: 400; user-select: none; font-size: 14px; margin-left: 0.5em; cursor: pointer;">del = 1</label>

  • delaware = 1

  • <input type="checkbox" readonly="" class="index-module--checkbox--3fDuX" id="ureact-checkbox-1lywko7c" value="on" style="box-sizing: border-box; margin: 0px 0.5em 0px 0px; font-family: inherit; font-size: inherit; line-height: inherit; overflow: visible; padding: 0px; flex: 0 0 auto; height: 16px; outline: none; width: 16px; cursor: pointer;"> <label for="ureact-checkbox-1lywko7c" class="index-module--label--2YzyD" style="box-sizing: inherit; display: inline-block; font-weight: 400; user-select: none; font-size: 14px; margin-left: 0.5em; cursor: pointer;">1 de = first</label>

  • de = 1

好好學習夹攒,天天向上

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末蜘醋,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子咏尝,更是在濱河造成了極大的恐慌压语,老刑警劉巖啸罢,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異胎食,居然都是意外死亡扰才,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門厕怜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來训桶,“玉大人,你說我怎么就攤上這事酣倾《娼遥” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵躁锡,是天一觀的道長午绳。 經常有香客問我,道長映之,這世上最難降的妖魔是什么拦焚? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮杠输,結果婚禮上赎败,老公的妹妹穿的比我還像新娘。我一直安慰自己蠢甲,他們只是感情好僵刮,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鹦牛,像睡著了一般搞糕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上曼追,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天窍仰,我揣著相機與錄音,去河邊找鬼礼殊。 笑死驹吮,一個胖子當著我的面吹牛,可吹牛的內容都是我干的晶伦。 我是一名探鬼主播碟狞,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼坝辫!你這毒婦竟也來了篷就?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎竭业,沒想到半個月后智润,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡未辆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年窟绷,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咐柜。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡兼蜈,死狀恐怖,靈堂內的尸體忽然破棺而出拙友,到底是詐尸還是另有隱情为狸,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布遗契,位于F島的核電站辐棒,受9級特大地震影響,放射性物質發(fā)生泄漏牍蜂。R本人自食惡果不足惜漾根,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鲫竞。 院中可真熱鬧辐怕,春花似錦、人聲如沸从绘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽顶考。三九已至赁还,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間驹沿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工蹈胡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留渊季,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓罚渐,卻偏偏與公主長得像却汉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子荷并,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內容