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)
相关推荐
在世修行8 分钟前
从零打造 C# 工业视觉检测系统(十五):项目打包部署与进阶展望
开发语言·c#·视觉检测
xfhuangfu10 分钟前
Oracle 19c 本地索引分区变为 UNUSABLE 后的空间占用验证
数据库·oracle
杨云龙UP14 分钟前
Oracle ASH 报告详解:与 AWR 的区别、使用场景及生成方法
linux·运维·服务器·数据库·oracle
vx-程序开发15 分钟前
django汽车租赁系统---附源码25360
java·javascript·spring boot·python·eclipse·django·php
笨鸟先飞,勤能补拙17 分钟前
AI Agent应用领域深度解析:从概念到落地的全维度审视
大数据·人工智能·python·物联网·安全·网络安全·github
Nebula_g22 分钟前
JavaSE基础语法:特殊类(特殊情景下的设计模式)
java·开发语言·设计模式
不正经学生26 分钟前
C语言大小端字节序:内存里字节的排列顺序
c语言·开发语言·arm开发·c++·算法·c#
黑泽明*30 分钟前
LVS-Keepalived-全解析
服务器·开发语言·笔记·学习·php
动词ing39 分钟前
【C语言】命名规范
c语言·开发语言
大可-1 小时前
Go Air 热重载安装配置指南
开发语言·后端·golang