AI学习笔记——Python的几个练习题
上一篇文章中提到了学习编程练习的重要性,今天就通过几个练习题,来巩固一下Python中几个重要的技能。
- 将字典中大于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}
读取输入的一句话中的单词数。
a,b 中的对应数字想加并输出结果。
#Print out in each line the sum of homologous items from the two sequences a = [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 %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)
- 打印出第三个“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 2
a = [1, 2, 3]
- 在字母数字和符号中选6个字符,随机生成6位的密码。
- 要求用户输入用户名和密码,用户名不能与数据库中(database["Mercury", "Venus","Earth","Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"])重复,密码必须大于5位,并且包含数字和大写字母。
以上题目节选自udemy上的课程
答案如下:
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 2
a = [1, 2, 3] for index, item in enumerate(a): print("Item s% has index s%\n"%(item, index))
枚举enumerate 的应用,非常重要更多例子见(这里)[http://book.pythontips.com/en/latest/enumerate.html]
- 在字母数字和符号中选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: break while 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()的使用。
相关文章
AI学习笔记之——强化学习(Reinforcement Learning, RL)
AI学习笔记之——如何理解机器学习(Machine Learning)
人工智能学习笔记之——人工智能基本概念和词汇
人工智能学习笔记二 —— 定义问题
文章首发steemit.com 为了方便墙内阅读,搬运至此,欢迎留言或者访问我的Steemit主页

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java虚拟机介绍
jdk;8版本bin Java。exe(java的控制台程序运行) Javaw。exe (窗口程序运行) javac.exe(编译java源码称为class文件) jar.exe(吧class文件打成一个Java压缩包) javadoc.exe(把源码生成一个帮助文档) db(数据库)管理数据的一个软件内嵌数据库(然并卵) include(引入平台相关代码,win32.c语言代码) jre(java运行器):bin(运行命令) java运行环境 jre是真正的虚拟机 lib:Java的第三方库(第三方代码) javafx-src.zip javafx源码 (凉凉了)网页中运行的Java程序类似于flash凉凉的原因1。需要在客户电脑安装虚拟机。2.flash被HTML5击败了 src....
- 下一篇
ArcGIS API for JavaScript4.x 之加载2D、3D地图
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gisdoer/article/details/81545607 ArcGIS API for JavaScript 之加载2D、3D地图 点击查看文章
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Hadoop3单机部署,实现最简伪集群
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS7设置SWAP分区,小内存服务器的救世主