Fork me on GitHub

寒假学习(1)

今天在校,还没有团队项目的开始计划,所以今天先从python入手,先学习python基础,以下为部分今天看过的知识点:

列表是Python中最常用的数据结构之一,可以存储多个元素,并且可以进行增删改查等操作。

  1. 创建列表:可以使用方括号 [] 或者 list() 函数来创建一个列表。例如:
fruits = ['apple', 'banana', 'orange']
  1. 访问列表元素:可以使用索引来访问列表中的元素,索引从0开始。例如:
print(fruits[0])  # 输出 'apple'
  1. 修改列表元素:可以通过索引来修改列表中的元素。例如:
fruits[1] = 'grape'
print(fruits)  # 输出 ['apple', 'grape', 'orange']
  1. 列表的长度:可以使用 len() 函数来获取列表的长度。例如:
print(len(fruits))  # 输出 3
  1. 添加元素:可以使用 append() 方法向列表末尾添加一个元素。例如:
fruits.append('kiwi')
print(fruits)  # 输出 ['apple', 'grape', 'orange', 'kiwi']
  1. 删除元素:可以使用 del 关键字或者 remove() 方法删除列表中的元素。例如:
del fruits[0]
print(fruits)  # 输出 ['grape', 'orange', 'kiwi']

fruits.remove('orange')
print(fruits)  # 输出 ['grape', 'kiwi']
  1. 切片操作:可以使用切片对列表进行部分取值。例如:
print(fruits[1:])   # 输出 ['kiwi']
print(fruits[:2])   # 输出 ['grape', 'kiwi']
print(fruits[1:3])  # 输出 ['kiwi']
  1. 列表的遍历:可以使用 for 循环来遍历列表中的元素。例如:
for fruit in fruits:
    print(fruit)
posted @ 2024-01-10 20:35  (该昵称暂可见)  阅读(12)  评论(0)    收藏  举报