Python学习 Day02 列表

列表

  • 变量存储的是一个对象的引用

  • 列表动态分配一个连续的空间,可存储多个不同数据类型的元素

1.列表的地址

# encoding: utf-8

a=10 #变量存储的是一个对象的引用
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)
140084787793864
<type 'list'>
['hello', 'world', 98]
sandbox> exited with status 0

2.列表的创建方式

# encoding: utf-8

#列表创建的第一种方式,使用[]
lst1=['hello','world',98]
print(lst1)
print(lst1[0],lst1[-3])

#列表创建的第二种方式,使用内置函数list()
lst2=(['hello','world',98])
['hello', 'world', 98]
('hello', 'hello')

3.查询列表中元素的索引

  • 单个元素的索引
# encoding: utf-8

#列表中有多个相同元素,只返回第一个元素的索引
lst1=['hello','world',98,'hello']
print(lst1.index('hello'))

#查找列表中不存在的元素会报错
print(lst1.index(hell)
  • 多个元素的切片操作
# encoding: utf-8

lst=[10,20,30,40,50,60,70,80]

print(lst[1:6:1])
print('原列表:',id(lst))

#切片后的ID
print('切片后:',id(lst[1:6:1]))
print(id(lst[1:6]))

#不同步长
print(lst[1:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值