函数 global全局变量; 局部变量;缺省参数

发布时间 2023-07-05 12:34:02作者: 胖豆芽
# 函数 在函数里设置全局变量,会因为被赋值而修改
x=2
def funcx():
    global x #这个x 是全局变量  会因为函数里面被赋值而修改
    x=9
    print("this x is in the funcx:-->",x)
funcx()
print("--------------")
print("this x is in the funcx:-->",x)
'''
this x is in the funcx:--> 9
--------------
this x is in the funcx:--> 9
'''
# 函数 求大于【6】岁的男生的列表  女生的列表
def statmf(students,minage=3):
    malelist=[]
    femalelist=[]
    for student in students:
        if student['age']>minage:
            if student['gender']=='male':
               malelist.append(student['name'])
            elif student['gender']=='female':
               femalelist.append(student['name'])
    print(f'男生{" ".join(malelist)}')# 一个字符串的方法,它将列表中的所有元素连接成一个字符串
#验证
students=[{'age':18,'name':'fqs','gender':'male'},
          {'age':3,'name':'d','gender':'male'},
          {'age':19,'name':'f','gender':'male'},
          {'age':35,'name':'a','gender':'male'},]
statmf(students)

'''
this x is in the funcx:--> 9
--------------
this x is in the funcx:--> 9
'''