引用
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)))
a 变量保存数据的内存地址是 140712361203648
在函数内部 10 对应的内存地址是 140712361203648
函数要返回数据的内存地址是 2397674202672
hello 的内存地址是 2397674202672
可变和不可变类型
a = [1, 2, 3]
print(id(a))
a.append(999)
print(a)
print(id(a))
a.remove(2)
print(a)
print(id(a))
a.clear()
print(a)
print(id(a))
a = []
print(id(a))
d = {"name": "xiaoming"}
print(d)
d["age"] = 18
print(d)
print(id(d))
d.pop("age")
print(d)
print(id(d))
d = {}
print(id(d))
2850887492864
[1, 2, 3, 999]
2850887492864
[1, 3, 999]
2850887492864
[]
2850887492864
2850887492928
{'name': 'xiaoming'}
{'name': 'xiaoming', 'age': 18}
2850887446592
{'name': 'xiaoming'}
2850887446592
2850887446656
局部变量和全局变量
局部变量
def demo1():
num = 10
print("在demo1函数内部的变量是 %d" % num)
def demo2():
num = 99
print("demo2 ==> %d" % num)
pass
demo1()
demo2()
demo1 ==> 10
demo2 ==> 99
修改全局变量
num = 10
def demo1():
global num
num = 99
print("demo1 ==> %d" % num)
def demo2():
print("demo2 ==> %d" % num)
demo1()
demo2()
demo1 ==> 99
demo2 ==> 99
全局变量的位置及命名
num = 10
title = "aixuexideamelia"
name = "amelia"
def demo():
print("%d" % num)
print("%s" % title)
print("%s" % name)
demo()
10
aixuexideamelia
amelia
函数返回类型为元组,处理办法
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)
测量开始...
测量结束...
(39, 50)
39
50
测量开始...
测量结束...
39
50
交换数字的方法–三种解法
a = 6
b = 100
a, b = b, a
print(a)
print(b)
100
6
在函数内部,针对参数使用赋值语句
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)
函数内部的代码
100
[1, 2, 3]
函数执行完成
99
[4, 5, 6]
函数中传递的参数为可变类型
def demo(num_list):
print("函数内部的代码")
num_list.append(9)
print(num_list)
print("函数执行完成")
gl_list = [1, 2, 3]
demo(gl_list)
print(gl_list)
函数内部的代码
[1, 2, 3, 9]
函数执行完成
[1, 2, 3, 9]
+=的使用
def demo(num, num_list):
print("函数开始")
num += num
num_list = 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)
函数开始
18
[1, 2, 3, 1, 2, 3]
函数完成
9
[1, 2, 3]
缺省参数
gl_list = [6, 3, 9]
gl_list.sort()
print("升序:", gl_list)
gl_list.sort(reverse=True)
print("降序:", gl_list)
升序: [3, 6, 9]
降序: [9, 6, 3]
指定函数的缺省参数
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("amelia", gender=False)
小明 是 男生
amelia 是 女生
缺省函数注意点
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("amelia", gender=False)
[]小明 是 男生
[]amelia 是 女生
多值参数
def demo(num, *nums, **person):
print(num)
print(nums)
print(person)
print("1.demo(1)")
demo(1)
print("-" * 50)
print("2.demo(1, 2, 3, 4, 5, name=\"小明\", age=18)")
demo(1, 2, 3, 4, 5, name="小明", age=18)
1.demo(1)
1
()
{}
--------------------------------------------------
2.demo(1, 2, 3, 4, 5, name="小明", age=18)
1
(2, 3, 4, 5)
{'name': '小明', 'age': 18}
多值参数求和
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)
(1, 2, 3, 4, 5)
15
元组变量和字典变量拆包
def demo(*args, **kwargs):
print(args)
print(kwargs)
gl_nums = (1, 2, 3)
gl_dict = {"name": "小明", "age": 18}
print("1.输出demo(gl_nums, gl_dict):")
demo(gl_nums, gl_dict)
print("2.输出demo(*gl_nums, **gl_dict):")
demo(*gl_nums, **gl_dict)
print("3.输出demo(1, 2, 3, name=\"小明\", age=18):")
demo(1, 2, 3, name="小明", age=18)
1.输出demo(gl_nums, gl_dict):
((1, 2, 3), {'name': '小明', 'age': 18})
{}
2.输出demo(*gl_nums, **gl_dict):
(1, 2, 3)
{'name': '小明', 'age': 18}
3.输出demo(1, 2, 3, name="小明", age=18):
(1, 2, 3)
{'name': '小明', 'age': 18}
递归
def sum_number(num):
print(num)
if num == 1:
return
sum_number(num - 1)
sum_number(3)
3
2
1
使用递归从1加到100
def sum_numbers(num):
if num == 1:
return 1
temp = sum_numbers(num - 1)
return num + temp
result = sum_numbers(100)
print(result)
5050
|