Python函数进阶

文章目录

  • [1 函数多返回值](#1 函数多返回值)
  • [2 函数多种传参方式](#2 函数多种传参方式)
    • [2.1 位置参数](#2.1 位置参数)
    • [2.2 关键字参数](#2.2 关键字参数)
    • [2.3 缺省参数](#2.3 缺省参数)
    • [2.4 不定长参数](#2.4 不定长参数)
  • [3 匿名函数](#3 匿名函数)

1 函数多返回值

python 复制代码
def test_return():
    return 1,2,3
x,y,z = test_return()
print(x)
print(y)
print(z)

2 函数多种传参方式

2.1 位置参数

2.2 关键字参数

python 复制代码
def user_info(name,age,gender):
    print(name,age,gender)
user_info('小明','18','男')
user_info(name='小王',age=18,gender='男')
user_info(age=18,gender='女',name='小白')
user_info('天天',age=48,gender='男')

2.3 缺省参数

python 复制代码
def user_info(name,age,gender='男'):
    print(name,age,gender)
user_info('小天',13)
user_info('x',55,'女')

2.4 不定长参数


python 复制代码
def user_info(*args):
    print(args)
user_info(1,2,3,'xiaom')
python 复制代码
def user_info(**kwargs):
    print(kwargs)
user_info(name='小王',age=18,gender='男')

3 匿名函数

函数作为参数传递

python 复制代码
def test_func(compute):
    result = compute(1,2)
    print(type(compute))
    print(result)
def compute(x,y):
    return x+y
test_func(compute)

lambda匿名函数


python 复制代码
def test_func(compute):
    result = compute(1,2)
    print(result)
test_func(lambda x,y:x+y)
相关推荐
ben9518chen几秒前
数据库系统概述
数据库
Chasing__Dreams4 分钟前
python杂识--24--Nuitak打包exe
开发语言·python
花间相见6 分钟前
【计算基础|MySQL03】【上手】多表查询与聚合
数据库·mysql
把我格子衫拿来8 分钟前
RANSAC 粗配准:离群点很多时,怎样为 ICP 找到可信初值?
python·计算机视觉·三维重建·ransac·点云配准
yuniko-n9 分钟前
【JUC】读写锁
java·开发语言
niucloud-admin10 分钟前
NIUCLOUD LITE AI开发文档--基础开发规范
开发语言·php
Web3&Basketball15 分钟前
决策模型vs GPT-5实测:便宜400倍
人工智能·python·gpt·大模型·ai落地·推理优化·决策模型
ljt272496066118 分钟前
Python笔记(四)--模式匹配与正则
笔记·python
小羊没烦恼!18 分钟前
T-SQL查询高级—SQL Server索引中的碎片和填充因子
开发语言·前端·javascript·数据库·后端·python·web安全
the局外人20 分钟前
学会 SQLAlchemy 搭建 Python 与 MySQL 之间的数据访问桥梁 🚀
后端·python·sql