欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python全局变量修改

程序员文章站 2024-01-22 19:27:10
...


n=1
ls=[]
def test1():
    global n  #用global可以修改全局变量
    n=5
test1()
print("the number is :",n)

def test2():
    for i in range(10):
        ls.append(i)   #函数内可以直接修改 列表的值
test2()
print("the list is :",ls)