重新学习python20

0, 如果希望在函数中修改全局变量的值,应该使用什么关键字?

global关键字

 

1,在嵌套函数中,如果希望在内部函数修改函数外部的局部变量,应该使用什么关键字?

???nonlocal关键字

 

2, python函数可以嵌套,但需要注意访问的作用域问题

## 看一下打印输出结果
'''
def outside():
    print("I am outside!")
    def inside():
        print("I am inside!")

inside()

'''

将会报错,需要看一下 函数的嵌套的使用范围,需要注意一下。改成如下即可:

def outside():
    print("I am outside!")
    def inside():
        print("I am inside!")

    inside()

outside()

 

3,模块A运行正常,模块B运行失败。

## 模块A是正常输出
'''
def outside():
    var = 5
    def inside():
        var = 3
        print(var)

    inside()

outside()
'''
##输出结果: 3


## 模块B为什么出错?
'''
def outside():
    var = 5
    def inside():
        print(var)
        var = 3

    inside()

outside()
'''
##输出结果:
UnboundLocalError: local variable 'var' referenced before assignment

outside函数中有var变量,注意内嵌函数中也有同名变量,python为了保护变量作用域,将outside的var变量屏蔽起来,因此 此时是无法访问到外层的var变量。

 

所有可以使用nonlocal关键字标记变量,将模块B修改:


def outside():
    var = 5
    def inside():
        nonlocal var
        print(var)
        var = 3
        #print(var)

    inside()

outside()

 

4

'''
##如何访问funIn?
def funOut():
    def funIn():
        print("!!! 成功访问到我啦!")
    return funIn()
'''

使用如下类似语法即可访问
a = funOut()

 

如下所示,如何访问funIn? 注意,与上面的例子不同。

def funOut():
    def funIn():
        print("!!! 成功访问到我啦!")
    return funIn

b = funOut()()

 

5 输出 6 7 8

## 以下是闭包的例子,看一下输出结果?
def funX():
    x = 5
    def funY():
        nonlocal x
        x += 1
        return x
    return funY

a = funX()
print(a())
print(a())
print(a())

当a = funX()时候,只要a变量没有被重新赋值,funX()就没有被释放,也就是说局部变量x没有被重新初始化。

所以当全局变量 不适用的时候,可以考虑使用闭包更稳定和安全。

 

6,计算字符串中每个字符出现的次数

方法1)

def chCount(s):
    #length = len(s)
    dic = {}
    for i in s:
        dic[i] = s.count(i)
    print(dic)

s = input("请输入一个字符串: ")
print(chCount(s))

 

方法2)

str1 = '''拷贝过来的字符串'''
list1 = []

for each in str1 :
    if each not in list1 :
        if each == '\n' :
            print('\\n', str1.count(each))
        else :
            print(each, str1.count(each))
        list1.append((each))

 

 

7 按照如下要求写出代码

 

str1 =  """拷贝过来字符串"""
countA = 0
countB = 0
countC = 0
length = len(str1)

for i in range(length):
    if str1[i] == '\n':
        continue
    if str1[i].isupper():
        if counB == 1 :
            countC += 1
            countA = 0
        else :
            countA += 1
        continue

    if str1[i].islower() and countA == 3 :
        countB = 1
        countA = 0
        target = i
        continue

    if str1[i].islower() and countC == 3 :
        print(str1[target], end='')
    
    countA = 0
    countB = 0
    countC = 0

 

参考:

Python 字典(Dictionary)

https://www.runoob.com/python/python-dictionary.html

Python--统计字符串中字符个数

https://blog.csdn.net/qq_40801709/article/details/86070833

Python:删除、增加字典的元素

Python 如何将字符串转为字典

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值