列表:列表 就像在其他语言中声明的动态大小的数组(C++ 中的向量和Java的ArrayList )。列表不必总是同质的,这使其成为Python最强大的工具。列表的主要特点是——
- 列表是Python中可用的数据类型,可以写为方括号之间的逗号分隔值(项目)列表。
- 列表是可变的。即它可以转换为另一种数据类型,并且可以在其中存储任何数据元素。
- 列表可以存储任何类型的元素。
例子:
Python3
# Python3 program to demonstrate
# List
# Creating a List
List = []
print("Blank List: ")
print(List)
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
# Creating a List of strings and accessing
# using index
List = ["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])
Python3
# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
#Creating a Tuple
#with the use of built-in function
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)
Python3
# Python3 program to demonstrate
# Set in Python
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
输出:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
元组:元组 是一个很像列表的Python对象的集合。存储在元组中的值序列可以是任何类型,并且它们由整数索引。元组的值在语法上由“逗号”分隔。虽然不是必须的,但更常见的是通过关闭括号中的值序列来定义元组。元组的主要特征是——
- 元组是Python的一个不可变序列。
- 它无法更改或替换,因为它是不可变的。
- 它在括号()下定义。
- 元组可以存储任何类型的元素。
例子:
蟒蛇3
# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
#Creating a Tuple
#with the use of built-in function
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)
输出:
Initial empty Tuple:
()
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with the use of function:
('G', 'e', 'e', 'k', 's')
设置:在Python,设置 是可迭代、可变且没有重复元素的数据类型的无序集合。与列表相反,使用集合的主要优点是它具有高度优化的方法来检查特定元素是否包含在集合中。 set的主要特点是——
- 集合是Python中元素的无序集合或非预期的项目集合。
- 这里元素加入集合的顺序不是固定的,它可以经常变化。
- 它在花括号下定义{}
- 集合是可变的,但是,只能在其中存储不可变的对象。
例子:
蟒蛇3
# Python3 program to demonstrate
# Set in Python
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
输出:
Initial blank Set:
set()
Set with the use of an Object:
{'G', 's', 'e', 'o', 'r', 'F', 'k'}
Set with the use of List:
{'Geeks', 'For'}
列表、集合和元组之间的差异表
List | Set | Tuple |
---|---|---|
Lists is Mutable | Set is Mutable | Tuple is Immutable |
It is Ordered collection of items | It is Unordered collection of items | It is Ordered collection of items |
Items in list can be replaced or changed | Items in set cannot be changed or replaced | Items in tuple cannot be changed or replaced |