Python数据类型(list与tuple)

本文介绍了Python中列表和元组的共同点与区别,列表作为可变序列允许元素的增删,而元组一旦创建则不可修改。展示了如何将字符串、字节和列表转换为元组,以及将字符串、字节和元组转换为列表。同时列举了元组和列表各自的操作方法,包括计数、索引等。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值