1.list和tuple共同点和区别
共同点:都属于序列
不同点:
tuple:一旦初始化就不能修改,不可变数据类型,只有1个元素的tuple定义时必须加一个逗号
list:可以随时添加和删除其中的元素,可变数据类型
2.定义一个变量,包含现在所学的数据类型
list_data = [1, 1.2, b'233', True, None, 1+2j, '6', (6, 6, 6), [1]]
print(list_data, type(list_data))
E:\Python\python.exe E:/Python/NewProject_hwj/define_var.py
[1, 1.2, b'233', True, None, (1+2j), '6', (6, 6, 6), [1]] <class 'list'>
Process finished with exit code 0
3.目前学到的序列有哪些?
字符串str,字节bytes,元组tuple,列表list
将除tuple之外的序列转换为tuple
str_data = 'hello'
bytes_data = b'2333'
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))
E:\Python\python.exe E:/Python/NewProject_hwj/define_var.py
('h', 'e', 'l', 'l', 'o') <class 'tuple'>
(50, 51, 51, 51) <class 'tuple'>
(1, 2) <class 'tuple'>
Process finished with exit code 0
将除list之外的序列转换为list
str_data = 'hello'
bytes_data = b'2333'
tuple_data = (1, 2)
list_data1 = list(str_data)
list_data2 = list(bytes_data)
list_data3 = list(tuple_data)
print(list_data1, type(list_data1))
print(list_data2, type(list_data2))
print(list_data3, type(list_data3))
E:\Python\python.exe E:/Python/NewProject_hwj/define_var.py
['h', 'e', 'l', 'l', 'o'] <class 'list'>
[50, 51, 51, 51] <class 'list'>
[1, 2] <class 'list'>
Process finished with exit code 0
4.tuple中有哪些操作方法
def count(self, *args, **kwargs): # real signature unknown
""" Return number of occurrences of value. """
pass
def index(self, *args, **kwargs): # real signature unknown
"""
Return first index of value.
Raises ValueError if the value is not present.
"""
pass
统计值出现的次数;返回值的第一个索引
5.list中有哪些操作方法
class list(object):
"""
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
"""
def append(self, *args, **kwargs): # real signature unknown
""" Append object to the end of the list. """
pass
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all items from list. """
pass
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of the list. """
pass
def count(self, *args, **kwargs): # real signature unknown
""" Return number of occurrences of value. """
pass
def extend(self, *args, **kwargs): # real signature unknown
""" Extend list by appending elements from the iterable. """
pass
def index(self, *args, **kwargs): # real signature unknown
"""
Return first index of value.
Raises ValueError if the value is not present.
"""
pass
def insert(self, *args, **kwargs): # real signature unknown
""" Insert object before index. """
pass
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass
def remove(self, *args, **kwargs): # real signature unknown
"""
Remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
def reverse(self, *args, **kwargs): # real signature unknown
""" Reverse *IN PLACE*. """
pass
def sort(self, *args, **kwargs): # real signature unknown
"""
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them,
ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
"""
pass