python列表、元组与集合

本文主要探讨Python编程中的三种数据结构——列表、元组和集合。首先介绍了列表的基本操作,如创建、索引、切片、类型转换和重复元素处理,接着会涉及列表的成员操作符应用。
摘要由CSDN通过智能技术生成

python列表
一、列表的创建与操作
1.创建列表

a = []    ##元素类型可为int,float,complex,str,list,tuple
b = [a, 1, True, 3j + 2, "hi"]
c = [[1, 2, 3, 4], [a, b], 233, "hello"]
d = [a, b, c ]

2.列表的索引与切片

>> c = [[1, 2, 3, 4], [5,6], 233, "hello"]">>>> c
[[1, 2, 3, 4], [5, 6], 233, 'hello']
>>> type(c)    ##显示类型
<class 'list'>
>>> c[0][-1]
4
>>> c[-1][3]
'l'
>>> c[-2]
233

3.强制转换

>>> s=list(range(5))  ##采用内置方法list()强制转换
>>> s
[0, 1, 2, 3, 4]
>>> s[3]
3

4.列表的重复

>>> list=['a',3,True,'hello']
>>> list*3
['a', 3, True, 'hello', 'a', 3, True, 'hello', 'a', 3, True, 'hello']

5.列表的成员操作符

>>> list
['a', 3, True, 'hello']
>>> 'a' in list   ##判断元素是否在列表中
True
>>> 'test' in list
False
>>> True in list
True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值