第一单元 Python程序设计基础

第1课 初识Python

P4 输出语句

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#e6c07b">print</span>(<span style="color:#98c379">"欢迎来到精彩的Python世界!"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"Hello, Python World!"</span>)
</code></span></span>

P5 打招呼

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em># 我的第一个程序</em></span>
name=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入你的姓名"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"你好,"</span>,name)
</code></span></span>

P6 随堂练习2

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#e6c07b">print</span>(<span style="color:#98c379">"2+3="</span>,<span style="color:#d19a66">2</span>+<span style="color:#d19a66">3</span>)
</code></span></span>

结果为2+3= 5

P6 随堂练习3

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">name=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入你的姓名:"</span>)
age=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入你的年龄:"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"大家好,我叫"</span>,name,<span style="color:#98c379">",今年"</span>,age,<span style="color:#98c379">"岁。"</span>)
</code></span></span>

第2课 常用数据类型

P8 字符串索引

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>st=<span style="color:#98c379">"Python"</span>
<span style="color:#61aeee">>>> </span><span style="color:#e6c07b">print</span>(st[<span style="color:#d19a66">0</span>],st[<span style="color:#d19a66">1</span>],st[<span style="color:#d19a66">2</span>],st[<span style="color:#d19a66">3</span>],st[<span style="color:#d19a66">4</span>],st[<span style="color:#d19a66">5</span>])
</code></span></span>

结果为P y t h o n

P9 列表索引

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>lst=[<span style="color:#d19a66">571</span>,<span style="color:#98c379">"杭州"</span>,<span style="color:#d19a66">572</span>,<span style="color:#98c379">"湖州"</span>,<span style="color:#d19a66">573</span>,<span style="color:#98c379">"嘉兴"</span>]
<span style="color:#61aeee">>>> </span><span style="color:#e6c07b">print</span>(lst[<span style="color:#d19a66">0</span>],lst[<span style="color:#d19a66">1</span>],lst[<span style="color:#d19a66">2</span>],lst[<span style="color:#d19a66">3</span>],lst[<span style="color:#d19a66">4</span>],lst[<span style="color:#d19a66">5</span>])
</code></span></span>

结果为571 杭州 572 湖州 573 嘉兴

P9 切片

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>lst=[<span style="color:#98c379">'HUAWEI'</span>,<span style="color:#98c379">'OPPO'</span>,<span style="color:#98c379">'VIVO'</span>,<span style="color:#98c379">'XIAOMI'</span>]
<span style="color:#61aeee">>>> </span>lst[<span style="color:#d19a66">1</span>:<span style="color:#d19a66">3</span>]
</code></span></span>

结果为['OPPO', 'VIVO']

P9 开动脑筋

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>lst=[<span style="color:#98c379">'HUAWEI'</span>,<span style="color:#98c379">'OPPO'</span>,<span style="color:#98c379">'VIVO'</span>,<span style="color:#98c379">'XIAOMI'</span>]
<span style="color:#61aeee">>>> </span>lst[<span style="color:#d19a66">0</span>][<span style="color:#d19a66">1</span>]
</code></span></span>

结果为'U'

P9 日积月累
st="Python",则st[-1]返回值是'n',st[1]返回值是'y'。
lst=['HUAWEI','OPPO','VIVO','XIAOMI'],则lst[-3:-1]返回值是['OPPO','VIVO']。

P10 三角形周长

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">a,b,c=<span style="color:#d19a66">3</span>,<span style="color:#d19a66">4</span>,<span style="color:#d19a66">5</span>
msg=<span style="color:#98c379">"三角形周长是"</span>
p=a+b+c
<span style="color:#e6c07b">print</span>(msg,p)
</code></span></span>

P11 随堂练习1

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>a,b=<span style="color:#d19a66">3</span>,<span style="color:#98c379">"Hello"</span>
<span style="color:#61aeee">>>> </span><span style="color:#e6c07b">type</span>(a),<span style="color:#e6c07b">type</span>(b)
</code></span></span>

结果为(<class 'int'>, <class 'str'>)

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#e6c07b">len</span>(b)
</code></span></span>

结果为5

P11 随堂练习2

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>Mphone=[<span style="color:#98c379">'HUAWEI'</span>,<span style="color:#98c379">'OPPO'</span>,<span style="color:#98c379">'VIVO'</span>,<span style="color:#98c379">'XIAOMI'</span>]
<span style="color:#61aeee">>>> </span>_____________
</code></span></span>

输出结果为['OPPO'],空处应填Mphone[1:2]

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>_____________
</code></span></span>

输出结果为['OPPO','XIAOMI'],空处应填Mphone[1:3]

第3课 算数运算符和表达式

P13 运算符示例

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#d19a66">5</span>+<span style="color:#d19a66">30</span>*<span style="color:#d19a66">20</span>/<span style="color:#d19a66">10</span>
</code></span></span>

结果为65.0

P13 亲身体验

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#d19a66">3</span>+<span style="color:#d19a66">5.0</span>
<span style="color:#61aeee">>>> </span><span style="color:#d19a66">9</span>**<span style="color:#d19a66">0.5</span>
<span style="color:#61aeee">>>> </span><span style="color:#d19a66">123</span>%<span style="color:#d19a66">10</span>
</code></span></span>

结果分别为
8.0
3.0
3

P15 华氏温度转摄氏温度

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">F=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入华氏温度:"</span>))
C=(<span style="color:#d19a66">5</span>/<span style="color:#d19a66">9</span>)*(F-<span style="color:#d19a66">32</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"对应的摄氏温度为:"</span>,C)
</code></span></span>

P15 fomat函数保留2位小数

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span> x=<span style="color:#d19a66">26.666666666668</span>
<span style="color:#61aeee">>>> </span> <span style="color:#e6c07b">print</span>(<span style="color:#e6c07b">format</span>(x,<span style="color:#98c379">'.2f'</span>))
</code></span></span>

结果为26.67

P16 随堂练习1 3位数的每一位

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">x=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">'输入3位数的自然数:'</span>))
a=x//<span style="color:#d19a66">100</span>              <span style="color:#5c6370"><em>#百位上的数字</em></span>
_________             <span style="color:#5c6370"><em>#十位上的数字</em></span>
c=x%<span style="color:#d19a66">10</span>                <span style="color:#5c6370"><em>#个位上的数字</em></span>
<span style="color:#e6c07b">print</span>(a,b,c,sep=<span style="color:#98c379">','</span>)
</code></span></span>

空处应填b=x//10%10或b=x%100//10等

P16 随堂练习2 摄氏温度转华氏温度

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">C=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入摄氏温度:"</span>))
F=<span style="color:#d19a66">9</span>*C/<span style="color:#d19a66">5</span>+<span style="color:#d19a66">32</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"对应的华氏温度为:"</span>,F)
</code></span></span>

第二单元 Python程序基本结构

第4课 顺序结构程序设计

P20 海伦公式求三角形面积

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输入三角形的三边长</em></span>
a=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"输入三角形的第一边长:"</span>))
b=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"输入三角形的第二边长:"</span>))
c=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"输入三角形的第三边长:"</span>))

<span style="color:#5c6370"><em>#计算三角形的半周长</em></span>
p = (a+b+c)/<span style="color:#d19a66">2.0</span>

<span style="color:#5c6370"><em>#计算三角形的面积</em></span>
s=(p*(p-a)*(p-b)*(p-c))**<span style="color:#d19a66">0.5</span>

<span style="color:#5c6370"><em>#输出三角形的面积</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三角形面积为:"</span>,s)
</code></span></span>

P21 随堂练习求圆的面积、周长

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">R=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入圆的半径R:"</span>))
PI=<span style="color:#d19a66">3.14</span>
S=________
L=________
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"圆的周长为:"</span>,S,<span style="color:#98c379">"圆的面积为:"</span>,L)
</code></span></span>

空处应填PI*R*R和2*PI*R以及其他等价形式

第5课 分支结构设计

P23 两个数较大

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">if</span> a>=b:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"两个数中较大为:"</span>,a)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"两个数中较大为:"</span>,b)
</code></span></span>

P23 亲身体验,两个数较小

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">if</span> a<=b:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"两个数中较小为:"</span>,a)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"两个数中较小为:"</span>,b)
</code></span></span>

P25 输入账号和密码,校验是否正确

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em># 输入账号和密码</em></span>
user=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入账号:"</span>)
pwd=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入密码:"</span>)

<span style="color:#5c6370"><em># 判断输入的账号和密码是否正确</em></span>
<span style="color:#c678dd">if</span> user==<span style="color:#98c379">"admin"</span> <span style="color:#c678dd">and</span> pwd==<span style="color:#98c379">"Python@16"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"登陆成功!"</span>)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"账号密码有误!"</span>)
</code></span></span>

P26 随堂练习1 出租车收费

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输入路程</em></span>
s=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入行驶路程:"</span>))
<span style="color:#5c6370"><em>#向上取整</em></span>
<span style="color:#c678dd">if</span> <span style="color:#e6c07b">int</span>(s)!=s:
    s=<span style="color:#e6c07b">round</span>(s+<span style="color:#d19a66">0.5</span>)
<span style="color:#5c6370"><em>#计算金额</em></span>
<span style="color:#c678dd">if</span> s<=<span style="color:#d19a66">3</span>:
    m=<span style="color:#d19a66">10</span>
<span style="color:#c678dd">else</span>:
    __________
<span style="color:#e6c07b">print</span>(s,<span style="color:#98c379">"千米"</span>,<span style="color:#98c379">"付费"</span>,m,<span style="color:#98c379">"元"</span>)
</code></span></span>

空处应填m=10+(s-3)*2
P26 随堂练习2 判断三角形是否成立

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输入三条边长</em></span>
a=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"输入三角形第一边长:"</span>))
b=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"输入三角形第二边长:"</span>))
c=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"输入三角形第三边长:"</span>))
<span style="color:#5c6370"><em>#若构成三角形、则求三角形面积</em></span>
<span style="color:#c678dd">if</span> _____________
    p = (a+b+c)/<span style="color:#d19a66">2.0</span>
    s=(p*(p-a)*(p-b)*(p-c))**<span style="color:#d19a66">0.5</span>
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三角形面积为:"</span>,s)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"输入的三条边,不能构成三角形!"</span>)
</code></span></span>

空处应填a+b>=c and a+c>=b and b+c>=a:

第6课 多分支结构程序设计

P28 三个数的最大值

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">if</span> a>=b <span style="color:#c678dd">and</span> a>=c:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三个数中最大数为:"</span>,a)
<span style="color:#c678dd">elif</span> b>=c:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三个数中最大数为:"</span>,b)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三个数中最大数为:"</span>,c)
</code></span></span>

P29 亲身体验 三个数的最小值

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">if</span> a<=b <span style="color:#c678dd">and</span> a<=c:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三个数中最小数为:"</span>,a)
<span style="color:#c678dd">elif</span> b<=c:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三个数中最小数为:"</span>,b)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"三个数中最小数为:"</span>,c)
</code></span></span>

P29 开动脑筋 三个数最大值的其他解法

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">max_value=a
<span style="color:#c678dd">if</span> b>max_value:
    max_value=b
<span style="color:#c678dd">if</span> c>max_value:
    max_value=c
<span style="color:#e6c07b">print</span>(max_value)
</code></span></span>

P30 算术四则运算的简易计算器

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em># 简易计算器(四则运算)</em></span>
x=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入第一个运算数:"</span>))
y=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入第二个运算数:"</span>))
op=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入算术运算符(+-*/):"</span>)
c=<span style="color:#d19a66">0</span>
<span style="color:#c678dd">if</span> op==<span style="color:#98c379">'+'</span>:
    c=x+y
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"运算结果为:"</span>,c)
<span style="color:#c678dd">elif</span> op==<span style="color:#98c379">'-'</span>:
    c=x-y
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"运算结果为:"</span>,c)
<span style="color:#c678dd">elif</span> op==<span style="color:#98c379">'*'</span>:
    c=x*y
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"运算结果为:"</span>,c)
<span style="color:#c678dd">elif</span> op==<span style="color:#98c379">'/'</span>:
    <span style="color:#c678dd">try</span>:
        c=x/y
        <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"运算结果为:"</span>,c)
    <span style="color:#c678dd">except</span>:
        <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"除数不能为零!"</span>)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"运算符输入有误!"</span>)
</code></span></span>

P31 随堂练习 运动会成绩名次
使用多分支语句

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输入名次</em></span>
mc=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"参加运动会项目名次(1~6):"</span>))
<span style="color:#5c6370"><em>#计算并输出得分</em></span>
<span style="color:#c678dd">if</span> mc==<span style="color:#d19a66">1</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"得分为:7分"</span>)
<span style="color:#c678dd">elif</span> mc==<span style="color:#d19a66">2</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"得分为:5分"</span>)
<span style="color:#c678dd">elif</span> mc==<span style="color:#d19a66">3</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"得分为:4分"</span>)
<span style="color:#c678dd">elif</span> mc==<span style="color:#d19a66">4</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"得分为:3分"</span>)
<span style="color:#c678dd">elif</span> mc==<span style="color:#d19a66">5</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"得分为:2分"</span>)
<span style="color:#c678dd">elif</span> mc==<span style="color:#d19a66">6</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"得分为:1分"</span>)
_______
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"输入的名次不在1~6范围!"</span>)
</code></span></span>

空处应填else:

使用列表

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输入名次</em></span>
mc=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"参加运动会项目名次(1~6):"</span>))
<span style="color:#5c6370"><em>#利用列表,计算得分</em></span>
lst=[<span style="color:#d19a66">7</span>,<span style="color:#d19a66">5</span>,<span style="color:#d19a66">4</span>,<span style="color:#d19a66">3</span>,<span style="color:#d19a66">2</span>,<span style="color:#d19a66">1</span>]
<span style="color:#c678dd">if</span> mc>=<span style="color:#d19a66">1</span> <span style="color:#c678dd">and</span> mc<=<span style="color:#d19a66">6</span>:
    __________
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"项目得分为:"</span>,df)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"输入的名次不在1~6范围!"</span>)
</code></span></span>

空处应填df=lst[mc-1]

第7课 多分支结构的应用实例

P34 根据车牌首字母识别汽车所在地

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#汽车牌号所在地查询</em></span>
chepai= <span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入汽车牌号首字母:"</span>)
<span style="color:#c678dd">if</span> chepai==<span style="color:#98c379">"A"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:杭州市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"B"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:宁波市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"C"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:温州市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"D"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:绍兴市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"E"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:湖州市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"F"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:嘉兴市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"G"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:金华市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"H"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:衢州市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"J"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:台州市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"K"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:丽水市"</span>)
<span style="color:#c678dd">elif</span> chepai==<span style="color:#98c379">"L"</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"所在地:舟山市"</span>)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"无法查询"</span>)
</code></span></span>

P35 开动脑筋 要求不区分大小写

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#将小写转换为大写</em></span>
<span style="color:#c678dd">if</span> chepai>=<span style="color:#98c379">'a'</span> <span style="color:#c678dd">and</span> chepai<=<span style="color:#98c379">'z'</span>:
    chepai=<span style="color:#e6c07b">chr</span>(<span style="color:#e6c07b">ord</span>(chepai)-<span style="color:#e6c07b">ord</span>(<span style="color:#98c379">'a'</span>)+<span style="color:#e6c07b">ord</span>(<span style="color:#98c379">'A'</span>)) 

<span style="color:#5c6370"><em>#首字母是列表第二个元素</em></span>
chepai= chepai[<span style="color:#d19a66">1</span>]
</code></span></span>

P35 随堂练习 空气质量指数查询

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">AQI = <span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入空气指数值:"</span>))
<span style="color:#c678dd">if</span> AQI<=<span style="color:#d19a66">50</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"空气质量优,各类人群可户外正常活动"</span>)
<span style="color:#c678dd">elif</span> AQI<=<span style="color:#d19a66">100</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"空气质量良,少数敏感人群减少户外运动"</span>)
<span style="color:#c678dd">elif</span> AQI<=<span style="color:#d19a66">150</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"空气轻度污染,减少长时间户外运动"</span>)
<span style="color:#c678dd">elif</span> AQI<=<span style="color:#d19a66">200</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"空气中度污染,应适量减少户外运动"</span>)
<span style="color:#c678dd">elif</span> AQI<=<span style="color:#d19a66">300</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"空气重度污染,一般人减少户外活动"</span>)
<span style="color:#c678dd">else</span>:
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"空气严重污染,一般人避免户外活动"</span>)
</code></span></span>

第8课 循环结构--for循环

P37 for循环遍历字符串

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输出字符串中的各个字符</em></span>
s=<span style="color:#98c379">"Python3"</span>
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> s:
    <span style="color:#e6c07b">print</span>(i)
</code></span></span>

P38 开动脑筋 for循环输出["apple","banana","peach"]

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输出列表中各个元素</em></span>
lst=[<span style="color:#98c379">"apple"</span>,<span style="color:#98c379">"banana"</span>,<span style="color:#98c379">"peach"</span>]
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> lst:
    <span style="color:#e6c07b">print</span>(i)
</code></span></span>

P38 求1到n中的奇数的和

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#e6c07b">sum</span>=<span style="color:#d19a66">0</span>
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">1</span>,<span style="color:#d19a66">101</span>,<span style="color:#d19a66">2</span>):
    <span style="color:#e6c07b">sum</span>=<span style="color:#e6c07b">sum</span>+i
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"1~100的所有奇数的和:"</span>,<span style="color:#e6c07b">sum</span>)
</code></span></span>

P38 输出列表中的各个元素

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#输出列表中各个元素</em></span>
lst=[<span style="color:#98c379">"apple"</span>,<span style="color:#98c379">"banana"</span>,<span style="color:#98c379">"peach"</span>]
n=<span style="color:#e6c07b">len</span>(lst)
<span style="color:#c678dd">for</span> index <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(n):
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"当前水果:"</span>,lst[index])
</code></span></span>

P39 亲身体验 利用len()函数输出"Python3"各个字符

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">s=<span style="color:#98c379">"Python3"</span>
n=<span style="color:#e6c07b">len</span>(s)
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(n):
    <span style="color:#e6c07b">print</span>(s[i])
</code></span></span>

P39 随堂练习1

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#通过成员资格运算符输出列表元素</em></span>
program=[<span style="color:#98c379">"Java"</span>,<span style="color:#98c379">"Python"</span>,<span style="color:#98c379">"Visual Basic"</span>]
<span style="color:#c678dd">for</span> w <span style="color:#c678dd">in</span> program:
    <span style="color:#e6c07b">print</span>(w,<span style="color:#e6c07b">len</span>(w))
</code></span></span>

输出应填
Java 4
Python 6
Visual Basic 12

P39 随堂练习2

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#通过索引遍历输出列表元素</em></span>
program=[<span style="color:#98c379">"Java"</span>,<span style="color:#98c379">"Python"</span>,<span style="color:#98c379">"Visual Basic"</span>]
n=<span style="color:#e6c07b">len</span>(program)
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(n):
    <span style="color:#e6c07b">print</span>(i,program[i]) 
</code></span></span>

输出应填
0 Java
1 Python
2 Visual Basic

第9课 for循环的应用实例

P42 数麦粒

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#n表示棋盘的格子数</em></span>
n=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入棋盘格子数:"</span>))
p=<span style="color:#d19a66">1</span>     <span style="color:#5c6370"><em>#第1格麦粒数p置初值1</em></span>
s=<span style="color:#d19a66">0</span>     <span style="color:#5c6370"><em>#累加器s初始化为0</em></span>
<span style="color:#5c6370"><em>#通过for语句循环累加,并输出麦粒总数</em></span>
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">1</span>,n+<span style="color:#d19a66">1</span>):
    s=s+p
    p=p*<span style="color:#d19a66">2</span>
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"当前格子序号:"</span>,i,<span style="color:#98c379">",当前格子麦粒数:"</span>,<span style="color:#e6c07b">int</span>(p/<span style="color:#d19a66">2</span>))
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"棋盘格子数为:"</span>,n,<span style="color:#98c379">"麦粒总数:"</span>,s)
</code></span></span>

P43 利用麦粒数估算重量

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">s=(s*<span style="color:#d19a66">0.03</span>)/(<span style="color:#d19a66">1000</span>*<span style="color:#d19a66">1000</span>)
</code></span></span>

P44 寻找水仙花数

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">lst=[]
<span style="color:#c678dd">for</span> __________
    a=i%<span style="color:#d19a66">10</span>
    b=(i//<span style="color:#d19a66">10</span>)%<span style="color:#d19a66">10</span>
    c=i//<span style="color:#d19a66">100</span>
    <span style="color:#c678dd">if</span> (a**<span style="color:#d19a66">3</span>+b**<span style="color:#d19a66">3</span>+c**<span style="color:#d19a66">3</span>)==i:
        lst.append(i)
<span style="color:#e6c07b">print</span>(lst)
</code></span></span>

空处应填i in range(100,1000,1):

第10课 循环结构--while循环

P46 多个数求和

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#计数器和累加器置初值0</em></span>
s=<span style="color:#d19a66">0</span>
i=<span style="color:#d19a66">0</span>
<span style="color:#5c6370"><em>#输入需循环计算的数,并给出结束循环的条件</em></span>
xstr=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入数【直接按回车则】:"</span>)
<span style="color:#c678dd">while</span> xstr!=<span style="color:#98c379">""</span>:
    x=<span style="color:#e6c07b">float</span>(xstr)
    s=s+x
    i=i+<span style="color:#d19a66">1</span>
    xstr=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入数【直接按回车则】:"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"\n输入个数为:"</span>,i)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"输入和为:"</span>,<span style="color:#e6c07b">format</span>(s,<span style="color:#98c379">".2f"</span>))
</code></span></span>

P47 开动脑筋,输入"quit"退出循环

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">while</span> xstr!=<span style="color:#98c379">"quit"</span>:
</code></span></span>

P47 登录程序

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">user=<span style="color:#98c379">""</span>
psd=<span style="color:#98c379">""</span>
<span style="color:#c678dd">while</span> <span style="color:#56b6c2">True</span>:
    <span style="color:#c678dd">if</span> user!=<span style="color:#98c379">"admin"</span>:
        user=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入账号:"</span>)
        <span style="color:#c678dd">continue</span>
    psw=<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入密码:"</span>)
    <span style="color:#c678dd">if</span> psw==<span style="color:#98c379">"Python@16"</span>:
        <span style="color:#c678dd">break</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"账号密码正确,登陆成功!"</span>)
</code></span></span>

P38 随堂练习1 阅读程序

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">x=<span style="color:#d19a66">0</span>
<span style="color:#c678dd">while</span> x<<span style="color:#d19a66">50</span>:
    x=(x+<span style="color:#d19a66">2</span>)*(x+<span style="color:#d19a66">3</span>)
<span style="color:#e6c07b">print</span>(x)
</code></span></span>

输出结果为72
P38 随堂练习2 阅读程序

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">x=<span style="color:#d19a66">0</span>
<span style="color:#c678dd">while</span> x<<span style="color:#d19a66">10</span>:
    x=x+<span style="color:#d19a66">1</span>
    <span style="color:#c678dd">if</span> x%<span style="color:#d19a66">2</span>==<span style="color:#d19a66">0</span>:
        <span style="color:#c678dd">continue</span>
    <span style="color:#e6c07b">print</span>(x)
</code></span></span>

输出结果为
1
3
5
7
9

第11课 while循环的应用实例

P51 猜数游戏

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">num=<span style="color:#d19a66">66</span>
i=<span style="color:#d19a66">0</span>
<span style="color:#c678dd">while</span> <span style="color:#56b6c2">True</span>:
    guess=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入猜测的数:"</span>))
    <span style="color:#c678dd">if</span> guess<num:
        <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"偏小"</span>)
        i=i+<span style="color:#d19a66">1</span>
    <span style="color:#c678dd">elif</span> guess>num:
        <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"偏大"</span>)
        i=i+<span style="color:#d19a66">1</span>
    <span style="color:#c678dd">else</span>:
        <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"正确,共猜测次数:"</span>,i+<span style="color:#d19a66">1</span>)
        <span style="color:#c678dd">break</span>
</code></span></span>

P51 随堂练习 储蓄问题

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#储蓄年数n置初值0</em></span>
n=<span style="color:#d19a66">0</span>
<span style="color:#5c6370"><em>#p是本金金额</em></span>
p=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">'请输入本金金额:'</span>))
<span style="color:#5c6370"><em>#q是预期数额</em></span>
q=<span style="color:#e6c07b">float</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">'请输入预期金额:'</span>))

<span style="color:#c678dd">while</span> __________:
    p=p+p*<span style="color:#d19a66">0.03</span>
    n+=<span style="color:#d19a66">1</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"需要储蓄(年数):"</span>,n)
</code></span></span>

空处应填p<q或q>p

第三单元 算法与程序设计

第12课 Python的函数

P55 内建len()函数

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span>st=<span style="color:#98c379">"Python"</span>
<span style="color:#61aeee">>>> </span><span style="color:#e6c07b">len</span>(st)
</code></span></span>

输出结果为6

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">lst=[<span style="color:#98c379">'HUAWEI'</span>,<span style="color:#98c379">'OPPO'</span>,<span style="color:#98c379">'VIVO'</span>,<span style="color:#98c379">'XIAOMI'</span>]
<span style="color:#e6c07b">len</span>(lst)
</code></span></span>

输出结果为3

P56 调用函数打印姓名和年龄

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#定义printinfo函数</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">printinfo</span>(name,age):
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"Name:"</span>,name)
    <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"Age:"</span>,age)

<span style="color:#5c6370"><em>#调用printinfo函数</em></span>
printinfo(<span style="color:#98c379">"miki"</span>,<span style="color:#d19a66">50</span>)
printinfo(age=<span style="color:#d19a66">50</span>,name=<span style="color:#98c379">"miki"</span>)
</code></span></span>

输出结果为
Name: miki
Age: 50
Name: miki
Age: 50

P58 斐波那契数列的前n个元素

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#定义fib函数,n为形参</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">fib</span>(n):
    a,b=<span style="color:#d19a66">1</span>,<span style="color:#d19a66">1</span>
    <span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">3</span>,n+<span style="color:#d19a66">1</span>):
        a,b=b,a+b
    <span style="color:#c678dd">return</span> b

<span style="color:#5c6370"><em>#输入月数</em></span>
n=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">'请输入第n个月:'</span>))
<span style="color:#5c6370"><em>#调用fib函数,i值为实参</em></span>
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">1</span>,n+<span style="color:#d19a66">1</span>):
    <span style="color:#e6c07b">print</span>(fib(i),end=<span style="color:#98c379">' '</span>)
</code></span></span>

P59 随堂练习2 第几天

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#定义函数 fcount()</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">fcount</span> (year, month, day):
    <span style="color:#e6c07b">sum</span>=<span style="color:#d19a66">0</span>
    <span style="color:#5c6370"><em>#判断该年是否为闰年</em></span>
    <span style="color:#c678dd">if</span> year%<span style="color:#d19a66">400</span>==<span style="color:#d19a66">0</span> <span style="color:#c678dd">or</span> (year%<span style="color:#d19a66">4</span>==<span style="color:#d19a66">0</span> <span style="color:#c678dd">and</span> year%<span style="color:#d19a66">100</span>!=<span style="color:#d19a66">0</span>):
        lst=[<span style="color:#d19a66">31</span>,<span style="color:#d19a66">29</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>]
        <span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(month-<span style="color:#d19a66">1</span>):
            <span style="color:#e6c07b">sum</span>=<span style="color:#e6c07b">sum</span>+lst[i]
        <span style="color:#c678dd">return</span> <span style="color:#e6c07b">sum</span>+day
    <span style="color:#c678dd">else</span>:
        lst=[<span style="color:#d19a66">31</span>,<span style="color:#d19a66">28</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>,<span style="color:#d19a66">30</span>,<span style="color:#d19a66">31</span>]
        <span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(month-<span style="color:#d19a66">1</span>):
            <span style="color:#e6c07b">sum</span>=<span style="color:#e6c07b">sum</span>+lst[i]
        <span style="color:#c678dd">return</span> <span style="color:#e6c07b">sum</span>+day

<span style="color:#5c6370"><em>#输入年月日</em></span>
year=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入年份:"</span>))
month=<span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入月份:"</span>))
day=<span style="color:#e6c07b">int</span>( <span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入日期:"</span>))
<span style="color:#5c6370"><em># 调用函数fount(),并输出天数</em></span>
<span style="color:#e6c07b">sum</span>=_____________
<span style="color:#e6c07b">print</span>(year,<span style="color:#98c379">"年"</span>, month,<span style="color:#98c379">"月"</span>,day,<span style="color:#98c379">"日"</span>,<span style="color:#98c379">"是今年的第"</span>,<span style="color:#e6c07b">sum</span>,<span style="color:#98c379">"天!"</span>)
</code></span></span>

空处应填fcount(year, month, day)

第13课 Python的模块

P61 导入整个模块的文件

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#c678dd">import</span> math
<span style="color:#61aeee">>>> </span>math.sqrt(<span style="color:#d19a66">2</span>)
</code></span></span>

只导入模块中的指定函数

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">from</span> math <span style="color:#c678dd">import</span> <span style="color:#e6c07b">pow</span>
<span style="color:#e6c07b">pow</span>(<span style="color:#d19a66">2</span>,<span style="color:#d19a66">3</span>)
</code></span></span>

P64 利用蒙特卡罗方法计算π的值

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">import</span> random
<span style="color:#c678dd">import</span> math
num = <span style="color:#e6c07b">int</span>(<span style="color:#e6c07b">input</span>(<span style="color:#98c379">"请输入投点数:"</span>))
point = <span style="color:#d19a66">0</span>
<span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">1</span>, num+<span style="color:#d19a66">1</span>):
    x, y = random.random(), random.random()
    dist = math.sqrt(x ** <span style="color:#d19a66">2</span> + y ** <span style="color:#d19a66">2</span>)
    <span style="color:#c678dd">if</span> dist <= <span style="color:#d19a66">1.0</span>:
        point = point + <span style="color:#d19a66">1</span>
Pi = <span style="color:#d19a66">4.0</span> * (point/num)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"Pi值是:"</span>,Pi)
</code></span></span>

P65 随堂练习1 观察输出结果

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#c678dd">import</span> random
<span style="color:#61aeee">>>> </span><span style="color:#e6c07b">dir</span>(random)
<span style="color:#61aeee">>>> </span><span style="color:#e6c07b">help</span>(random)
<span style="color:#61aeee">>>> </span>random.random()
<span style="color:#61aeee">>>> </span>random.randint(<span style="color:#d19a66">1</span>,<span style="color:#d19a66">10</span>)
</code></span></span>

P65 随堂练习2 列表元素循环左移问题
定义函数left(),并保存到模版文件move.py

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em># 模块文件 move.py</em></span>
<span style="color:#5c6370"><em># 将列表循环左移K位</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">left</span>(lst,k):
    temp=lst
    <span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(k):
        temp.append(temp.pop(<span style="color:#d19a66">0</span>))
    <span style="color:#c678dd">return</span> temp
</code></span></span>

新建“列表元素的循环左移”文件lst_move.py,程序代码如下,请填写空缺的代码。

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">import</span> __________
p = [<span style="color:#d19a66">1</span>,<span style="color:#d19a66">2</span>,<span style="color:#d19a66">3</span>,<span style="color:#d19a66">4</span>,<span style="color:#d19a66">5</span>,<span style="color:#d19a66">6</span>]
n=<span style="color:#d19a66">3</span>
<span style="color:#e6c07b">print</span>(__________)
</code></span></span>

空处应分别填
move
move.left(p,3)

第14课 枚举算法

P70 百钱买百鸡

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">count=<span style="color:#d19a66">0</span> <span style="color:#5c6370"><em>#循环计数</em></span>
<span style="color:#c678dd">for</span> x <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">21</span>):
    <span style="color:#c678dd">for</span> y <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">34</span>):
        count = count + <span style="color:#d19a66">1</span>
        <span style="color:#c678dd">if</span> <span style="color:#d19a66">5</span>*x+<span style="color:#d19a66">3</span>*y+(<span style="color:#d19a66">100</span>-x-y)/<span style="color:#d19a66">3</span>==<span style="color:#d19a66">100</span>:
            <span style="color:#e6c07b">print</span>(<span style="color:#98c379">"公鸡:"</span>,x,<span style="color:#98c379">"母鸡:"</span>,y,<span style="color:#98c379">"小鸡:"</span>,<span style="color:#d19a66">100</span>-x-y)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"循环运行次数:"</span>,count)
</code></span></span>

P70 随堂练习1 零钱兑换

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python">count=<span style="color:#d19a66">0</span> <span style="color:#5c6370"><em>#循环计数</em></span>
<span style="color:#c678dd">for</span> x <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">21</span>):
    <span style="color:#c678dd">for</span> y <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">11</span>):
        sy=<span style="color:#d19a66">100</span>-(<span style="color:#d19a66">5</span>*x+<span style="color:#d19a66">10</span>*y)
        <span style="color:#c678dd">if</span> sy>=<span style="color:#d19a66">0</span> <span style="color:#c678dd">and</span> sy%<span style="color:#d19a66">20</span>==<span style="color:#d19a66">0</span>:
            count = count + <span style="color:#d19a66">1</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"兑换方法数:"</span>,count)
</code></span></span>

P71 随堂练习2 寻找完全数

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em># 初始化空列表</em></span>
lst = []
<span style="color:#c678dd">for</span> n <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">2</span>,<span style="color:#d19a66">10001</span>):
    s=<span style="color:#d19a66">0</span>
    <span style="color:#c678dd">for</span> i <span style="color:#c678dd">in</span> <span style="color:#e6c07b">range</span>(<span style="color:#d19a66">1</span>,n-<span style="color:#d19a66">1</span>):
        <span style="color:#c678dd">if</span> n%i==<span style="color:#d19a66">0</span>:
            _____________
    <span style="color:#c678dd">if</span> s==n:
        lst.append(n)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"2~10000中的完全数有:"</span>,lst)
</code></span></span>

空处应填s=s+i

第15课 Python的图形界面设计

P73 没有组件的GUI程序

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">import</span> tkinter <span style="color:#5c6370"><em>#导入tkinter模块</em></span>
win=tkinter.Tk() <span style="color:#5c6370"><em>#生成一个主窗体对象</em></span>
win.geometry(<span style="color:#98c379">"250x130"</span>)
win.title(<span style="color:#98c379">"没有组建的窗体"</span>)

win.mainloop() <span style="color:#5c6370"><em>#进入消息循环</em></span>
</code></span></span>

P73 "退出"按钮的程序代码

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">import</span> tkinter <span style="color:#c678dd">as</span> tk <span style="color:#5c6370"><em>#导入tkinter模块重名为tk</em></span>
win=tk.Tk() <span style="color:#5c6370"><em>#生成一个主窗体对象</em></span>
win.title(<span style="color:#98c379">"没有组建的窗体"</span>)
win.geometry(<span style="color:#98c379">"250x130"</span>) <span style="color:#5c6370"><em>#窗体大小</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">my_quit</span>():
    win.quit()
    <span style="color:#5c6370"><em># win.destroy()</em></span>

but_quit=tk.Button(win,text=<span style="color:#98c379">"退出"</span>,
        commad=my_quit(),width=<span style="color:#d19a66">10</span>,height=<span style="color:#d19a66">2</span>)

but_quit.pack()

win.mainloop() <span style="color:#5c6370"><em>#进入消息循环</em></span>
</code></span></span>

P76 tkinter实现登录

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#设计基本框架</em></span>
<span style="color:#c678dd">import</span> tkinter <span style="color:#c678dd">as</span> tk
<span style="color:#c678dd">import</span> tkinter.messagebox
win=tk.Tk()
win.title(<span style="color:#98c379">"用户登录"</span>)
win.geometry(<span style="color:#98c379">"250x130"</span>)

<span style="color:#5c6370"><em>#---功能代码开始---</em></span>
<span style="color:#5c6370"><em>#一设计功能函数--</em></span>
<span style="color:#5c6370"><em>#设置变量</em></span>
var_Name=tk.StringVar() <span style="color:#5c6370"><em>#设置变里为StringVar对象</em></span>
var_Name.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)   <span style="color:#5c6370"><em>#初始和重置时的清空</em></span>
var_Pwd=tk.StringVar()
var_Pwd.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)
<span style="color:#5c6370"><em># 按钮处理函数</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">login</span>():
    name=var_Name.get()  <span style="color:#5c6370"><em>#获取用户名</em></span>
    pwd=var_Pwd.get()  <span style="color:#5c6370"><em>#获取密码</em></span>
    <span style="color:#c678dd">if</span> name==<span style="color:#98c379">'admin'</span> <span style="color:#c678dd">and</span> pwd ==<span style="color:#98c379">'python@16'</span>:
        tk.messagebox.showinfo(title=<span style="color:#98c379">"用户登录"</span>, message=<span style="color:#98c379">"成功!"</span>)
    <span style="color:#c678dd">else</span>:
        tk.messagebox.showinfo(title=<span style="color:#98c379">"用户登录"</span>, message=<span style="color:#98c379">"失败!"</span>)

<span style="color:#c678dd">def</span> <span style="color:#61aeee">cancel</span>():
    var_Name.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)<span style="color:#5c6370"><em>#清空用户名</em></span>
    var_Pwd.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)<span style="color:#5c6370"><em>#清空密码</em></span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">quit</span>():
    win.quit()
    <span style="color:#5c6370"><em>#win.destroy()</em></span>

<span style="color:#5c6370"><em>#一登录窗口各组件设计一</em></span>
<span style="color:#5c6370"><em>#设计2个提示标签</em></span>
labname=tk.Label(win,text=<span style="color:#98c379">"账号"</span>,width=<span style="color:#d19a66">80</span>)
labpwd=tk.Label(win,text=<span style="color:#98c379">"密码"</span>,width=<span style="color:#d19a66">80</span>)
<span style="color:#5c6370"><em>#设计2个输入框( textvariable为文本框的值,并关联变里 var_Name)</em></span>
entname=tk.Entry(win, width=<span style="color:#d19a66">100</span>, textvariable=var_Name)
entpwd=tk.Entry(win, show=<span style="color:#98c379">'*'</span>, width=<span style="color:#d19a66">100</span>, textvariable=var_Pwd)
<span style="color:#5c6370"><em>#设计3个按钮</em></span>
but_Ok=tk.Button(win,text=<span style="color:#98c379">"登陆"</span>, command=login)
but_Cancel=tk.Button(win,text=<span style="color:#98c379">"重置"</span>, command=cancel)
but_quit=tk.Button(win,text=<span style="color:#98c379">"退出"</span>, command=quit)
<span style="color:#5c6370"><em>#---登录窗口各组件布局---</em></span>
<span style="color:#5c6370"><em>#组建的窗口布局</em></span>
labname.place(x=<span style="color:#d19a66">20</span>, y=<span style="color:#d19a66">10</span>, width=<span style="color:#d19a66">80</span>, height=<span style="color:#d19a66">20</span>)
labpwd.place(x=<span style="color:#d19a66">20</span>, y=<span style="color:#d19a66">40</span>, width=<span style="color:#d19a66">80</span>, height=<span style="color:#d19a66">20</span>)
entname.place(x=<span style="color:#d19a66">120</span>,y=<span style="color:#d19a66">10</span>,width=<span style="color:#d19a66">80</span>, height=<span style="color:#d19a66">20</span>)
entpwd.place(x=<span style="color:#d19a66">120</span>, y=<span style="color:#d19a66">40</span>, width=<span style="color:#d19a66">80</span>, height=<span style="color:#d19a66">20</span>)
but_Ok.place(x=<span style="color:#d19a66">30</span>, y=<span style="color:#d19a66">80</span>, width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)
but_Cancel.place(x=<span style="color:#d19a66">100</span>,y=<span style="color:#d19a66">80</span>,width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)
but_quit.place(x=<span style="color:#d19a66">170</span>, y=<span style="color:#d19a66">80</span>, width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)
win.mainloop()
</code></span></span>

P76 随堂练习 温度转换的tkinter程序

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#c678dd">from</span> tkinter <span style="color:#c678dd">import</span> *
<span style="color:#c678dd">from</span> tkinter <span style="color:#c678dd">import</span> messagebox

<span style="color:#c678dd">def</span> <span style="color:#61aeee">cancel</span>():
    swdVar.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)
    hwdVar.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)

<span style="color:#c678dd">def</span> <span style="color:#61aeee">is_number</span>(s):
    <span style="color:#c678dd">try</span>:
        <span style="color:#e6c07b">float</span>(s)
        <span style="color:#c678dd">return</span> <span style="color:#56b6c2">True</span>
    <span style="color:#c678dd">except</span> ValueError:
        <span style="color:#c678dd">return</span> <span style="color:#56b6c2">False</span>

<span style="color:#c678dd">def</span> <span style="color:#61aeee">FToC</span>():
    <span style="color:#c678dd">if</span> is_number(hwdVar.get()):
        <span style="color:#5c6370"><em># 华氏温度转化为摄氏温度</em></span>
        s = <span style="color:#d19a66">5</span> / <span style="color:#d19a66">9.0</span> * (<span style="color:#e6c07b">float</span>(hwdVar.get()) - <span style="color:#d19a66">32</span>)
        swdVar.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">'{:.2f}'</span>.<span style="color:#e6c07b">format</span>(s))
    <span style="color:#c678dd">else</span>:
        messagebox.showinfo(title=<span style="color:#98c379">"错误"</span>, message=<span style="color:#98c379">"请输入数字!"</span>)

<span style="color:#c678dd">def</span> <span style="color:#61aeee">CToF</span>():
    <span style="color:#c678dd">if</span> is_number(swdVar.get()):
        <span style="color:#5c6370"><em># 摄氏温度转换为华氏温度</em></span>
        h = <span style="color:#d19a66">9</span> / <span style="color:#d19a66">5.0</span> * <span style="color:#e6c07b">float</span>(swdVar.get()) + <span style="color:#d19a66">32</span>
        hwdVar.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">'{:.2f}'</span>.<span style="color:#e6c07b">format</span>(h))
    <span style="color:#c678dd">else</span>:
        messagebox.showinfo(title=<span style="color:#98c379">"错误"</span>, message=<span style="color:#98c379">"请输入数字!"</span>)
win = Tk()
win.title(<span style="color:#98c379">'温度转换'</span>)
win.geometry(<span style="color:#98c379">'250x150'</span>)

<span style="color:#5c6370"><em># 摄氏温度变量</em></span>
swdVar = StringVar()
lb1 = Label(win, text=<span style="color:#98c379">"摄氏温度C"</span>)
lb1.place(x=<span style="color:#d19a66">20</span>, y=<span style="color:#d19a66">10</span>, width=<span style="color:#d19a66">80</span>, height=<span style="color:#d19a66">20</span>)
swd = Entry(win, textvariable=swdVar)
swdVar.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)
swd.place(x=<span style="color:#d19a66">100</span>, y=<span style="color:#d19a66">10</span>, width=<span style="color:#d19a66">100</span>, height=<span style="color:#d19a66">20</span>)

<span style="color:#5c6370"><em># 华氏温度变量</em></span>
hwdVar = StringVar()
lb1 = Label(win, text=<span style="color:#98c379">"华氏温度C"</span>)
lb1.place(x=<span style="color:#d19a66">20</span>, y=<span style="color:#d19a66">50</span>, width=<span style="color:#d19a66">80</span>, height=<span style="color:#d19a66">20</span>)
hwd = Entry(win, textvariable=hwdVar)
hwdVar.<span style="color:#e6c07b">set</span>(<span style="color:#98c379">''</span>)
hwd.place(x=<span style="color:#d19a66">100</span>, y=<span style="color:#d19a66">50</span>, width=<span style="color:#d19a66">100</span>, height=<span style="color:#d19a66">20</span>)
but_FToC=Button(win,text=<span style="color:#98c379">"C转F"</span>, command=CToF)
but_FToC.place(x=<span style="color:#d19a66">50</span>, y=<span style="color:#d19a66">80</span>, width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)

but_Cancel=Button(win,text=<span style="color:#98c379">"重置"</span>, command=cancel)
but_Cancel.place(x=<span style="color:#d19a66">50</span>, y=<span style="color:#d19a66">110</span>, width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)

but_FToC=Button(win,text=<span style="color:#98c379">"F转C"</span>, command=FToC)
but_FToC.place(x=<span style="color:#d19a66">150</span>, y=<span style="color:#d19a66">80</span>, width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)
<span style="color:#5c6370"><em>#退出按钮</em></span>
but_quit=Button(win,text=<span style="color:#98c379">"退出"</span>, command=win.destroy)
but_quit.place(x=<span style="color:#d19a66">150</span>, y=<span style="color:#d19a66">110</span>, width=<span style="color:#d19a66">50</span>, height=<span style="color:#d19a66">20</span>)
win.mainloop()
</code></span></span>

P78 阅读材料 Python第三方库的使用
数值计算NumPy、图像处理PIL、2D图形绘制Matplotlib、数据分析pandas和Python的Web开发框架Django,对应的包名分别为numpy、pillow、matplotlib、pandas和django。
可以直接在命令行下pip install或者切换到Python安装目录下的scipts目录中执行命令

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-bash">> pip install pillow
</code></span></span>

如果下载太慢,可以加上-i Simple Index 使用清华的源来安装

使用import关键字来导入第三方库中的对象,PIL库包含21个与图像处理相关的对象,最常用的是Image和ImageFilter对象,可以按如下方式导入。

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#61aeee">>>> </span><span style="color:#c678dd">from</span> PIL <span style="color:#c678dd">import</span> Image
<span style="color:#61aeee">>>> </span><span style="color:#c678dd">from</span> PIL <span style="color:#c678dd">import</span> ImageFilter
</code></span></span>

将一张BMP图片生成其JPEG格式的缩略图

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#将一张图片,生成其缩略图</em></span>
<span style="color:#c678dd">from</span> PIL <span style="color:#c678dd">import</span> Image                 <span style="color:#5c6370"><em>#导入PIL库中的Image对象</em></span>
img = Image.<span style="color:#e6c07b">open</span>(<span style="color:#98c379">"丽江山水.bmp"</span>)       <span style="color:#5c6370"><em>#加载图像文件</em></span>
img.thumbnail((<span style="color:#d19a66">128</span>,<span style="color:#d19a66">128</span>))              <span style="color:#5c6370"><em>#创建文件的缩略图</em></span>
img.save(<span style="color:#98c379">"丽江山水缩略图.jpg"</span>,<span style="color:#98c379">"JPEG"</span>)   <span style="color:#5c6370"><em>#以JPEG格式保存图像文件</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"处理结束!"</span>)
</code></span></span>

将图片处理成浮雕效果

<span style="background-color:#282c34 !important"><span style="color:#303133"><code class="language-Python"><span style="color:#5c6370"><em>#将图片处理成浮雕效果</em></span>
<span style="color:#c678dd">from</span> PIL <span style="color:#c678dd">import</span> Image                 <span style="color:#5c6370"><em>#导入PIL库中的Image对象</em></span>
<span style="color:#c678dd">from</span> PIL <span style="color:#c678dd">import</span> ImageFilter           <span style="color:#5c6370"><em>#导入PIL库中的ImageFilter对象</em></span>
img = Image.<span style="color:#e6c07b">open</span>(<span style="color:#98c379">"丽江山水.bmp"</span>)       <span style="color:#5c6370"><em>#加载图像文件</em></span>
omg=img.<span style="color:#e6c07b">filter</span>(ImageFilter.EMBOSS)    <span style="color:#5c6370"><em>#生成图像的浮雕效果</em></span>
omg.save(<span style="color:#98c379">"丽江山水浮雕效果.bmp"</span>)         <span style="color:#5c6370"><em>#保存图像文件</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"处理结束!"</span>)</code></span></span>