python入门篇08- 数据容器, 函数(参数传递) -总结进阶


1.前言简介

1.1 专栏传送门

=> 传送: python专栏 <=

1.1.1 上文传送门

=> python入门篇07-数据容器(序列 集合 字典,json初识)基础(下)

1.1.2 上文(数据容器)小总结

介绍了几种数据容器 以及相对应的api操作
应用场景以及基础小案例

2. python基础使用

2.0 数据容器总结进阶

2.0.1 数据容器表格对比

数据容器 list列表 [] tuple元组() str字符串"" set集合{} dict字典{k:v}
下标索引
重复元素
可否修改
是否有序
while循环
for循环

2.0.2 数据容器进阶操作

准备测试案例代码

list1 = [1, 2, 3]
tuple1 = (1, 2, 3, 4, 5)
str1 = "abcdefg"
set1 = {
    
    1, 2, 3, 4, 5}
dict1 = {
    
    "key1": 1, "key2": 2}
-> (1) 长度: len() 方法
print(len(list1))
print(len(tuple1))
print(len(str1))
print(len(set1))
print(len(dict1))
-> (2) 最大值: max() 方法
print(max(list1))
print(max(tuple1))
print(max(str1))
print(max(set1))
print(max(dict1))
-> (3) 最小值: min() 方法
print(min(list1))
print(min(tuple1))
print(min(str1))
print(min(set1))
print(min(dict1))
-> (4) 容器转换list: list() 方法
print(list(list1))  # [1, 2, 3]
print(list(tuple1))  # [1, 2, 3, 4, 5]
print(list(str1))  # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(list(set1))  # [1, 2, 3, 4, 5]
print(list(dict1))  # ['key1', 'key2']
-> (5) 容器转换元组: tuple() 方法
print(tuple(list1))  # (1, 2, 3)
print(tuple(tuple1))  # (1, 2, 3, 4, 5)
print(tuple(str1))  # ('a', 'b', 'c', 'd', 'e', 'f', 'g')
print(tuple(set1))  # (1, 2, 3, 4, 5)
print(tuple(dict1))  # ('key1', 'key2')
-> (6) 容器转换集合: set() 方法
print(set(list1))  # {1, 2, 3}
print(set(tuple1))  # {1, 2, 3, 4, 5}
print(set(str1))  # {'g', 'a', 'f', 'b', 'e', 'd', 'c'}
print(set(set1))  # {1, 2, 3, 4, 5}
print(set(dict1))  # {'key1', 'key2'}
-> (7) sorted排序: sorted() 方法
print(sorted(list1))  # [1, 2, 3]
print(sorted(tuple1))  # [1, 2, 3, 4, 5]
print(sorted(str1))  # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(sorted(set1))  # [1, 2, 3, 4, 5]
print(sorted(dict1))  # ['key1', 'key2']
# 倒序 reverse默认是 false 正序
print(sorted(dict1, reverse=True))  # ['key2', 'key1']

2.0.3 字符串比较(ASCII编码)

与ASCII 数字对应的 American Standard Code for Information Interchange: American信息交换标准代码

# abd 与 abc比较
print('abd' > 'abc')  # True

# a 与 A 比较
print('a' > 'A')  # True

# ba 大于 ab 先比较第一位 然后再比较第二位 9897>9798
print('ba' > 'ab')  # True

2.1 函数进阶 - 参数传递

2.1.1 设置多个返回值

def test_func():
    return 1, 2, 3


# 看一种报错
# arg1, arg2 = test_func()
# print(arg1)  # ValueError: too many values to unpack (expected 2)


arg1, arg2, arg3 = test_func()
print(arg1)  # 1
print(arg2)  # 2
print(arg3)  # 3

print(test_func())  # (1, 2, 3) 元组

2.2 传参方式(多种)

2.1.0 代码准备

def test_func01(name, age, address):
    print(f"名字:{
      
      name},年龄:{
      
      age},地址:{
      
      address}")

2.1.1 方式一: 参数位置传递

test_func01("张三", 13, "地球")

2.1.2 方式二: 关键字参数传递

(这种类似 k-v结构 位置就可以不对应了)

test_func01(name="张三", address="地球", age=13)

ps: 看一个报错
TypeError: test_func01() missing 1 required positional argument: ‘age’

# test_func01(name="张三", address="地球")

2.1.3 方式三: 缺省参数传递

给参数设置默认值 不传递也可以
注意 设置默认值的多个参数 都必须放到最后

test_func01(name="张三", address="地球", age=13)

测试代码+整体效果如下:

# 名字:张三,年龄:13,地址:地球
# 名字:张三,年龄:13,地址:地球
# 名字:张三,年龄:13,地址:火星
# 名字:张三,年龄:13,地址:中国
test_func02(age=13, name="张三")
test_func02("张三", 13)
test_func02(age=13, address="火星", name="张三")
test_func02("张三", 13, "中国")

2.1.4 方式四: 不定长参数传递

不定长参数(类似java的可变参数)
不定长定义的形式参数作为元组存在 用 “*” 表示

def test_func03(*args):
    print(args)   # (1, 2, 3, 4)
    print(type(args))  # <class 'tuple'>


test_func03(1, 2, 3, 4)

2.1.5 方式五: 关键字不定长参数传递

变成 字典 用 “**” 表示
java没有此写法 直接传递map

def test_func04(**kwargs):
    print(kwargs)  # {'age': 12, 'name': '张三'}
    print(type(kwargs))  # <class 'dict'>


test_func04(age=12, name="张三")

2.3 函数参数传递

这个有点意思, 也就是把动作行为装成参数进行传递了

2.3.1 正常写法

参数传递的是函数 参数名随便写 也就是将函数运算过程(不是参数) 传递进去

def test_func01(test_func):
    result = test_func(1, 2)
    print(type(test_func))  # <class 'function'>
    print(result)  # 3


def test_func02(arg1, arg2):
    return arg1 + arg2


test_func01(test_func02)  # 3

2.3.2 lambda匿名函数写法

对上面函数进行改造 (一行代码lambda ,多行使用def)

test_func01(lambda x, y: x + y)

3. 基础语法总结案例

python入门篇09- 文件操作,函数, 包及模块的综合案例
在这里插入图片描述

4. 文章的总结与预告

4.1 本文总结

函数的不同传递方式, 使用更加灵活



作者pingzhuyan 感谢观看

猜你喜欢

转载自blog.csdn.net/pingzhuyan/article/details/133794092