一、列表
数组:存储同一种数据类型的集合 例如:scores = [1,2,3]
列表:可以存储任意类型的数据
1. 创建列表
列表里可以存储不同的数据类型
列表里也可以嵌套列表(列表本身也是一种数据类型)
2. 列表的特性
1)索引
列表索引从0开始,最后一个索引可以用‘-1’ 表示
2)切片
list[start : stop-1 : step]
包左不包右
3)重复
4)连接
5)成员操作符
6)循环遍历
3. 列表里嵌套列表
1)索引
2)切片
4. 判断
1)用户输入月份,判断这个月是哪个季节
2)假定有下面的列表:
name = ['fentiao','fendai','fensi','apple']
输出结果为:'I have fentiao, fendai, fensi and apple.'
5. 列表的增加
1)
2)append :追加一个元素到列表中
3)extend:拉伸,追加多个元素到列表中
4)insert:在指定索引位置插入元素
6. 列表的删除
1)pop:弹出列表中最后一个元素
2)remove:删除指定元素
3)del:关键字,从内存中删除整个列表
7. 列表的修改
1)通过索引,重新赋值
2)通过切片
8. 列表的查看
1)查看出现的次数
2)查看指定元素的索引值(可以指定索引范围查看)
9. 列表的排序
1)
sort:默认按照ascII 码值进行排序
忽略大小写,进行排序
2)将原有的列表顺序打乱
10. 用户管理系统
添加用户:
判断用户是否存在
如果存在,报错
如果不存在,添加用户名和密码分别到列表中
删除用户:
判断用户名是否存在
如果存在,删除
如果不存在,报错
用户登录
用户查看
通过索引编历密码
系统里面有多个用户,用户的信息目前保存在列表里面
users = ['root','westos']
passwd = ['123','456']
用户登录(判断用户登录是否正确)
判断用户是否存在
如果存在
判断用户密码是否正确
如果密码不正确,重新登录,总共有三次机会登录
如果用户不存在
重新登录,总共有三次机会
users = ['admin', 'root', 'westos']
passwd = ['admin', '123', '456']
for i in range(3):
a = input('user: ')
b = input('passwd: ')
if a not in users:
print('not exist')
else:
temp = users.index(a)
if b == passwd[temp]:
print('success')
if a == 'admin':
while True:
print("""
content
1. add user
2. del user
3. show user
4. quit
""")
n = input('please input a num: ')
if n == '1':
while True:
print(' add user ')
inuser = input('please input username(q-quit): ')
if inuser in users:
print('existed')
elif inuser == 'q':
break
else:
inpasswd = input('please input passwd: ')
print('success')
users.append(inuser)
passwd.append(inpasswd)
elif n == '2':
while True:
print(' del user ')
inuser = input('please input username(q-quit): ')
if inuser in users:
temp = users.index(inuser)
users.remove(inuser)
passwd.pop(temp)
print('success')
elif inuser == 'q':
break
else:
print('not exist')
elif n == '3':
while True:
print(' show user ')
print('\n'.join(i for i in users))
inuser = input('please input username(q-quit): ')
if inuser in users:
temp = users.index(inuser)
print('%s password: %s' % (inuser, passwd[temp]))
elif inuser == 'q':
break
else:
print('not exist')
elif n == '4':
break
else:
print('error')
break
else:
print('passwd false')
else:
print('3 times failed')