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)
相关推荐
djjdjdjdjjdj17 小时前
如何快速查询SQL中的重复记录:GROUP BY与COUNT统计
jvm·数据库·python
2301_7826591817 小时前
Redis怎样监控当前发生了多少次内存驱逐
jvm·数据库·python
万世浮华戏骨17 小时前
PHP 与数据库交互 与 SQL注⼊漏洞
数据库·sql·php
m0_6178814217 小时前
如何在新电脑上正确运行已部署的 Django 项目
jvm·数据库·python
u01091476017 小时前
Golang怎么计算日期差天数_Golang如何计算两个日期之间相差多少天【方法】
jvm·数据库·python
蚰蜒螟17 小时前
从 pthread_create 到 thread_native_entry:glibc 如何唤醒 Java 线程
java·开发语言
We་ct17 小时前
LeetCode 300. 最长递增子序列:两种解法从入门到优化
开发语言·前端·javascript·算法·leetcode·typescript
gCode Teacher 格码致知17 小时前
Python提高: unittest和 pytest的使用方法-由Deepseek产生
开发语言·python·pytest
pele17 小时前
Python Tkinter如何实现组件拖拽交换位置_计算鼠标坐标重排布局
jvm·数据库·python
hua8722217 小时前
Spring Boot 中使用 @Transactional 注解配置事务管理
数据库·spring boot·sql