python-demo/basic_learn/列表.py

36 lines
698 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : 列表.py
@Author : lichao
@Date : 2024/11/26 17:06
@Software : PyCharm
"""
# 创建列表的两种方式
# 1.使用[]创建
list1 = [1, 2, 3, 4, 5]
list1.append(6)
# 2.使用list()创建
list2 = list("hello")
print(list1, list2)
# 3.使用 + 连接两个列表
list3 = list1 + list2
print(list3)
# 4.使用extend()方法,将一个列表中的元素添加到另一个列表中
list4 = [1, 2, 3]
list5 = [4, 5, 6]
list4.extend(list5)
print(list4)
# 使用 + 不改变原列表, 使用+= 更改原列表
# 使用append()改变原列表
insert_data = [1, 2, 4]
insert_data.insert(2, 3) # 在索引2的位置插入3