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)
相关推荐
深蓝电商API1 天前
Selenium多窗口切换与Cookie管理
爬虫·python·selenium·测试工具
砚边数影1 天前
时序数据库国产化替代,破局迁移“三不”困局
数据库·时序数据库·kingbase·kingbasees·金仓数据库
专注于大数据技术栈1 天前
Redis 中 USED 和 RSS
数据库·redis·缓存
咚为1 天前
Rust 错误处理的工程化演进:从 Result 到系统级边界设计
开发语言·后端·rust
qq_406176141 天前
深入剖析JS中的XSS与CSRF漏洞:原理、攻击与防御全指南
服务器·开发语言·前端·javascript
一个响当当的名号1 天前
lectrue8 表索引
数据库
独自破碎E1 天前
MySQL是怎么实现事务的?
数据库·mysql
小北方城市网1 天前
Spring Cloud 服务治理实战:构建高可用微服务体系
spring boot·python·rabbitmq·java-rabbitmq·数据库架构
qq_12498707531 天前
基于Java的心理测试系统的设计与实现(源码+论文+部署+安装)
java·开发语言·vue.js·spring boot·计算机毕设·计算机毕业设计
卜锦元1 天前
Docker Compose 部署 MySQL 8.4 LTS(生产级实践方案)
数据库·mysql·docker·容器