Python--03--函数入门

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


函数入门

1. 函数的概念


内置函数:https://docs.python.org/zh-cn/3.13/library/functions.html
模块提供的函数:https://docs.python.org/zh-cn/3.13/py-modindex.html

2.基本使用





3.参数













4.返回值



5.全局作用域 VS 局部作用域



python 复制代码
# 全局作用域 与 局部作用域,以及global的使用
a = 100
b = 200

def test():
    c = '尚硅谷'
    d = '你好啊'
    global a
    a = 300
    print('函数中的打印(a)', a)
    print('函数中的打印(b)', b)
    print('函数中的打印(c)', c)
    print('函数中的打印(d)', d)
test()
print('***************')
print('全局的打印(a)', a)
print('全局的打印(b)', b)
print(c)
print(d)



6.嵌套调用

嵌套调用:在一个函数执行的过程中,调用了另外一个函数,例如下面的代码:

python 复制代码
# 函数嵌套调用测试1
def greet(name, msg):
    print(f'我叫{name},我想说的话在下面:')
    speak(msg)
    print('嗯,我想说的结束了')

def speak(msg):
    print('----------')
    print(msg)
    print('----------')

greet('张三', '你好啊')

# 函数嵌套调用测试2
def test1():
    print('进入 test1 函数')
    test2()
    print('退出 test1 函数')

def test2():
    print('进入 test2 函数')
    test3()
    print('退出 test2 函数')

def test3():
    print('进入 test3 函数')
    print('***正在执行 test3 函数')
    print('退出 test3 函数')

test1()

7.递归调用




8.函数说明文档


相关推荐
weixin_3954489117 分钟前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu22 分钟前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_7634724629 分钟前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤1 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板
Duang007_1 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
熊文豪1 小时前
金仓数据库如何以“多模融合“重塑文档数据库新范式
数据库·金仓数据库·电科金仓·mongodb迁移
霖霖总总1 小时前
[小技巧56]深入理解 MySQL 聚簇索引与非聚簇索引:原理、差异与实践
数据库·mysql
Dreamboat-L1 小时前
Redis及其两种持久化技术详解
数据库·redis·缓存
伐尘1 小时前
【MySQL】间隙锁 与 排他锁 的区别
数据库·mysql
浒畔居2 小时前
机器学习模型部署:将模型转化为Web API
jvm·数据库·python