热线电话:13121318867

登录
2019-02-14 阅读量: 661
python如何创建一个列表

只需将序列放在方括号[]内即可创建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)

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子