python 基础(一)

python list

本篇主要记录下python 中 list的打印输出.

python 复制代码
list = ['1','2','3','4','5']
print(list[1:])
print(list[1])

这个很简单 ,我们都知道输出的是:

shell 复制代码
['2', '3', '4', '5']
2

但是如果是下面的代码呢?

python 复制代码
list = ['1','2','3','4','5']
print(list[11:])
print(list[11])

测试输出如下:

复制代码
[]
Traceback (most recent call last):
  File "/home/zh/workSpace/python/Test1/venv/test.py", line 12, in <module>
    print(list[11])
IndexError: list index out of range

list[1:]的输出为[1],表示从索引为1的位置开始,一直到列表的末尾的所有元素.

list[1]的输出为2,表示获取列表中索引为1的元素,即2。

前者返回的是列表,后者是单独的元素.

所以 当获取超过下标的值时,前者返回了[]. 后者抛出了下标越界.

相关推荐
脆皮炸鸡755几秒前
大山之二:文件系统(Ext系列)
linux·开发语言·经验分享·学习方法
杨超越luckly几秒前
Python应用指南:百度热搜数据
python·百度·html·数据可视化
打工人小夏几秒前
使用finalshell在新服务器上部署前端页面
linux·服务器·前端·vue.js
少司府2 分钟前
C++基础入门:vector深度解析(七千字深度剖析)
c语言·开发语言·数据结构·c++·容器·vector·顺序表
Zhu7583 分钟前
软件更新-openssh和openssl-centos
linux·运维·centos
星空椰3 分钟前
Python 使用飞书 API 获取子部门列表接口信息
python·pandas·飞书
yqcoder3 分钟前
突破性能瓶颈:深入理解 JavaScript TypedArray
java·开发语言·javascript
故事还在继续吗4 分钟前
嵌入式Linux基础知识
linux·运维·服务器
yqcoder4 分钟前
JS 中的“空”之双雄:null vs undefined
开发语言·前端·javascript
ch.ju4 分钟前
Java Programming Chapter 3——Traversal of array
java·开发语言