【笔试题】局部变量和全局变量---------------【答不对,你还敢说你精通、熟悉python?】...

请说出运行结果,并解释why?

 

 ================第一部分==================

res = None
def calc(a,b):
    res = a+b
calc(1,2)
print(res) 

上面代码结果是:

 

res = None
def calc(a,b):
    res = 0
    res = a+b
calc(1,2)
print(res)

上面代码结果是:

 

res = None
def calc(a,b):
    global res
    res = a+b
calc(1,2)
print(res)

上面代码结果是:

 

res2 = None
def calc(a,b):
    global res
    res = a+b
calc(1,2)
print(res)

上面代码结果是:  

 

res = None
def calc(a,b):
    res = a+b
    global res
calc(1,2)
print(res)

上面代码结果是:

 

res = None
def calc(a,b):
    global res
    res = 0
    res = a+b
calc(1,2)
print(res)

上面代码结果是:

 

res = None
def calc(a,b):
    res = 0
    global res
    res = a+b
calc(1,2)
print(res) 

上面代码结果是:

 

res = None
def calc(a,b):
    res = 0    
    res = a+b
    global res
calc(1,2)
print(res)

上面代码结果是:

 

money = 0
def tom():
    global money
    money = 100

def jack():
    global money
    money = money - 50
tom()
jack()
print('jack消费后剩余%s'%money) 

上面代码结果是:

 

def tom():
    global money
    money = 100

def jack():
    global money
    money = money - 50
tom()
jack()
print('jack消费后剩余%s'%money)

上面代码结果是:  

 

 ================第二部分================== 

 

d = {}
def test():
    d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
def test2():
    d['url']='https://www.cnblogs.com/uncleyong/'
test()
test2()
print(d)

上面代码结果是:

 

def test():
    d = {}
    d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
def test2():
    d = {}
    d['url']='https://www.cnblogs.com/uncleyong/'
test()
test2()
print(d)

上面代码结果是:

 

def test():
    d = {}
    d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
def test2():
    global d
    d = {}    
    d['url']='https://www.cnblogs.com/uncleyong/'
test()
test2()
print(d)

上面代码结果是:

 

info ={'age':18, 'url':'https://www.cnblogs.com/uncleyong/p/10530261.html'}
def test():
    global info
    info={}
    info['name']='qzcsbj'
test()
print(info)

上面代码结果是: 

 

info ={'age':18, 'url':'https://www.cnblogs.com/uncleyong/p/10530261.html'}
def test():
    info={}
    info['name'] = 'qzcsbj'
test()
print(info)

上面代码结果是: 

 

info ={'age':18, 'url':'https://www.cnblogs.com/uncleyong/p/10530261.html'}
def test():
    info['age']=info['age']+1
test()
print(info) 

上面代码结果是:

 

s = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
def test():
    s = 'test'
test()
print(s)

上面代码结果是: 

 

url = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
def test():
    s = 'test'
test()
print(s)

上面代码结果是:

 

url = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
def test():
    global s
    s = 'test'
test()
print(s)

上面代码结果是:

 

s = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
def test():
    global s
    s = 'test'
test()
print(s) 

上面代码结果是:

 

s = [1,2,3]
def test():
    s[0]= 123
test()

print(s)

上面代码结果是:

 

s = [1,2,3]
def test():
    s = []
    s.append(123)
test()

print(s)

上面代码结果是:  

 

s = [1,2,3]
def test():
    global s
    s[0]= 123
test()

print(s)

上面代码结果是:

 

s = [1,2,3]
def test():
    global s
    s = []
    s.append(123)
test()

print(s)

上面代码结果是: 

 

s = (1,2,3)
def test():
    s[0]=123
test()
print(s)

上面代码结果是:

 

s = (1,2,3)
def test():
    global s
    s[0]=123
test()
print(s)

上面代码结果是:

 

s = (1,2,3)
def test():
    s = (4,5)
test()
print(s)

上面代码结果是: 

 

s = (1,2,3)
def test():
    global s
    s = (4,5)
test()
print(s)

上面代码结果是:

 

s = {1,2,3}
def test():
    s.add(5)
test()
print(s)

上面代码结果是:

 

s = {1,2,3}
def test():
    global s
    s.add(5)
test()
print(s)

上面代码结果是:

 

s = {1,2,3}
def test():
    s = set()
    s.add(5)
test()
print(s)

上面代码结果是:

 

s = {1,2,3}
def test():
    global s
    s = set()
    s.add(5)
test()
print(s)

上面代码结果是:

 

转载于:https://www.cnblogs.com/uncleyong/p/11230413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值