文章目录
- 1.引用
- 2.局部变量
- 3.全局变量
- 4.函数不能直接修改全局变量
- 5.修改全局变量
- 6.全局变量的位置
- 7.全局变量命名
- 8.多个返回值
- 9.交换数字
- 10.不可变和可变的参数
- 11.函数内部通过方法修改可变参数
- 12.加等于
- 13.函数的缺省参数
- 14.函数的缺省参数定义
- 15.缺省参数的注意点
- 16.多值参数
- 17.多值参数求和
- 18.元组和字典的拆包
- 19.递归函数的特点
- 20.递归求和
1.引用
def test(num):
print("在函数内部 %d 对应的内存地址是 %d" % (num, id(num)))
result = "hello"
print("函数要返回数据的内存地址是 %d" % id(result))
return result
a = 10
print("a 变量保存数据的内存地址是 %d" % id(a))
r = test(a)
print("%s 的内存地址是 %d" % (r, id(r)))
2.局部变量
def demo1():
num = 10
print("在demo1函数内部的变量是 %d" % num)
def demo2():
num = 99
print("demo2 ==> %d" % num)
pass
demo1()
demo2()
3.全局变量
num = 10
def demo1():
print("demo1 ==> %d" % num)
def demo2():
print("demo2 ==> %d" % num)
demo1()
demo2()
4.函数不能直接修改全局变量
num = 10
def demo1():
num = 99
print("demo1 ==> %d" % num)
def demo2():
print("demo2 ==> %d" % num)
demo1()
demo2()
5.修改全局变量
num = 10
def demo1():
global num
num = 99
print("demo1 ==> %d" % num)
def demo2():
print("demo2 ==> %d" % num)
demo1()
demo2()
6.全局变量的位置
num = 10
title = "黑马程序员"
name = "小明"
def demo():
print("%d" % num)
print("%s" % title)
print("%s" % name)
demo()
7.全局变量命名
gl_num = 10
gl_title = "黑马程序员"
gl_name = "小明"
def demo():
num = 99
print("%d" % num)
print("%s" % gl_title)
print("%s" % gl_name)
demo()
8.多个返回值
def measure():
"""测量温度和湿度"""
print("测量开始...")
temp = 39
wetness = 50
print("测量结束...")
return temp, wetness
result = measure()
print(result)
print(result[0])
print(result[1])
gl_temp, gl_wetness = measure()
print(gl_temp)
print(gl_wetness)
9.交换数字
a = 6
b = 100
a, b = b, a
print(a)
print(b)
10.不可变和可变的参数
def demo(num, num_list):
print("函数内部的代码")
num = 100
num_list = [1, 2, 3]
print(num)
print(num_list)
print("函数执行完成")
gl_num = 99
gl_list = [4, 5, 6]
demo(gl_num, gl_list)
print(gl_num)
print(gl_list)
11.函数内部通过方法修改可变参数
def demo(num_list):
print("函数内部的代码")
num_list.append(9)
print(num_list)
print("函数执行完成")
gl_list = [1, 2, 3]
demo(gl_list)
print(gl_list)
12.加等于
def demo(num, num_list):
print("函数开始")
num += num
num_list += num_list
print(num)
print(num_list)
print("函数完成")
gl_num = 9
gl_list = [1, 2, 3]
demo(gl_num, gl_list)
print(gl_num)
print(gl_list)
13.函数的缺省参数
gl_list = [6, 3, 9]
gl_list.sort(reverse=True)
print(gl_list)
14.函数的缺省参数定义
def print_info(name, gender=True):
"""
:param name: 班上同学的姓名
:param gender: True 男生 False 女生
"""
gender_text = "男生"
if not gender:
gender_text = "女生"
print("%s 是 %s" % (name, gender_text))
print_info("小明")
print_info("老王")
print_info("小美", False)
15.缺省参数的注意点
def print_info(name, title="", gender=True):
"""
:param title: 职位
:param name: 班上同学的姓名
:param gender: True 男生 False 女生
"""
gender_text = "男生"
if not gender:
gender_text = "女生"
print("[%s]%s 是 %s" % (title, name, gender_text))
print_info("小明")
print_info("老王")
print_info("小美", gender=False)
16.多值参数
def demo(num, *nums, **person):
print(num)
print(nums)
print(person)
demo(1, 2, 3, 4, 5, name="小明", age=18)
17.多值参数求和
def sum_numbers(*args):
num = 0
print(args)
for n in args:
num += n
return num
result = sum_numbers(1, 2, 3, 4, 5)
print(result)
18.元组和字典的拆包
def demo(*args, **kwargs):
print(args)
print(kwargs)
gl_nums = (1, 2, 3)
gl_dict = {"name": "小明", "age": 18}
demo(*gl_nums, **gl_dict)
demo(1, 2, 3, name="小明", age=18)
19.递归函数的特点
def sum_number(num):
print(num)
if num == 1:
return
sum_number(num - 1)
sum_number(3)
20.递归求和
def sum_numbers(num):
if num == 1:
return 1
temp = sum_numbers(num - 1)
return num + temp
result = sum_numbers(100)
print(result)