python列表

数值列表

复制代码
numbers = [1, 2, 3.14, 42, 1.618]

字符串列表

复制代码
fruits = ["apple", "banana", "cherry", "date"]

混合类型列表

复制代码
mixed_list = [1, "hello", 3.14, True, [5, 6]]

嵌套列表

复制代码
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

使用append方法动态添加元素的列表

复制代码
animals = []
animals.append("dog")
animals.append("cat")
animals.append("bird")

使用列表推导式创建的列表

复制代码
squares = [x**2 for x in range(10)]  # 生成0到9的平方

列表生成器

复制代码
# 生成0到9中所有偶数的列表
even_numbers = [num for num in range(10) if num % 2 == 0]

封装通用打印方法

复制代码
def print_collection_common(array):
    print("-------------------------")
    for index, obj in enumerate(array):
        print(f"Index: {index}, Value: {obj}")

列表截取

复制代码
变量[头下标:尾下标]
print(list)
print(list[0])
print(list[1:3])
print(list[2:])
相关推荐
Warson_L15 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅15 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅15 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L15 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅16 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L16 小时前
python的类&继承
python
Warson_L16 小时前
类型标注/type annotation
python
ThreeS19 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵20 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏