只需将序列放在方括号[]内即可创建Python中的列表。与集合不同,list不需要内置函数来创建列表。列表可以包含具有不同位置的重复值,因此,多个不同或重复的值可以在列表创建时作为序列传递。
注 -与集不同,列表可能包含可变元素。
# Python program to demonstrate
# Creation of List
# Creating a List
List = []
print("Intial blank List: ")
print(List)
# Creating a List with
# the use of a String
List = ['GeeksForGeeks']
print("\nList with the use of String: ")
print(List)
# Creating a List with
# the use of multiple values
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple values: ")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'] , ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)
# Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
print("\nList with the use of Mixed Values: ")
print(List)








暂无数据