一、列表
my_list = ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
1.增加
1.1 append演示(只能添加一个元素(元素类型不限制,可以是一个容器),添加到列表的尾部)
my_list.append("a") # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'a']
my_list.append(2) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'a', 2]
my_list.append([9,8,7,"添加"]) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'a', 2, [9, 8, 7, '添加']]
my_list.append((1,2,3)) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'a', 2, [9, 8, 7, '添加'], (1, 2, 3)]
# 错误示范,append一次只能添加一个元素
# my_list.append(1,2,3) # TypeError: list.append() takes exactly one argument (3 given)
1.2 insert演示(添加元素到指定下标位置)
my_list.insert(2,"haha") # ['h', 'e', 'haha', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
my_list.insert(100,"超出范围的下标添加") # ['h', 'e', 'haha', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '超出范围的下标添加']
my_list.insert(2,[1,2]) # ['h', 'e', [1, 2], 'haha', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '超出范围的下标添加']
# 错误,insert每次都要提供两个参数,第一参数为下标,第二个参数为添加的元素
# my_list.insert(2) # TypeError: insert expected 2 arguments, got 1
1.3 extend演示(追加一批元素到列表尾部,参数变量只能提供一个,提供的参数变量必须是可迭代类型,添加的时候会将变量内容一个一个添加进去)
my_list.extend("haha") # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'h', 'a', 'h', 'a']
my_list.extend("m") # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'h', 'a', 'h', 'a', 'm']
my_list.extend([1,2]) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 'h', 'a', 'h', 'a', 'm', 1, 2]
# 错误,extend只能使用一个参数
# my_list.extend(1,2,3) # TypeError: list.extend() takes exactly one argument (3 given)
# 错误,extend只能添加可迭代对象
# my_list.extend(5) # TypeError: 'int' object is not iterable
2.修改(修改指定下标对应的元素,未提供下标以及超出列表范围的下标是不允许的,会报错)
my_list[2] = "修改元素" # ['h', 'e', '修改元素', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
# 未提供下标,以及超出列表范围的下标是不能修改的
# my_list[100] = "超出下标的修改" # IndexError: list assignment index out of range
# my_list[] = "未提供下标的修改" # SyntaxError: invalid syntax
3.查询
3.1 index演示(查找对应范围里面元素的下标,列表不存在的元素是无法查询的,只能查找一个元素的下标,如果符合的元素存在多个,返回的是第一个符合条件的下标,起始下标和结束下标均可省略,都省略的会查找整个列表,省略结束下标的会查找从起始下标开始到列表结尾)
a = my_list.index("l") # 2
a = my_list.index("l",3,5) # 3
a = my_list.index("l",8) # 9
# a = my_list.index("l",8,9) # ValueError: 'l' is not in list
# 错误,列表不存在的元素是无法查询的,只能查找一个元素的下标,如果符合的元素存在多个,返回的是第一个符合条件的下标
# a = my_list.index("haha") # ValueError: 'haha' is not in list
# a = my_list.index("l","l") # TypeError: slice indices must be integers or have an __index__ method
3.2count演示(统计整个列表中查找元素的个数,没有的则返回0)
num = my_list.count("l") # 3
num = my_list.count(5) # 0
# 错误,count只能统计整个列表的符合条件的元素个数,无法指定范围查找
# num = my_list.count("l",2,3) # TypeError: list.count() takes exactly one argument (3 given)
4.删除
4.1pop演示(提取指定下标的元素,然后将元素从列表中删除,未提供下标的话会提取列表最后的一个元素并删除,下标不能超出列表的范围)
s = my_list.pop(2) # my_list:['h', 'e', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] s: l
s = my_list.pop() # my_list:['h', 'e', 'l', 'o', ' ', 'w', 'o', 'r', 'l'] s: d
# 要提取的下标不能超出列表范围
# s = my_list.pop(100) # IndexError: pop index out of range
4.2remove演示(删除指定元素,如果这个元素在列表内存在多个,则删除第一个,无法一次性全部删除,指定的元素在列表中必须存在)
my_list.remove("l") # ['h', 'e', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
# 错误,remove只能且必须提供一个参数,且参数必须是在列表内包含的元素
# my_list.remove() # TypeError: list.remove() takes exactly one argument (0 given)
# my_list.remove("haha") # ValueError: list.remove(x): x not in list
4.3clear 演示(清除列表中的所有元素)
my_list.clear() # []
# 错误
# my_list.clear("h") # TypeError: list.clear() takes no arguments (1 given)
4.4del 演示(删除列表变量,删除后变量不存在)
del my_list
# print(my_list) # NameError: name 'my_list' is not defined
二、元组
元组属于不可修改类型,对应的方法较少,查找方法index,count
my_tuple = ('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
a = my_tuple.index("l") # 2
a = my_tuple.index("l",5) # 9
num = my_tuple.count("l") # 3
num = my_tuple.count("haha") # 0
三、字符串
字符串为不可变类型,增删改只会生成新的字符串,原字符串不会发生改变
1.增加
1.1 +(将两个字符串进行拼接)
my_str = "hello world" # hello world
add_str = "要添加的字符串" # 要添加的字符串
new_str = my_str + add_str # hello world要添加的字符串
1.2 *(将同一个字符串重复拼接n次)
my_str = "hello world" # hello world
new_str = my_str * 2 # hello worldhello world
2.删除
replace
my_str = "hello world"
new_str = my_str.replace("l","") # heo word
3.修改
3.1upper(全改大写)
my_str = "hello world"
new_str = my_str.upper() # HELLO WORLD
3.2ower(全改小写)
my_str = "HELLO WORLD"
new_str = my_str.lower() # hello world
3.3strip(去除首尾字符(符合条件的字符都要去除))
my_str = "12hello world21"
new_str = my_str.strip("12") # hello world
3.4split(切割)
my_str = "hello world"
new_list = my_str.split(" ")
print(new_list) # ['hello', 'world']
3.5replace(替换)
my_str = "hello world"
new_str = my_str.replace("l","替换") # he替换替换o wor替换d
4.查询
4.1count(计算元素出现了多少次)
my_str = "hello world"
num = my_str.count("l")
print(num) # 3
4.2index(查元素,只查第一个)
my_str = "hello world"
a = my_str.index("l")
print(a) # 2
a = my_str.index("ld")
print(a) # 9
4.3find(找到返回下标,找不到返回-1)
my_str = "hello world"
a = my_str.find("haha") # -1
a = my_str.find("wor") # 6
4.4islower(判断是否全部为小写)
my_str = "hello world"
a = my_str.islower()
print(a) # True
4.5isupper(判断是否全部为大写)
my_str = "Hello World"
a = my_str.isupper()
print(a) # False
4.6isdigit(判断是否全部为数字)
my_str = "123"
a = my_str.isdigit()
print(a) # True
4.7isalpha(判断是否全部为字母)
my_str = "helloworld"
a = my_str.isalpha()
print(a) # True
补充:序列类型切片
s[起始下标:结束下标:步长]
包含起始下标,到结束下标(不包含)为止,步长代表取元素的间隔
s = "hello world"
print(s[0:5:1]) # hello