1.list和tuple共同点和区别
现已知的序列有四种:
字符串:str 字节:b 元组:tuple 列表:list
共同点:
1:里面元素都可以储存不同的数据类型
2:都属于序列
不同点:
1:list:可变序列(元素1, 元素2)
2:tuple:不可变序列[元素1,元素二](如果元素里有list,则list中的序列可变)
2.定义一个变量,包含现在所学的数据类型
输入
list_data = [1, 2.2, 1+2j, "and", b'123', True, None, (1, 2, 3), [1]]
print(list_data)
输出
[1, 2.2, (1+2j), 'and', b'123', True, None, (1, 2, 3), [1]]
Process finished with exit code 0
3.目前学到的序列有哪些?
字符串、字节、元组、列表
将除tuple之外的序列转换为tuple
输入
str_data = 'a'
bytes_data = b'1'
list_data = [1, 2]
tuple_data1 = tuple(str_data)
tuple_data2 = tuple(bytes_data)
tuple_data3 = tuple(list_data)
print(tuple_data1, type(tuple_data1))
print(tuple_data2, type(tuple_data2))
print(tuple_data3, type(tuple_data3))
输出
('a',) <class 'tuple'>
(49,) <class 'tuple'>
(1, 2) <class 'tuple'>
将除list之外的序列转换为list
输入
str_var = "123"
list_var1 = list(str_var)
byt_var = b'and'
list_var2 = list(byt_var)
tuple_var = (1, 2, 3)
list_var3 = list(tuple_var)
print(list_var1)
print(list_var2)
print(list_var3)
输出
['1', '2', '3']
[97, 110, 100]
[1, 2, 3]
Process finished with exit code 0
4.tuple中有哪些操作方法
count(self, value, /)
Return number of occurrences of value.
#统计出现的次数
index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
#返回第一个的索引(索引:从0开始)
5.list中有哪些操作方法、
append(self, object, /)
Append object to the end of the list.
#将对象追加到列表的末尾。
clear(self, /)
Remove all items from list.
#从列表中删除所有项目。
copy(self, /)
Return a shallow copy of the list.
#返回列表的浅层副本。
count(self, value, /)
Return number of occurrences of value.
#返回值的出现次数。
extend(self, iterable, /)
Extend list by appending elements from the iterable.
#通过从可迭代对象追加元素来扩展列表。
index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
#返回值的第一个索引。
Raises ValueError if the value is not present.
#如果值不存在,则提高值错误。
insert(self, index, object, /)
Insert object before index.
#在索引之前插入对象。
pop(self, index=-1, /)
Remove and return item at index (default last).
#删除并返回索引处的项目(默认在后面)。
Raises IndexError if list is empty or index is out of range.
#如果列表为空或索引超出范围,则引发索引错误。
remove(self, value, /)
Remove first occurrence of value.
#删除第一个出现的值。
Raises ValueError if the value is not present.
#如果值不存在,则提示值错误。
reverse(self, /)
Reverse *IN PLACE*.
#反向
sort(self, /, *, key=None, reverse=False)
按升序对列表进行排序,并返回 None