列表List
定义列表
可以将不同的基本数据类型或者列表装到一个列表里
my_list = [1,2,3,4,5]
print(my_list) # [1, 2, 3, 4, 5] 直接打印出列表的内容
print(type(my_list)) # <class 'list'>
my_list = ["1","2","3","4","5"]
print(my_list)
my_list = [1,2,"3",4,5]
print(my_list)
my_list = [1,2,"3",4.1,True]
print(my_list)
my_list = [1,["a","b"],"3",4.1,True]
print(my_list)
角标访问列表元素
my_list = ["a","qwe","c","d","e"]
print(my_list)
print(my_list[1])
print(type(my_list[1])) # <class 'str'>
print(my_list[1][0]) # q
访问列表中的某一个区间的所有元素
my_list = ["a","b","c","d","e"]
print(my_list)
# 如果我想访问列表中的某一个区间的所有元素
print(my_list[:])
# 从最开始的位置打印到第二个位置
# 但不包含第二个位置
# 大部分情况下都是左闭右开的原则
print(my_list[:2]) # ['a', 'b']
print(my_list[1:3]) # ['b', 'c']
my_list = ["a","b","c","d","e"]
# 访问最后一个就从-1开始
print(my_list[-1]) # 结果是e
print(my_list[-4]) # 结果是b
# 我们在访问列表的时候[]里边写的都是从左往右数的
print(my_list[-1:-3]) # 结果是[]
print(my_list[-4:-2]) # 结果是['b', 'c']
修改列表中的元素
列表元素的增加
my_list = ["a","b","c","d","e"]
print(my_list) # ['a', 'b', 'c', 'd', 'e']
my_list.append("f")
print(my_list) # ['a', 'b', 'c', 'd', 'e', 'f']
列表元素值的修改,更新
my_list = ["a","b","c","d","e"]
print(my_list) # ['a', 'b', 'c', 'd', 'e']
# 你要更新哪一个数据,必须先能访问到这个数据
my_list[2] = "G"
print(my_list) # ['a', 'b', 'G', 'd', 'e']
列表元素值的删除
my_list = ["a","b","c","d","e"]
print(my_list) # ['a', 'b', 'c', 'd', 'e']
# 1、根据值删除
my_list.remove("d")
print(my_list) # ['a', 'b', 'c', 'e']
# 2、根据索引来删除
my_list.pop(1)
print(my_list) # ['a', 'c', 'e']
使用 for 循环遍历 List
for i in my_list:
if i == "b":
continue
if i == "d":
break
print(i)
# 执行顺序为:
# 循环到 a 不符合上面条件,打印 a
# 循环到 b 符合上面第一个条件,结束此次循环 不打印b
# 循环到 c 不符合上面条件,打印 c
# 循环到 d 符合上面第二个条件,结束整个 for 循环 不打印d 程序结束
使用 while 循环遍历 List
my_list = ["a","b","c","d","e"]
l = len(my_list) # 打印list的长度,结果为 5
print(l)
i = 0
while True:
print(my_list[i])
i+=1
if i == l:
break
print(len("aaaa")) # 打印字符串的长度,结果为 4
练习:魔法读心术程序
假设:你想的数字出现在 A,D,E 这三张卡片上,这时候,我们只需要将这三张卡片的第一个数字相加,也就是1+8+16=25,那么你想的数字就是 25。
A = [1, 9, 17, 25, 33, 41, 49, 57,
3, 11, 19, 27, 35, 43, 51, 59,
5, 13, 21, 29, 37, 45, 53, 61,
7, 15, 23, 31, 39, 47, 55, 63]
B = [2, 10, 18, 26, 34, 42, 50, 58,
3, 11, 19, 27, 35, 43, 51, 59,
6, 14, 22, 30, 38, 46, 54, 62,
7, 15, 23, 31, 39, 47, 55, 63]
C = [4, 12, 20, 28, 36, 44, 52, 60,
5, 13, 21, 29, 37, 45, 53, 61,
6, 14, 22, 30, 38, 46, 54, 62,
7, 15, 23, 31, 39, 47, 55, 63]
D = [8, 12, 24, 28, 40, 44, 56, 60,
9, 13, 25, 29, 41, 45, 57, 61,
10, 14, 26, 30, 42, 46, 58, 62,
11, 15, 27, 31, 43, 47, 59, 63]
E = [16, 20, 24, 28, 48, 52, 56, 60,
17, 21, 25, 29, 49, 53, 57, 61,
18, 22, 26, 30, 50, 54, 58, 62,
19, 23, 27, 31, 51, 55, 59, 63]
F = [32, 36, 40, 44, 48, 52, 56, 60,
33, 37, 41, 45, 49, 53, 57, 61,
34, 38, 42, 46, 50, 54, 58, 62,
35, 39, 43, 47, 51, 55, 59, 63]
# 魔法读心术
A = [1, 9, 17, 25, 33, 41, 49, 57,
3, 11, 19, 27, 35, 43, 51, 59,
5, 13, 21, 29, 37, 45, 53, 61,
7, 15, 23, 31, 39, 47, 55, 63]
B = [2, 10, 18, 26, 34, 42, 50, 58,
3, 11, 19, 27, 35, 43, 51, 59,
6, 14, 22, 30, 38, 46, 54, 62,
7, 15, 23, 31, 39, 47, 55, 63]
C = [4, 12, 20, 28, 36, 44, 52, 60,
5, 13, 21, 29, 37, 45, 53, 61,
6, 14, 22, 30, 38, 46, 54, 62,
7, 15, 23, 31, 39, 47, 55, 63]
D = [8, 12, 24, 28, 40, 44, 56, 60,
9, 13, 25, 29, 41, 45, 57, 61,
10, 14, 26, 30, 42, 46, 58, 62,
11, 15, 27, 31, 43, 47, 59, 63]
E = [16, 20, 24, 28, 48, 52, 56, 60,
17, 21, 25, 29, 49, 53, 57, 61,
18, 22, 26, 30, 50, 54, 58, 62,
19, 23, 27, 31, 51, 55, 59, 63]
F = [32, 36, 40, 44, 48, 52, 56, 60,
33, 37, 41, 45, 49, 53, 57, 61,
34, 38, 42, 46, 50, 54, 58, 62,
35, 39, 43, 47, 51, 55, 59, 63]
# 返回时cards是一个字符串
cards = input("请告诉我你想的数字都位于哪些卡片上,多张卡片使用\",\"进行分隔哦,不要撒谎哦!!>>")
# split是分割字符串的一个操作方法, # 它的返回值就是一个list
# 记录用户输入的卡片
cards_number_list = []
card_list = cards.split(",")
for card in card_list:
# upper()可以将所有字母变成大写字母
# lower()可以将所有的字母变成小写字母
upper_char = card.upper()
if upper_char == "A":
cards_number_list.append(A[0])
elif upper_char == "B":
cards_number_list.append(B[0])
elif upper_char == "C":
cards_number_list.append(C[0])
elif upper_char == "D":
cards_number_list.append(D[0])
elif upper_char == "E":
cards_number_list.append(E[0])
elif upper_char == "F":
cards_number_list.append(F[0])
# 计算所有数字的和
your_number = 0
for number in cards_number_list:
your_number += number
print("你想的数字是{}".format(your_number))
元组
列表和元组在声明时的区别是,列表使用[]进行声明,元组使用()进行声明
在访问时没有区别
列表是可以进行增加、删除和修改的操作的,但是元组不支持。
元组比列表占用的资源更小。
元组声明
t = (1,2,3,4,5)
print(t) # (1, 2, 3, 4, 5)
print(type(t)) # <class 'tuple'>
元组访问
t = (1,2,3,4,5)
for i in t:
print(i)
print(t[1]) # 2
print(t[-1]) # 5
元祖不支持操作(CRUD)
t = (1,2,3,4,5)
# 以下操作会报错:元组不支持这些操作
t.append(6)
t[1] = 6
t.remove(2)
t.pop(1)
字典—存储键值对的容器
字典的声明
字典的键可以是整数、字符串(其实也可以是任意类型,但是这样使用 boolean 和 double 这些类型一般不这么用,这样会非常不具阅读性,容易出错)
字典的值可以是任意数据类型
d = {
"a":"a-value",
"b":"b-value",
"c":[1,2,3],
"d":True,
"e": 2.3
}
print(d) # {'a': 'a-value', 'b': 'b-value'},打印的结果跟声明的字典是一模一样的
print(type(d)) # <class 'dict'>
字典的访问
d = {
"a":"a-value",
"b":"b-value",
1: 11
}
# print(d[0]) # 不能这么访问字典
print(d["a"]) # a-value
print(d[1]) # 11
单独访问字典所有的键和一次性访问字典所有的值
d = {
"a":"a-value",
"b":"b-value",
"c":[1,2,3],
"d":True,
"e": 2.3
}
print(d.keys())
print(d.values())
# 在对象调用的时候,方法加括号,属性不加括号
字典增删改查(CRUD)
如果我们想要向字典中添加元素该怎么操作
my_dict = {
1:"a",
2:"b",
3:"c",
4:"d"
}
# 如果我们想要向字典中添加元素该怎么操作
dict1 = {
"e":5
}
my_dict.update(dict1)
print(my_dict)
my_dict = {
1:"a",
2:"b",
3:"c",
4:"d",
}
my_dict[5] = "ttttt"
print(my_dict) # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'ttttt'}
更新元素,先找到这个元素,然后再对值进行更新
my_dict = {
1:"a",
2:"b",
3:"c",
4:"d"
}
my_dict[1] = "aaaaa"
print(my_dict) # {1: 'aaaaa', 2: 'b', 3: 'c', 4: 'd'}
当我们字典中存在相同键的时候,前边的值会被覆盖,就是说有一个默认的规则,字典中不可以存在相同的键
my_dict = {
1:"a",
2:"b",
3:"c",
4:"d",
1:"dd"
}
print(my_dict) # {1: 'dd', 2: 'b', 3: 'c', 4: 'd'},键 1 的值被最后一个 dd 值覆盖了
我们是否可以根据值更新键,我们不可以根据值来对键进行更新
字典的删除操作
my_dict = {
1:"a",
2:"b",
3:"c",
4:"d",
1:"dd"
}
# 字典的删除操作
my_dict.pop(2)
print(my_dict) # {1: 'dd', 3: 'c', 4: 'd'} 删除掉了键为 2 的