# -*- coding: utf-8 -*-
# @Time : 2021/9/26 19:23
# @Author : 李新宇
# @FileName: demo1.py
# @Software: PyCharm
a=10 #变量引用的是一个1对象的引用
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)
# -*- coding: utf-8 -*-
# @Time : 2021/9/26 20:17
# @Author : 李新宇
# @FileName: demo2.py
# @Software: PyCharm
'''创建列表的第一种方式,使用[]'''
lst=['hello','world',98,'hello']
print(lst)
print(lst[-3])
'''创建列表的第二种方式,使用内置函数list()'''
lst2=list(['hello','world',98])
# -*- coding: utf-8 -*-
# @Time : 2021/9/26 20:29
# @Author : 李新宇
# @FileName: demo3.py
# @Software: PyCharm
lst=['hello','world',98,'hello']
print(lst.index('hello')) #如果列表中有相同元素,只返回列表中第一个元素的索引
#print(lst.index('lixinyu'))#ValueError: 'lixinyu' is not in list
#print(lst.index('hello',1,3))#ValueError: 'hello' is not in list,左闭右开
print(lst.index('hello',1,4)) #3
# -*- coding: utf-8 -*-
# @Time : 2021/9/26 20:36
# @Author : 李新宇
# @FileName: demo4.py
# @Software: PyCharm
'''获取列表当中指定的元素'''
lst=['hello','world',98,'hello','world',234]
#希望获取索引为2的元素
print(lst[2])
#希望获取索引为-3的元素
print(lst[-3])
# -*- coding: utf-8 -*-
# @Time : 2021/9/26 20:42
# @Author : 李新宇
# @FileName: demo5.py
# @Software: PyCharm
lst=[10,20,30,40,50,60,70,80]
#start=1,stop=6,step=1
#print(lst[1:6:1])
print('原列表:',id(lst))
lst2=lst[1:6:1]
print('切的片段:',id(lst2))
print(lst[1:6])#默认步长为1
print(lst[1:6:])#默认步长为1
print(lst[1:6:2])
#start=1,stop=默认,step=1
print(lst[1::1])#stop无,从start遍历
print('------步长为负数的情况-----')
#
print(lst[::-1]) #[80, 70, 60, 50, 40, 30, 20, 10]
#start=7,stop省略 step=-1
print(lst[7::-1]) #[80, 70, 60, 50, 40, 30, 20, 10]
#start=6,stop=0省略 step=-2
print(lst[6:0:-2])#[70, 50, 30]
# -*- coding: utf-8 -*-
# @Time : 2021/9/26 21:02
# @Author : 李新宇
# @FileName: demo6.py
# @Software: PyCharm
# print('p' in 'python')#True
# print('k' in 'python')#False
#列表元素的判断与遍历
lst=[10,20,'python','hello']
print(10 in lst)#True
print(10 not in lst)#False
print('------------')
for item in lst:
print(item)
# -*- coding: utf-8 -*-
# @Time : 2021/9/26 21:06
# @Author : 李新宇
# @FileName: demo7.py
# @Software: PyCharm
#向列表的末尾添加一个元素
lst=[10,20,30]
print('添加元素之前',lst,id(lst))
lst.append(100)
print('添加元素之后',lst,id(lst))
lst2=['hello','world']
# lst.append(lst2) #将lst2作为一个元素添加到列表的末尾
# print(lst)
lst.extend(lst2) #extend扩展,向列表的末尾一次性添加多个元素
print(lst) #[10, 20, 30, 100, 'hello', 'world']
#在任意位置上添加元素insert
lst.insert(1,90)
print(lst)#[10, 90, 20, 30, 100, 'hello', 'world']
lst3=[True,False,'hello','world']
#在任意的位置上添加n多个元素,切片,从start切掉剩下的,用新的列表替换
lst[1:]=lst3 #[10, True, False, 'hello', 'world']
print(lst)
# -*- coding: utf-8 -*-
# @Time : 2021/9/27 9:51
# @Author : 李新宇
# @FileName: demo8.py
# @Software: PyCharm
lst=[20,30,40,50,60,30]
lst.remove(30)#从列表中移除一个元素,如果有重复元素,只移除第一个元素
print(lst)
#lst.remove(100) #ValueError: list.remove(x): x not in list
#pop()根据索引去移除元素
lst.pop(1)
print(lst)
#lst.pop(9)#IndexError: pop index out of range,如果指定的索引位置不存在,将抛出异常
#print(lst)
lst.pop()#如果不指定参数,将删除列表中的最后一个元素
print(lst)
print('--------切片操作,删除至少一个元素,将产生一个新的列表对象-------')
lst2=[10,40,50,60]
new_lst=lst2[1:3] #左闭右开
print(new_lst)
print(lst2)
print('-----不产生新的列表对象,而是删除原列表中的内容------')
lst2[1:3]=[]
print(lst2)
'''-----clear清除列表中的所有元素-------'''
lst2.clear()
print(lst2)
'''------del语句将列表对象删除--------'''
# del lst2
# print(lst2) #NameError: name 'lst2' is not defined
# -*- coding: utf-8 -*-
# @Time : 2021/9/27 10:05
# @Author : 李新宇
# @FileName: demo9.py
# @Software: PyCharm
lst=[10,20,30,40]
#一次修改一个值
lst[2]=100
print(lst)
lst[1:3]=[300,400,500,600]
print(lst)
# -*- coding: utf-8 -*-
# @Time : 2021/9/27 10:09
# @Author : 李新宇
# @FileName: demo10.py
# @Software: PyCharm
lst=[20,40,10,98,54]
print('----排序前的列表----')
print(lst,id(lst))
#开始排序,调用列表对象的sort方法,升序排序
lst.sort()
print('-------默认sort()升序排序后的列表---------')
print(lst,id(lst))
#通过指定关键字参数,将列表中的元素进行降序排序
lst.sort(reverse=True) #reverse=True表示降序排序
print(lst)
lst.sort(reverse=False)#reverse=False表示升序排序
print(lst)
print('------使用内置函数sorted()对列表进行排序,将产生新的列表--------')
lst=[20,40,10,98,54]
print('原列表',lst)
new_lst=sorted(lst)
print(lst)
print(new_lst)
#指定关键词参数,实现列表元素降序排序
new_lst=sorted(lst,reverse=True)
print(new_lst)
# -*- coding: utf-8 -*-
# @Time : 2021/9/27 10:22
# @Author : 李新宇
# @FileName: demo11.py
# @Software: PyCharm
lst=[i for i in range(1,10) ]
print(lst)
'''列表中的元素的值为2,4,6,8,10'''
lst=[i*2 for i in range(1,6)]
print(lst)