1. 字典的定义
dict={
'messi':
'football player',
'kobe':
'basketball palyer'}
每个key值对应一个value,且key不能重复。
2. 遍历字典
for key
in dict.
keys():
print(key)
for value in dict.values():
print(
value)
3. 判断某值是不是在字典中
if key in dict:
#打印所对应的value值
print(dict[
key])
4. 举例说明
'''
根据输入的账号密码,检测密码是否正确,或用户是否存在
其中,密码运用MD5储存口令
'''
import hashlib
db = {
'micheal':
'e10adc3949ba59abbe56e057f20f883e',
'bob':
'878ef96e86145580c38c87f0410ad153',
'alice':
'99b1c2188db85afee403b1536010c2c9'}
def login(user,password):
md5 = hashlib.md5()
md5.update(password.encode(
'utf-8'))
MD5password = md5.hexdigest()
if user
in db:
if db[user]==MD5password:
print(
"welcome")
return True
else:
print(
"wrong password")
return False
else:
print(
"no such user")
login(
'micheal',
'123456')
输出结果:
welcome