python语言篇(7练习3)

# 此示例示意用def 语句创建一个函数
def say_hello():
    print("hello world!")
    print("hello tarena!")
    print("hello everyone!")


# 调用say_hello函数,调用时会执行say_hello内的代码块
say_hello()

say_hello()
 

# function_variable.py
def test():
    x = 100  # 此变量是局部变量,只能在函数内部使用
    print(y)  # 这是合法的,此函数可以访问函数以外的全局变量


y = 200

test()  # 调用test

# print(x)  # 此时没有x这个变量, 出错

# 此示例来示意fn函数中lst绑定的缺省参数的列表的生命周期
def fn(a, lst=[]):  # <<--- 重要
    lst.append(a)
    print(lst)


# L = [1, 2, 3, 4]
# fn(5, L)  # [ 1,2,3,4,5]
# fn(6, L)  # [1,2,3,4,5,6]

fn(1.1)  # [1.1]
fn(2.2)  # [1.1, 2.2]

猜你喜欢

转载自blog.csdn.net/Jason_Edison/article/details/88831947