列出Python中的数据类型
列表是Python复合数据类型中功能最多的。列表包含用逗号分隔并括在方括号([])中的项目。在某种程度上,列表类似于C中的数组。它们之间的区别是,属于列表的所有项目都可以具有不同的数据类型。
示例
可以使用切片运算符([]和[:])访问列表中存储的值,其中的索引从列表开头的0开始,一直到-1结束。加号(+)是列表串联运算符,星号(*)是重复运算符。例如-
#!/usr/bin/python list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print list # Prints complete list print list[0] # Prints first element of the list print list[1:3] # Prints elements starting from 2nd till 3rd print list[2:] # Prints elements starting from 3rd element print tinylist * 2 # Prints list two times print list + tinylist # Prints concatenated lists
输出结果
这产生以下结果-
['abcd', 786, 2.23, 'john', 70.2] abcd [786, 2.23] [2.23, 'john', 70.2] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
热门推荐
7 祝福语简短七夕
10 贺卡文字同事祝福语简短
11 致姐姐生日祝福语简短
12 关于扬州的祝福语简短
13 幼儿老师过年简短祝福语
14 2026新年祝福语简短语
15 简短佛教新年祝福语大全
16 贺卡祝福语情侣搞笑简短
17 小红书生日祝福语简短
18 新婚祝福语简短十六字