一、列表的定义:
- 列表作为序列的一种,是一组有顺序的元素的集合。
- 列表是pyrhon中最常用的内置数据内型,用中括号定义,里面的元素以逗号分割开,元素之间没有任何关系,元素可以是任何数据类型。
- 列表被称为打了激素的数组。
定义方法:
运行结果如下图:
二、列表的特性:
1. 列表的索引:
和字符串的索引做类比,只是列表的组成成分是每个数据 元素;而字符串的组成成分是每个元素,如单个字母,数字……
如下图是索引的方法:
运行结果如下:
2.嵌套列表的索引方法:
上图程序执行结果如下:
3. 列表的切片:
类比字符串的切片特性。
4.嵌套列表的切片
:
注意! 索引的方式从列表中取出来的数据类型是数据所对应的数据类型,而切片的方式从列表中取出来的数据还是列表类型。
5. 列表的重复:
6.列表的连接:
7.成员操作符号:
8.列表的for循环:
三、列表元素的增加:
1. 添加一个元素到列表中:
2. 列表中同时添加多个元素的方法:
3. 列表中增加嵌套列表:
4.在列表中指定的索引位置前面插入元素
四、 列表元素的删除:
1.pop的方式:
2.remove: 这种方式直接从内存中将元素删除
3. del:直接从内存中将元素删除
五、列表元素的修改:
1.通过索引修改:
2.通过切片修改:
六、列表元素的查看:
1.统计某元素出现的次数:
2.查看某个元素的索引值:
3.排序查看:按照Ascii码大小排序:
4.打乱列表顺序: random.shuffle()
5.查看列表的长度(有多少个元素):
七、python中常用的内置函数:
1. 求最大值,最小值,和
2.枚举:显示列表中的元素和其对应的索引值:
X
3. zip:将两个字符串或者列表中相同索引位置的两个元素取出来放在一个元组中:
八、列表相关练习:
1. 用户登陆系统,加入防暴力破解功能。系统中有多个用户,且用户名和密码保存在列表中:
users = ['suzhao','xiaoxiong']
passwds = ['123','456']
trylogin = 0
while trylogin < 3 :
username = input('用户名:')
password = input('密码:')
trylogin += 1
if username in users:
index = users.index(username)
passwd = passwds[index]
if passwd == password:
print('登录成功!')
break
else:
print('密码错误!')
print('您已经登录%d次,还剩%d次机会!' %(trylogin,3-trylogin))
else :
print('用户名不存在!')
print('您已经登录%d次,还剩%d次机会!' %(trylogin,3-trylogin))
2.题目如下图所示:
思路:
1.定义一个用户名列表,及对应的密码列表。
2. 定义功能菜单(添加、删除、登陆、查看、退出):
3.编写添加功能模块
username = ['suzhao','xiaoxiong']
passwd = ['123','456']
e = """
1.添加用户
2.删除用户
3.用户登陆
4.用户信息查看
5.退出系统
"""
while True:
print('用户管理系统'.center(24,'*'))
print(e)
opration1 = input('请选择您的操作:')
if opration1 == '1':
print('欢迎进入添加用户界面'.center(30,'*'))
while True:
print('按 a 开始添加,按 q 退出添加!')
opration2 = input('请选择:')
if opration2 == 'a':
user_name = input('请输入您要添加的用户名:')
if user_name in username:
print('该用户名已经存在,请重新想一个!')
opration2 = 'a'
else:
username.append(user_name)
user_passwd = input('请为用户设置密码:')
passwd.append(user_passwd)
print('用户注册成功!')
# print(username)
# print(passwd)
elif opration2 == 'q':
print('退出添加用户界面!')
break
else:
print('输入有误,请重新输入!')
elif opration1 == '2':
print('欢迎进入到删除用户界面'.center(31,'*'))
while True:
print('开始删除请按 s ,退出删除界面请按 q')
opration3 = input('请选择:')
if opration3 == 's':
user_name1 = input('请输入您要删除的用户名:')
if user_name1 not in username:
print('您删除的用户不存在,请好好想想!')
else:
index1 = username.index(user_name1)
username.remove(user_name1)
del passwd[index1]
print('用户删除成功!')
# print(username)
# print(passwd)
elif opration3 == 'q':
print('退出删除用户界面!')
break
else:
print('输入有误,请重新输入!')
elif opration1 == '3':
print('欢迎进入用户登陆界面'.center(30,'*'))
while True:
print('按 s 开始登陆, 按 q 退出登陆界面!')
opration4 = input('请选择:')
if opration4 == 's':
user_name2 =input('请输入用户名:')
if user_name2 in username:
b = username.index(user_name2)
for i in range(1,4):
user_passwd1 = input('请输入用户密码:')
if user_passwd1 != passwd[b]:
print('您输入的密码有误,请重新输入!')
else:
print('登陆成功!')
break
else :
print('您已失败3次,请100s后重试!')
print('将要退出系统')
exit()
else:
print('用户名不存在,请认真想想!')
elif opration4 == 'q':
print('退出登陆界面,谢谢来访!')
break
else :
print('您的输入有错误!请规范操作。')
elif opration1 == '4':
print('欢迎进入用户信息查看界面'.center(32,'*'))
while True:
print('按 s 开始查询, 按 q 退出查询 ')
opration5 = input('请选择:')
if opration5 == 's':
user_name3 = input('您要查看哪个用户的信息呢?请输入用户名:')
if user_name3 in username:
c = username.index(user_name3)
print(user_name3,'的密码是:',passwd[c])
else:
print('您输入的用户不存在,请重新输入!')
elif opration5 == 'q':
print('将要退出信息查询界面……')
break
else:
print('输入格式有误,请重新输入!')
elif opration1 == '5':
print('将要退出系统……')
exit()
else:
print('您的操作有误!请规范操作!')
3.题目如下图:
stack = ['westos',1,3,2.5]
e = """
1.入栈
2.出栈
3.显示栈顶元素
4.退出
"""
while True:
print('栈操作菜单'.center(25,'*'))
print(e)
oparate1 = input('请选择要进行什么栈操作:')
if oparate1 == '1':
while True:
instack = input('请输入要进栈的元素(按 q 停止):')
if instack == 'q':
break
stack.insert(0,instack)
print(stack)
elif oparate1 == '2':
while True:
a = len(stack)
if a > 0:
opration2 = input('按 s 开始出栈,按 q 退出出栈:')
if opration2 == 's':
outstack = stack.pop(0)
print(stack)
elif opration2 == 'q':
print('停止出栈!')
break
else :
print('输入格式有误,请重新输入!')
else:
print('现在是空栈,不能进行出栈操作!')
break
elif oparate1 == '3' :
if len(stack) > 0:
print('栈顶元素是%s' %stack[0])
else:
print('栈中没有元素!')
elif oparate1 == 4:
print('退出!')
break
else :
print('输入格式有误!')