#!/usr/bin/python3 # coding=utf-8 #可以返回多个变量 def look_return(): a,b = 3,5 return a,b c,d = look_return() print(c, d) c, d = d, c print(c, d)
#!/usr/bin/python3 # coding=utf-8 #函数内部对形参直接赋值,不会影响外面的实参变量的值 def demo(num, num_list): print("函数内部") #赋值语句 print(‘-‘ * 50) print(id(num), id(num_list)) num = 200 num_list = [1, 2, 3] print(id(num), id(num_list)) print(num) print(num_list) print("函数代码完成") g1_num = 99 g1_list = [4, 5, 6] print(id(g1_num), id(g1_list)) print(id(g1_num), id(g1_list)) print(g1_num) print(g1_list)
#!/usr/bin/python3 # coding=utf-8 #num_list传入传出参数 def mutable(num_list): num_list += [1, 2, 3] print(id(num_list)) g1_list = [6, 7, 8] print(id(g1_list)) mutable(g1_list) print(id(g1_list)) print(g1_list)
#!/usr/bin/python3 # coding=utf-8 def demo(num): print("函数内部代码") print(id(num)) num += num print(id(num)) print(num) print("函数代码完成") g1_num = 9 print(id(g1_num)) demo(g1_num) print(g1_num)
#!/usr/bin/python3 # coding=utf-8 #缺省参数方便函数调用时写的短 #缺省参数写到后面 def print_info(name, gender = True): gender_text = "男生" if not gender: gender_text = "女生" print("%s 是 %s" %(name, gender_text)) print_info(‘熊大‘, False)
#!/usr/bin/python3 # coding=utf-8 #多个缺省参数就必须带名字 def print_info(name, title="", gender=True): """ :param name: 班上同学的名字 :param title: 职位 :param gender: True男生 False女生 :return: """ gender_text = "男生" if not gender: gender_text = "女生" print("%s%s 是 %s" %(title, name, gender_text)) # 提示:在指定缺省参数的默认值时,应该使用最常见的值作为默认值! print_info("xiaoming") print_info("laowang", title="坏人", gender=True) print_info("xiaomei", gender=False)
原文:https://www.cnblogs.com/LinawZ/p/13593721.html