python基础学习(五)

10.列表

# 列表  List  同时存放很  多数据
# 正数索引index 0        1         2
# 负数        -3       -2        -1
fruits1 = ["apple", "orange", "pear"]
# 列表支持  数字 字符串 布尔 类型 同时存在
fruits2 = ["apple", "orange", "pear", "100", True]

print(fruits1)
# 列表可通过索引来查找   pear
print(fruits1[2])
print(fruits1[-1])
print(fruits2)

# 切片  slice [开始:结束)
print(fruits2[1:4])  # ['orange', 'pear', '100']
# 替换列表里的值
fruits1[0] = "grape"
print(fruits1)

run结果:

11.元组

# 元组  tuple   一旦确定元素不能被修改和删除
com = ("google", "microsoft", "alibaba", "tencent")
print(com[0])

run结果:

原文地址:https://www.cnblogs.com/songxiaoke/p/11880793.html