本文是对《北理工 嵩天/黄天宇/礼欣 Python语言程序设计》的学习笔记,供自己查阅使用。
降低编程难度和代码复用
def <函数名>(<参数(0个或多个)>):
<函数体>
return <返回值>
函数定义时,所指定的参数是一种占位符
函数定义后,如果不经过调用,不会被执行
函数定义时,参数是输入、函数体是处理、结果是输出(IPO)
调用是运行函数代码的方式
调用时要给出实际参数
实际参数替换定义中的参数
函数调用后得到返回值
函数可以有参数,也可以没有,但必须保留括号
def <函数名>():
<函数体>
return <返回值>
可选参数传递,可选参数需要放在非可选参数后面,并且赋初值 def fact(n,m=1) :
def <函数名>(<非可选参数>,<可选参数>):
<函数体>
return <返回值>
def fact(n,m=1) :
s = 1
for i in range(1, n+1):
s *= i
return s//m
函数定义时可以设计可变数量参数,既不确定参数总数量
def <函数名>(<参数>, *b):
<函数体>
return <返回值>
def fact(n, *b) :
s = 1
for i inrange(1, n+1):
s *= i
for item in b:
s *= item
return s
fact(10,3,5,8)
函数调用时,参数可以按照位置或名称方式传递
def fact(n,m=1) :
fact(10,5 ) # 位置传递
fact( m=5,n=10 ) # 名称传递
函数可以返回0个或多个结果
函数可以有返回值,也可以没有,可以有return,也可以没有
return可以传递0个返回值,也可以传递任意多个返回值
return s//m, n, m
>>>fact(10,5 )
(725760, 10, 5) # 元组
>>>a,b,c= fact(10,5)
>>>print(a,b,c)
725760 10 5
<函数名> = lambda <参数>:<表达式>
等价于
def <函数名>(<参数>):
<函数体>
return <返回值>
>>>f = lambda x, y : x + y
>>>f(10, 15)
25
>>>f = lambda: "lambda函数"
>>>print(f())
lambda函数
Python map() 函数
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
map(function, iterable, ...)
>>> def square(x) : # 计算平方数
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
<map object at 0x100d3d550> # 返回迭代器
>>> list(map(square, [1,2,3,4,5])) # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5])) # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
Python 2.x 返回列表。Python 3.x 返回迭代器。
import turtle, time
def drawLine(draw): # 绘制单段数码管
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
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): #data为日期,格式为'%Y-%m=%d+'
turtle.pencolor("red")
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(-300)
turtle.pensize(5)
drawDate(time.strftime('%Y-%m=%d+', time.gmtime()))
turtle.hideturtle()
turtle.done()
main()
函数定义中调用函数自身的方式
链条:计算过程存在递归链条
基例:存在一个或多个不需要再次递归的基例
递归是数学归纳法思维的编程体现
eg. 斐波那契数列、汉诺塔
(cmd命令行) pyinstaller -F <文件名.py>

pyinstaller –i curve.ico –F SevenDigitsDrawV2.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) # 3阶科赫曲线,阶数
turtle.hideturtle()
turtle.done()
main()