python多种结构的例题,AI学习笔记——Python的几个练习题

本文通过一系列Python编程练习,涵盖了字典值过滤、字符串处理、序列操作、用户输入及错误修复等核心技能。例如,过滤掉字典中大于2的值,计算两个序列对应元素的和,以及从复杂嵌套结构中提取信息。同时,文章还介绍了如何确保用户输入的用户名不重复,并遵循特定密码规则。这些练习有助于巩固Python基础知识并提升编程能力。
摘要由CSDN通过智能技术生成

AAffA0nNPuCLAAAAAElFTkSuQmCC

上一篇文章中提到了学习编程练习的重要性,今天就通过几个练习题,来巩固一下Python中几个重要的技能。将字典中大于2的值过滤掉。#Filter out values of equal or greater than 2#Note that for Python 2 you will have to use iteritemsd = {"a": 1, "b": 2, "c": 3}读取输入的一句话中的单词数。

a,b 中的对应数字想加并输出结果。#Print out in each line the sum of homologous items from the two sequencesa = [1, 2, 3]

b = (4, 5, 6)发现下面代码中的错误。#Please fix the script so that it returns the user submited first name for the first %s#and the second name for the second %sfirstname = input("Enter first name: ")

secondname = input("Enter second name: ")print("Your first name is %s and your second name is %s" % firstname, secondname)打印出第三个“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"}]}打印a中的index 和 item 输出格式如下

Item 1 has index 0

Item 2 has index 1

Item 3 has index 2a = [1, 2, 3]在字母数字和符号中选6个字符,随机生成6位的密码。

要求用户输入用户名和密码,用户名不能与数据库中(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)

这个题目有几个重要的点:(1). 遍历字典格式的数据需要,d.items().

(2). 这种重构的语句在Python中非常常见。同样的我们也可以输出一个小于3的list:a = list(key for key, value in d.items() if value<3)读取输入的一句话中的单词数。s = input('Please enter: ')

s_list = s.split(' ')

len(s_list)

这个题目几个重点:(1). 处理String中的几个非常常见的方法,如.split(), .strip() 等等。(2). len() size() type() 等Python常见的的方法。a,b 中的对应数字想加并输出结果a = [1, 2, 3]

b = (4, 5, 6)print(list(i+j for i,j in zip(a,b)))

zip的使用,非常重要.正确的答案如下firstname = input("Enter first name: ")

secondname = input("Enter second name: ")print("Your first name is %s and your second name is %s" % (firstname, secondname))

在Python中有多个格式输出的时候需要用元组(加括号)的形式打印出第三个“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"})

这道题的关键是找出d的结构,这是一个字典嵌套list再嵌套字典的结构。打印a中的index 和 item 输出格式如下

Item 1 has index 0

Item 2 has index 1

Item 3 has index 2a = [1, 2, 3]for index, item in enumerate(a):  print("Item s% has index s%\n"%(item, index))在字母数字和符号中选6个字符,随机生成6位的密码import ramdon

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

password = "".join(chosen)

这个题目有两个知识点,(1). ramdon,这个Python自带的重要库,已经random.sample()的使用方法。(2). string.joint()这个方法。要求用户输入用户名和密码,用户名不能与数据库中(database["Mercury", "Venus","Earth","Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"])重复,密码必须大于5位,并且包含数字和大写字母。database = ["Mercury", "Venus","Earth","Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]while True:

user_name = input("Please enter your user name: ")    if user_name in database:

print("The user name is exits, please try again!")    else:        breakwhile True:

error_msg = []

psw = input("Please enter your password: ")    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

这道题有这么几个知识点: (1). while 循环语句的使用。(2). if .... in .... 的使用。(3). i.isdigit() 和 i.isupper() 方法的使用。(4). any()的使用。

作者:Hongtao洪滔

链接:https://www.jianshu.com/p/3bbf3664a65f

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值