def 函数名(参数1,参数2,...):
"""
文档注释
"""
代码块1
代码块2
代码块2
return 返回值
# def:定义函数的关键字
# 函数名:是用来调用函数的
# 函数名的命名必须能反映出函数的功能
# 文档注释:描述该函数,来增强函数的可读性
# 代码块:函数的功能实现代码
# return:函数的返回值
有了函数之后,如需实现以下打印功能,我们可以使用函数来完成。
# print('========================')
# print('hello Albert')
# print('========================')
1 先定义
def print_sym(sym, count): # print_sym=<function print_msg at 0x000001B2A33698C8>
print(sym * count)
def print_msg(msg):
print('\033[045m%s\033[0m' % msg)
2 再调用(函数名加括号就是在调用函数)
print(print_sym) # 函数名对应一个内存地址
print_sym('#', 30)
print_msg('hello Albert')
print_sym('#', 30)
关于函数的参数,现在只需要知道定义的时候有几个参数,调用的时候就传入几个参数。