列表: 列表是可变的有序集合数据类型,这意味着它可以轻松修改,我们可以更改其数据值,并且可以对列表进行索引、切片和更改,并且可以使用列表中的索引值访问每个元素.以下是列表的主要特征:
- 该列表是数据类型的有序集合。
- 该列表是可变的。
- 列表是动态的,可以包含不同数据类型的对象。
- 列表元素可以通过索引号访问。
例子:
Python
# Python program to demonstrate List
list = ["mango", "strawberry", "orange",
"apple", "banana"]
print(list)
# we can specify the range of the
# index by specifying where to start
# and where to end
print(list[2:4])
# we can also change the item in the
# list by using its index number
list[1] = "grapes"
print(list[1])
Python
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")
Python
tuple = ("orange","apple","banana")
print(tuple)
# we can access the items in
# the tuple by its index number
print(tuple[2])
#we can specify the range of the
# index by specifying where to start
# and where to end
print(tuple[0:2])
输出 :
['mango', 'strawberry', 'orange', 'apple', 'banana']
['orange', 'apple']
grapes
大批: 数组是存储在连续内存位置的项目的集合。这个想法是将多个相同类型的项目存储在一起。这使得通过简单地将偏移量添加到基值,即数组的第一个元素的内存位置(通常由数组的名称表示)来更容易地计算每个元素的位置。以下是 Array 的主要特征:
- 数组是相似数据类型的有序集合。
- 数组是可变的。
- 可以使用数组的索引号访问数组。
例子:
Python
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")
输出:
The new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
元组: 元组是一种有序且不可变的数据类型,这意味着我们不能更改它的值,并且元组写在圆括号中。我们可以通过引用方括号内的索引号来访问元组。以下是元组的主要特征:
- 元组是不可变的,可以存储任何类型的数据类型。
- 它是使用 () 定义的。
- 它无法更改或替换,因为它是不可变的数据类型。
例子:
Python
tuple = ("orange","apple","banana")
print(tuple)
# we can access the items in
# the tuple by its index number
print(tuple[2])
#we can specify the range of the
# index by specifying where to start
# and where to end
print(tuple[0:2])
输出 :
('orange', 'apple', 'banana')
banana
('orange', 'apple')
List、Array 和 Tuple 之间的差异表:
List |
Array |
Tuple |
---|---|---|
List is mutable | Array is mutable | Tuple is immutable |
A list is ordered collection of items | An array is ordered collection of items | A tuple is an ordered collection of items |
Item in the list can be changed or replaced | Item in the array can be changed or replaced | Item in the tuple cannot be changed or replaced |
List can store more than one data type | Array can store only similar data types | Tuple can store more than one data type |