首页 > 编程语言 > 详细

0830Python基础练习

时间:2020-08-31 23:44:36      阅读:79      评论:0      收藏:0      [点我收藏+]
技术分享图片
#!/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)
View Code
技术分享图片
#!/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)
View Code
技术分享图片
#!/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)
View Code
技术分享图片
#!/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)
View Code
技术分享图片
#!/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)
View Code
技术分享图片
#!/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)
View Code

 

0830Python基础练习

原文:https://www.cnblogs.com/LinawZ/p/13593721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!