Python基础(五)


函数和代码复用

函数的定义和使用

函数的理解与定义

函数的使用及调用

函数参数的传递

可选参数的传递

def <函数名>(<非可选参数>,<可选参数>):
    <函数体>
    return <返回值>
def f(n,m=1):
    return n//m

可变参数传递

def <函数名>(<参数>,*b):
    <函数体>
    return <返回值>
def f(n,*b):
    s=1
    for i in range(1,n+1):
        s*=i
    for item in b:
        s*=item
    return s       

函数的返回值

return可以传递0个返回值,也可以传递任意多个返回值

.png)

局部变量和全局变量

在函数内部引用全局变量的时候要用global

n,s=10,100
def f(n):
    global s
    for i in range(1,n+1):
        s*=i
    return s
print(f(n),s)

组合数据类型,如果局部变量未真实创建,则是全局变量

.png)

.png)

lambda函数

.png)

实例:七段数码管绘制

import turtle
import time
def drawGap():
    turtle.penup()
    turtle.fd(5)
def drawLine(draw):
    drawGap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawGap()
    turtle.right(90)
def drawDigit(digit):
    drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)
    drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
    drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
    drawLine(True) if digit in [0,2,6,8] else drawLine(False)
    turtle.left(90)
    drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
    drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
    drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)
def drawDate(date):
    turtle.pencolor("purple")
    for i in date:
        if i=='-':
            turtle.write('年',font=("Arial",18,"normal"))
            turtle.pencolor("green")
            turtle.fd(40)
        elif i=='=':
            turtle.write('月', font=("Arial", 18, "normal"))
            turtle.pencolor("blue")
            turtle.fd(40)
        elif i=='+':
            turtle.write('日', font=("Arial", 18, "normal"))
        else:
            drawDigit(eval(i))
def main():
    turtle.setup(800,350,200,200)
    turtle.penup()
    turtle.fd(-350)
    turtle.pensize(5)
    drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
    turtle.hideturtle()
    turtle.done()
main()

代码复用与函数递归

汉诺塔1:

cnt=0
def han(n,src,dst,mid):
    global cnt
    if n==1:
        print("{}:{}->{}".format(n,src,dst))
        cnt+=1
    else:
        han(n-1,src,mid,dst)
        print("{}:{}->{}".format(n,src,dst))
        cnt+=1
        han(n-1,mid,dst,src)
han(3,"A","B","C")
print(cnt)

pyinstaller库的使用

作用:将.py源代码转换成无需源代码的可执行文件

安装:

pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/

使用:

pyinstaller -F name.py

pyinstaller -i curve.ico -F name.py

科赫雪花

科赫曲线绘制:

#KochDrawV1.py
import turtle
def koch(size, n):
    if n == 0:
        turtle.fd(size)
    else:
        for angle in [0, 60, -120, 60]:
           turtle.left(angle)
           koch(size/3, n-1)
def main():
    turtle.setup(800,400)
    turtle.penup()
    turtle.goto(-300, -50)
    turtle.pendown()
    turtle.pensize(2)
    koch(600,3)     # 0阶科赫曲线长度,阶数
    turtle.hideturtle()
main()

科赫雪花绘制:

#KochDrawV2.py
import turtle
def koch(size, n):
    if n == 0:
        turtle.fd(size)
    else:
        for angle in [0, 60, -120, 60]:
           turtle.left(angle)
           koch(size/3, n-1)
def main():
    turtle.setup(600,600)
    turtle.penup()
    turtle.goto(-200, 100)
    turtle.pendown()
    turtle.pensize(2)
    level = 3      # 3阶科赫雪花,阶数
    koch(400,level)     
    turtle.right(120)
    koch(400,level)
    turtle.right(120)
    koch(400,level)
    turtle.hideturtle()
main()

参考资料

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248249

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248250

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248251

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248252

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248253

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248254

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248255

https://www.icourse163.org/learn/BIT-268001?tid=1207014257#/learn/content?type=detail&id=1212094145&cid=1215248256


文章作者: Amonologue
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Amonologue !
  目录