def message():
temp = 20
wetness = 30
# 如果想要返回多个值,可以使用元祖
# return (temp, wetness)
# 如果返回的类型是元祖,可以省略括号
return 20, 30
result = message()
print(result)
# 如果返回的是元祖,同时希望单独处理元祖中的数据,可以使用多个变量,依次接受函数的返回结果
print(result[0])
print(result[1])
gl_temp, gl_wetness = message()
print(gl_temp)
print(gl_wetness)
# 交换两个数值
a = 6
b = 10
a = a + b
b = a - b
a = a - b
print(a)
print(b)
# 交换两个值的第二种写法,利用元祖
a, b = (b, a)
print(a)
print(b)
# 交换两个值的第三种写法,利用元祖
a, b = b, a
print(a)
print(b)
def demo(num, num_list):
num += num
# 列表变量使用+不会做相加再赋值的操作
# num_list=num_list+num_list本质上是在调用列表的extend
# num_list += num_list
num_list.extend(num_list)
print("num:%d" % num)
print(num_list)
pass
num = 3
num_list = [1, 2, 3]
demo(num, num_list)
print(num)
print(num_list)
# 排序
gl_list = [3, 9, 4]
gl_list.sort()
print(gl_list)
gl_list.sort(reverse=True)
print(gl_list)
# 指定参数默认值,必须保证缺省参数在参数列表最后一个位置
def print_info(name, title="", gender=True):
genderText = "男生"
if not gender:
print("男生")
else:
print("女生")
print_info("小安", False)
print_info("小安")
print_info("小安", gender=False)
# 多值参数
def demo(num, *nums, **person):
print(num)
print(nums)
print(person)
demo(1, 2, 3, 4, 5, name="小明", age=12)
def subNumbers(*args):
num = 0
print(args)
for n in args:
num += n
return num
result = subNumbers(1, 2, 3)
print(result)
# 元祖和字典的拆包
def demos(*args, **kwargs):
print(args)
print(kwargs)
gl_num = (1, 2, 3)
gl_dicts = {"name": "小明", "age": 123}
# demos(gl_num, gl_dicts)
demos(1, 2, 3, name="小明", age=123)
# demos(*gl_num,**gl_dicts)
(20, 30)
20
30
20
30
10
6
6
10
10
6
num:6
[1, 2, 3, 1, 2, 3]
3
[1, 2, 3, 1, 2, 3]
[3, 4, 9]
[9, 4, 3]
女生
女生
男生
1
(2, 3, 4, 5)
{'name': '小明', 'age': 12}
(1, 2, 3)
6
(1, 2, 3)
{'name': '小明', 'age': 123}
网友评论