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。

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

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

相关推荐
helloweilei10 小时前
python 抽象基类
python
用户83562907805110 小时前
Python 实现 PPT 转 HTML
后端·python
Johny_Zhao11 小时前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
haibindev11 小时前
在 Windows+WSL2 上部署 OpenClaw AI员工的实践与踩坑
linux·wsl2·openclaw
zone773916 小时前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone773916 小时前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒1 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习1 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽1 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama