AI学习笔记——Python的几个练习题

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## AI学习笔记——Python的几个练习题

hongtao2018 2018-08-09 22:38:00 浏览508

1. 将字典中大于2的值过滤掉。
``````#Filter out values of equal or greater than 2
#Note that for Python 2 you will have to use iteritems
d = {"a": 1, "b": 2, "c": 3}
``````
1. 读取输入的一句话中的单词数。

2. a,b 中的对应数字想加并输出结果。

``````#Print out in each line the sum of homologous items from the two sequences

a = [1, 2, 3]
b = (4, 5, 6)
``````
1. 发现下面代码中的错误。
``````#Please fix the script so that it returns the user submited first name for the first %s
#and the second name for the second %s

firstname = input("Enter first name: ")
secondname = input("Enter second name: ")
print("Your first name is %s and your second name is %s" % firstname, secondname)
``````
1. 打印出第三个“employee”的“LastName”,并添加一个“Albert Bert”的"employee"
``````d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}
``````
1. 打印a中的index 和 item 输出格式如下
Item 1 has index 0
Item 2 has index 1
Item 3 has index 2
``````a = [1, 2, 3]
``````
1. 在字母数字和符号中选6个字符，随机生成6位的密码。
2. 要求用户输入用户名和密码，用户名不能与数据库中(database["Mercury", "Venus","Earth","Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"])重复，密码必须大于5位，并且包含数字和大写字母。

1.将字典中大于2的值过滤掉

``````d = {"a": 1, "b": 2, "c": 3}
d = dict((key,value) for key, value in d.items() if value<3)
``````

(2). 这种重构的语句在Python中非常常见。同样的我们也可以输出一个小于3的list：a = list(key for key, value in d.items() if value<3)

1. 读取输入的一句话中的单词数。
``````s = input('Please enter: ')
s_list = s.split(' ')
len(s_list)
``````

1. a,b 中的对应数字想加并输出结果
``````a = [1, 2, 3]
b = (4, 5, 6)
print(list(i+j for i,j in zip(a,b)))
``````

zip的使用，非常重要.

1. 正确的答案如下
``````firstname = input("Enter first name: ")
secondname = input("Enter second name: ")
print("Your first name is %s and your second name is %s" % (firstname, secondname))
``````

1. 打印出第三个“employee”的“LastName”,并添加一个“Albert Bert”的"employee"
``````d = {"employees":[{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}],
"owners":[{"firstName": "Jack", "lastName": "Petter"},
{"firstName": "Jessy", "lastName": "Petter"}]}

print(d["employees"][2]["firstName"])
d["employees"].append({"firstName": "Albert", "lastName": "Bert"})
``````

1. 打印a中的index 和 item 输出格式如下
Item 1 has index 0
Item 2 has index 1
Item 3 has index 2
``````a = [1, 2, 3]
for index, item in enumerate(a):
print("Item s% has index s%\n"%(item, index))
``````

1. 在字母数字和符号中选6个字符，随机生成6位的密码
``````import ramdon

characters = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#\$%^&*()?"
chosen = ramdon.sample("characters",6)
``````

1. 要求用户输入用户名和密码，用户名不能与数据库中(database["Mercury", "Venus","Earth","Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"])重复，密码必须大于5位，并且包含数字和大写字母。
``````database = ["Mercury", "Venus","Earth","Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
while True:
if user_name in database:
print("The user name is exits, please try again!")
else:
break

while True:
error_msg = []
if len(psw)<6:
error_msg.append( "Password must over 5 characters.")
if not any(i.isdigit() for i in psw):
error_msg.append("Password must contain at lease one number")
if not any(i.isupper() for i in psw):
error_msg.append("Password must contain at least one uppercase letter")
if len(error_msg) > 0:
for i in error_msg:
print(i)
else:
break
``````

hongtao2018
+ 关注

corcosa 10077人浏览