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。

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

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

相关推荐
前端付豪30 分钟前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 小时前
Pydantic配置管理最佳实践(一)
python
哈基咪怎么可能是AI7 小时前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
阿尔的代码屋7 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
十日十行1 天前
Linux和window共享文件夹
linux
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python