Python函数基础

函数定义

def 函数名(参数)

# 函数
def hello(x,y):
	print(x+y)
hello(3,4)

def hello(x,y):
	return x+y
print(hello(3,5))
7
8

Process finished with exit code 0

返回值是元组的形式

def hello(x,y=6):
    return  x,y,12,5
print(hello(7))
(7, 6, 12, 5)

Process finished with exit code 0

可以指定参数赋值,实参优先级大于形参

def hello(x,y=6):
    return  x,y,12,5
print(hello(y=4,x=3))
(3, 4, 12, 5)

Process finished with exit code 0

不确定要传入几个值时可以用 *号,返回值是元组形式
def hello(*args)
* 后的任意取名

def hello(*args):
    print(args)

hello(1,2,3,4,5)
(1, 2, 3, 4, 5)

Process finished with exit code 0

def hello(**kwargs)
**kwargs用来传 键值对 返回值是字典

def hello(**kwargs):
    print(kwargs)
hello(hello = '你好',world = '世界')
{'hello': '你好', 'world': '世界'}

Process finished with exit code 0

lambda的用法

lambda匿名函数
一般在函数定义只使用一次的情况下使用
自带输入值和返回值

lambda函数示例

lambda x, y: xy;函数输入是x和y,输出是它们的积xy
lambda:None;函数没有输入参数,输出是None
lambda *args: sum(args); 输入是任意个数的参数,输出是它们的和(隐性要求是输入参数必须能够进行加法运算)
lambda **kwargs: 1;输入是任意键值对参数,输出是1

hello = lambda x,y:[x,y]
print(hello(3,555))

h = lambda x,y:x*y
print(h(3,6))
[3, 555]
18

Process finished with exit code 0

高阶函数

1.排序函数

sort() 正序
reverse() 逆序
sorted() 正序
sorted( ,reverse = True) 逆序

sort()和sorted()的区别
sort()是对原列表进行更改,sorted()是创建新的列表

lists = [1,5,2,7,3]
lists.sort()
print(lists)

new_lists = sorted(lists,reverse=True)
print(new_lists)

lists.reverse()
print(lists)
[1, 2, 3, 5, 7]
[7, 5, 3, 2, 1]
[7, 5, 3, 2, 1]

Process finished with exit code 0
2.map函数

批量处理一个列表或可迭代元素
调用时才做计算

# map
lists = [1,2,3,4]
m = map(lambda x:x**3,lists)
print(m)
# for i in m:
#     print(i)
print(list(m))
<map object at 0x0000015A83360C10>
[1, 8, 27, 64]

Process finished with exit code 0
# map
lists = [1,2,3,4]
m = map(lambda x:x**3,lists)
print(m)
for i in m:
    print(i)
# print(list(m))
<map object at 0x000001BDCA860C10>
1
8
27
64

Process finished with exit code 0
3.Reduce求和函数

reduce求和,需要导入reduce包

from functools import reduce

from functools import reduce
# Reduce
last = reduce(lambda x,y:x+y,range(1,101))
print(last)

5050

Process finished with exit code 0
4.filter 过滤函数

filter过滤

# filter
f = list(filter(lambda x:x%2==0,range(1,101)))
print(f)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]

Process finished with exit code 0

例题

1.自定义函数,要求可以把整数字符串转成int数据
# 自定义函数,把整数字符串转成int数据
from functools import reduce
num = '123.456'
dicts = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}

def get_data(num):
    m = list(map(lambda x:dicts[x],num))
    last = reduce(lambda x,y:10*x+y,m)
    return last

index = num.find('.')
num1 = num[:index]
num2 = num[index+1:]

n1 = get_data(num1)
n2 = get_data(num2)*0.1**3
print(n1+n2)
123.456

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值