首页 > 编程语言 >Python——索引与切片

Python——索引与切片

时间:2022-09-21 19:02:23浏览次数:72  
标签:Python list 切片 索引 str print Output tup

#索引与切片
##1.序列 序列:list,tuple,str 其中list是可变序列 typle,str是不可变序列
#修改序列的值
list = [3,4,5]
tup = (3,4,5)
str = '345'
list[1] = 99
list
output:[3, 99, 5]


tup[1] = 99
tup
  Output:TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 tup[1] = 99
      2 tup

TypeError: 'tuple' object does not support item assignment

str[1] = 'y'
str

 Output: 
TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 str[1] = 'y'
      2 str

TypeError: 'str' object does not support item assignment
!!!!此处不是代码错误,纯纯为了演示不可变序列不能修改序列的值
###2.索引
list=[1,2,3,4,5,[1,2,3,4,5]]
print(list)
print(list[0],list[1])
###python中的索引是从0开始的,list[0]代表第一个索引,list[1]代表第二个索引
 Output:   [1, 2, 3, 4, 5, [1, 2, 3, 4, 5]]
  1 2
len(list)
Output:2 list=[1,2,3,4,5,[1,2,3,4,5]] print(list) print(list[-1],list[-2]) ###倒数第二个数用list[-1]表示,倒数第二个数用list[-2]表示 Output: [1, 2, 3, 4, 5, [1, 2, 3, 4, 5]] [1, 2, 3, 4, 5] 5 tup=['a','b','c','d'] print(tup[2],tup[-1])
Output:c d #字符串也是索引 str='abcdefg' print(st[0],st[-2]) Output:a f

 

 

###3.切片 切片用:表示 切片是一个左闭又开的区间
list=[1,2,3,4,5,[1,2,3,4,5]]
print(list[2:3])#索引2到索引3,但不包含索引3
print(list[:2])#索引0到索引2,但不包含索引2
print(list[1:])#索引1到最后一个元素,且包含最后一个元素
print(list[1:-1])#索引1到最后一个元素,但不包括最后一个元素
  Output:  [3]
[1, 2]
[2, 3, 4, 5, [1, 2, 3, 4, 5]]
[2, 3, 4, 5]
tup=['a','b','c','d']
print(tup[1:3])
print(tup[0:])
print(tup[0:3])
  Output: ['b', 'c']
['a', 'b', 'c', 'd']
['a', 'b', 'c']
str='abcdefg'
print(str[0:])
print(str[0:6])
print(str[1:4])
  Output: abcdefg
abcdef
bcd

 

 
###4.步长
list=[10,20,30,40,50,60,70,80,90]
print(list[::3])
###list[a:b:n]  对于list序列,从索引a至索引b,但不包含索引b,其中间隔为n(步长) 
  Output: [10, 40, 70]


###列表的常用方法
list=[10,20,30,40,50,60,70,80,90] print(len(list))###长度 Output:9 list.append(99) list.append(['a','b'])####append是可以直接在原数据后边➕列表 print(list) Output: [10, 20, 30, 40, 50, 60, 70, 80, 90, 99, ['a', 'b']] list=[10,20,30,40,50,60,70,80,90] list.extend(['a','b'])###在原数据后方直接追加另一个序列中的多个值 print(list) Output: [10, 20, 30, 40, 50, 60, 70, 80, 90, 'a', 'b'] list=[10,20,30,40,50,60,70,80,90] list.insert(2,1000) print(list) Output: [10, 20, 1000, 30, 40, 50, 60, 70, 80, 90]

 



 

标签:Python,list,切片,索引,str,print,Output,tup
From: https://www.cnblogs.com/dd0016/p/16716707.html

相关文章

  • python1
    python课程总结(1)1.今日内容概要typora软件--下载与安卓--文件路径--主要功能markdown语法网络博文编写教程计算机的本质计算机的五大组成部分计算机......
  • python-Typora的安装及应用
      Typora软件是一个文本编辑器,可以记录文字类似于word,可以用来日常记笔记,学习代码,界面简洁,清晰,方便日常工作。安装        安装时尽量装在其他盘不要......
  • MySQL索引查询条件使用函数导致索引失效
     索引失效EXPLAINSELECTdt.nameASdeviceName,su.`name`ASuserName,date_format(co.upload_time,'%Y%m%d')astimeFROMtb_cust_ordercoI......
  • python django request接收UIRL传参
    POST方式:ifrequest.method=='POST':body_str=request.body.decode('utf-8')post_data=parse_qs(body_str)post_dict={}......
  • Python3-eg
    实例01(判断输入的是不是黄蓉所说的数)1print("今有物不知数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?\n")2number=int(input("请输入您认为符合条件的数:"))......
  • python-D1-typora软件和计算机入门1
    一typora软件typora是一款目前非常火爆文本编辑器1.1安装尽量安装在非系统盘符及设置为短路径,方便后面查找1.2文件路径在计算机上就是一个资源的定位坐标,表现为具......
  • python学习日记
    今日课堂内容总结:计算机的五大组成部分及功能:1.控制器:控制计算机各个硬件的工作2.运算器:进行数学运算以及逻辑运算3.存储器:分为短期记忆和长期记忆4.输出设备:接受外......
  • Ubuntu上安装python连接oracle数据库的包
    转载地址 https://blog.csdn.net/buluxianfeng/article/details/125376955  wgethttps://download.oracle.com/otn_software/linux/instantclient/217000/oracle-......
  • 1、python入门篇 typora的安装和常用语法及对计算机的认知
    一、typora软件typora是一款Markdown编辑器和阅读器风格极简/多种主题/支持macOS,Windows及Linux实时预览/图片与文字/代码块/数学公式/图表目录大纲......
  • python学习day01
    python课总结day011.今日内容概要typora软件安装markdown语法typora软件功能介绍网络博文编写教程计算机的本质计算机的五大组成部分计算机的三大核心硬件2.typ......