python语法入门三

  • 函数
  • lambda表达式
  • 过滤器filter
  • 字典
  • set集合

函数

def Fun(name):
    print(name)

Fun('abc')


def Add(num1, num2):
    '两数求和'
    # 有文档注释
    return num1 + num2

print(Add(3, 4))
print(Add.__doc__)
# 两数求和


def Say(name='姓名', age=0):
    print(name + ' ' + str(age))

Say(name='新')
# 新 0


def test(*num, exp):
    print('参数的长度是:', len(num), exp)
    print('第二个参数是:', num[1])

test(1, 2, 3, 4, exp = '其他参数')
# 参数的长度是: 4 其他参数
# 第二个参数是: 2


def mod():
    global cnt # 表示使用的是全局变量cnt
    cnt = 5
    print(cnt)

cnt = 10
mod()
# 5
print(cnt)
# 5


'闭包'
def fun1(x):
    def fun2(y):
        return x * y
    return fun2

t = fun1(2)
print(t(3))
# 6
print(fun1(2)(3))
# 6


'闭包'
def fun1():
    x = 5
    def fun2():
        nonlocal x
        x *= x
        return x
    return fun2()

print(fun1())
# 25

lambda表达式

'lambda表达式第一个参数表示参数,第二个表示返回值'
'''
以下相当于
def v(x, y):
    return 2 * x + y
'''
v = lambda x, y : 2 * x + y
print(v(3, 1))
# 7

过滤器filter + lambda表达式

'''
filter() 函数用于过滤序列
过滤掉不符合条件的元素
返回由符合条件元素组成的新列表。
'''
'返回是true的列表'
lists = list(filter(None, [1, 0, False, True]))
print(lists)
# [1, True]

'返回符合odd函数的列表'
def odd(x):
    # x % 2 == 1
    return x % 2
temp = range(10)
show = filter(odd, temp)
print(list(show))
# [1, 3, 5, 7, 9]

lists = list(filter(lambda x : x % 2, range(10)))
print(list(lists))
# [1, 3, 5, 7, 9]

lists = map(lambda x : x * 2, range(10))
print(list(lists))
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

字典

dict1 = {'中国':'zhongguo', '俄罗斯':'eluosi'}
print(dict1)
# {'中国': 'zhongguo', '俄罗斯': 'eluosi'}
print(dict1['中国'])
# zhongguo

dict2 = dict(F='30', C='67')
print(dict2)
# {'F': '30', 'C': '67'}
dict2['F'] = 0
print(dict2)
# {'F': 0, 'C': '67'}
dict2['G'] = '-1'
print(dict2)
# {'F': 0, 'C': '67', 'G': '-1'}

dict3 = {}
print(dict3.fromkeys((1, 2, 3)))
# {1: None, 2: None, 3: None}
print(dict3.fromkeys((1, 2, 3), ('one', 'two')))
# {1: ('one', 'two'), 2: ('one', 'two'), 3: ('one', 'two')}
print(dict3)
# {}

dict4 = dict3.fromkeys(range(5), '赞')
print(dict4)
# {0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞'}
print(dict4.get(0))
# 赞
print(dict4.get(5, '没有'))
# 没有
for i in dict4.keys():
    print(i)
# 0
# 1
# 2
# 3
# 4
for i in dict4.items():
    print(i)
# (0, '赞')
# (1, '赞')
# (2, '赞')
# (3, '赞')
# (4, '赞')
print(dict4.pop(1))
# 赞
print(dict4)
# {0: '赞', 2: '赞', 3: '赞', 4: '赞'}
dict4.setdefault('add', '不知道')
print(dict4)
{0: '赞', 2: '赞', 3: '赞', 4: '赞', 'add': '不知道'}

dict5 = {}
dict5.update(dict4)
print(dict5)
# {0: '赞', 2: '赞', 3: '赞', 4: '赞', 'add': '不知道'}
print(id(dict4), id(dict5))
# 1714578880696 1714578880536

set集合

num1 = {1, 1, 2, 3, 3, 4, 5}
print(type(num1))
# <class 'set'>
print(num1)
# {1, 2, 3, 4, 5}

set1 = set([1, 1, 1, 2, 3, 4, 4])
print(set1)
# {1, 2, 3, 4}
set1.add(5)
set1.remove(1)
print(set1)
# {2, 3, 4, 5}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值