笨方法学习Python-习题34: 访问列表的元素
来源:互联网 发布:网络直播市场分析 编辑:程序博客网 时间:2024/05/22 10:26
如何获取列表元素:
列表的每一个元素都有一个地址,改地址称为“索引(index)”,索引是以“0”开头,这类数字被称为“基数”,意味着你可以抓取任意元素。
我们日常使用的计数,是从1开始的,而编程语言的计数,是从0开始的。也就是说,列表中最靠前的元素是第0号元素,而不是我们日常生活中的第1号元素。
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下所示:
list1 = ['Google', 'baidu', 1997, 2000]list2 = [1, 2, 3, 4, 5, 6, 7 ] print ("list1[0]: ", list1[0])print ("list2[1:5]: ", list2[1:5])
理解基数与序数后,我们做一下练习:
animals = ['bear','python','peacock','kangaroo','whale','platypus']print("The animal at 1: %s" % animals[1])print("The 3rd animal: %s" % animals[2])print("The 1st animal: %s" % animals[0])print("The animal at 3: %s" % animals[3])print("The 5th animal: %s" % animals[4])print("The animal at 2: %s" % animals[2])print("The 6th animal: %s" % animals[5])print("The animal at 4: %s" % animals[4])
阅读全文
0 0
- 笨方法学习Python-习题34: 访问列表的元素
- 笨方法学Python 习题 34: 访问列表的元素
- 笨方法学python 习题34(访问列表元素)习题35(作死小游戏)
- 习题34 访问列表的元素
- 笨方法学习Python-习题39: 列表的操作
- 笨方法学习Python-习题32: 循环和列表
- 笨方法学Python 习题 38: 列表的操作
- Python学习_我该怎样访问列表中的元素
- Learn Python The Hard Way学习(34) - 访问列表中的元素
- 《笨方法学python》习题43的学习笔记
- 笨方法学习Python-习题25: 更多更多的练习
- 笨方法学习Python-习题40: 字典, 可爱的字典
- 005_006 Python 随机访问列表的元素
- 笨方法学Python 习题 32: 循环和列表
- 笨方法学习Python-习题11:提问
- 笨方法学习Python-习题42: 物以类聚
- 笨方法学习Python-习题43、44
- 笨方法学python 习题32(循环和列表)习题33(while循环)
- 四 了解安全问题(Security 面板)
- 布尔代数与编码
- 搭建Windows SVN服务器
- [Oracle 11g r2(11.2.0.4.0)]集群CHM工具介绍
- Freemarker基础
- 笨方法学习Python-习题34: 访问列表的元素
- 第7周【项目6
- freemarker 导出Excel
- git的个人学习总结(1)
- ajax返回字符串,前台页面比较为false
- ImageMagick简介、GraphicsMagick、命令行使用示例
- mn Adapter2
- php 计算执行文件时长
- 虚拟机中创建虚拟机报错:已将该虚拟机配置为使用64位,此主机不支持 Intel VT-x。