練習:賦值和修改變量
現在該你來使用變量了呜投。這道練習的注釋(以 #
開頭的行)提供了創(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