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。

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

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

相关推荐
m0_7349497914 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
t***54414 小时前
Clang 编译器在 Orwell Dev-C++ 中的局限性
开发语言·c++
实心儿儿14 小时前
Linux —— 进程控制 - mini shell
linux·运维·服务器
m0_5145205714 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey15 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
oy_mail15 小时前
QoS质量配置
开发语言·智能路由器·php
oyzz12015 小时前
PHP操作redis
开发语言·redis·php
程序员黄老师15 小时前
Windows文件移动到Linux上的坑
linux·运维·服务器
mounter62515 小时前
【内核前沿】Linux IPC 迎来大变局?POSIX 消息队列增强、io_uring IPC 与 Bus1 十年回归
linux·运维·服务器·kernel·ipc·io_uring
nashane15 小时前
HarmonyOS 6学习:网络能力变化监听与智能提示——告别流量偷跑,打造贴心网络感知应用
开发语言·php·harmony app