@Time :2021/4/1 19:57
@Author: 小被几
@File :demo01.py
'''
列表
一、成员运算 in、not in
返回布尔值:True、False
# 成员运算
#list_1=[1,2,3,4,5]
#print(1 in list_1)
#print(1 not in list_1)
二、排序
排序原理:通过ASCII码来排序
通过ASCII码值,获取对应的字符
print(chr(97)) ---a
通过字符,获取对应的ASCII码
print(ord('a')) ---97
1、sort:升序排序
**不会生成行的列表,在原有列表的基础上进行排序
可以对字符串、数值进行排序
list_2=[99,45,23,1,67,88,89,6]
list_2.sort()
返回[1, 6, 23, 45, 67, 88, 89, 99]
2、sort(reverse=True):降序排序
list_2=[99,45,23,1,67,88,89,6]
list_2.sort(reverse=True)
返回[99, 89, 88, 67, 45, 23, 6, 1]
3、sorted():内置函数可对所有可迭代对象操作(降序排序、升序排序)
会生成行的列表,不会修改原有列表 有返回值
list_2=[99,45,23,1,67,88,89,6]
list_3 = sorted(list_2,reverse=True)
返回[99, 89, 88, 67, 45, 23, 6, 1]
4、reverse():反序(反转列表,无排序)
list_2=[99,45,23,1,67,88,89,6]
list_2.reverse()
[6, 89, 88, 67, 1, 23, 45, 99]
5、list倒序切片和字符串倒序切片的区别
str_1='hello python'
str_2=str_1[::-1]
print(str_2)
lsit_1=['hello','python']
list_2=lsit_1[::-1]
print(list_2)
返回:nohtyp olleh
['python', 'hello']
三、内置函数使用
1、max() 最大值
2、min() 最小值
3、sum() 总和
4、len() 计算长度
5、input() 模拟输出
res=input('请输出值:')
print(res,type(res))
四、元组
特点:
1、元素不可修改
2、元素内容可以重复
3、如果元组里面只有一个元素,必须加逗号(,),否则类型就不是元组了
获取:通过索引
切片:同列表
元组分类
1、可变元组
元素包含了可变类型,实际上修改的是元素可变类型
tuple_1=(1,['a','b'],2,3,4,5,)
tuple_2=tuple_1[1]
tuple_2.append('[c]')
print(tuple_1)
2、不可变元组
元素为不可变类型
元组常用的方法
1、 max()
# list_3=['a','c','b']
# print(max(list_3))
min()
len()
# list_2 = [99, 45, 23, 1, 67, 88, 89, 6]
# print(max(list_2))
# print(min(list_2))
# print('列表长度:',len(list_2))
# # 1-100
# print(sum(list_2))
set():
去重元组、列表,返回的是集合,需要进行类型强制转换为需要的数据类型
list_1=(1,2,2,3,3,4,4,5)
res=set(list_1)
print(tuple(res))
# 列表去重(转换成集合)
# list_2=[1,2,2,3,3,4,4,5]
# res2=set(list_2)
# print(list(res2))
# 元组去重 set(转换成集合)
# list_1=(1,2,2,3,3,4,4,5)
# res=set(list_1)
# print(tuple(res))
# tuple_1=(1,2,3,4,5)
# print(tuple_1,type(tuple_1))
获取 : 通过索引
# tuple_1=(1,2,3)
# print(tuple_1[0])
切片
# print(tuple_1[0:3])
# print(tuple_1[:-3:-1])
合并
# tuple_2=(1,2,3)
# tuple_3=(4,5,6)
# print(tuple_2+tuple_3)
# print(tuple_2)
# print(tuple_3)
'''