Python编程百练
Python编程习题百练,助你百炼成钢
郭燕燕~
星光不负赶路人,时光不负有心人
展开
-
Python编程练习21
任务要求:0-9十个数字组成的两位数中,找出个位和十位数字相同的数,如11、22、33等知识点:嵌套for循环参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-01-08"""numList = []for i in range(1, 10): for j in range(1, 10): if i == j: num = i*10 + j numList原创 2021-04-12 15:19:46 · 299 阅读 · 1 评论 -
Python编程练习20
任务要求:0-9十个数字,能组成多少个两位数知识点:嵌套for循环参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-26"""numList = []count = 0for i in range(1, 10): for j in range(0, 10): num = i * 10 + j numList.append(num) count += 1print原创 2021-04-12 15:18:33 · 259 阅读 · 0 评论 -
Python编程练习19
任务要求:两个列表[1, 2, 15, 20, 25, 50, 100]和[2, 18, 20, 55, 100, 105],找出其中相同的元素、不同的元素、去重之后的元素知识点:集合的交集、差集、对称差集、并集参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-02-18"""list_1 = [1, 2, 15, 20, 25, 50, 100]list_2 = [2, 18, 20, 55, 100, 105]set_原创 2021-04-12 15:17:28 · 207 阅读 · 0 评论 -
Python编程练习18
任务要求:将字符串" Good morning, everyone! "中多余的空格去除,单词之间使用一个空格知识点:字符串去空格、字符替换参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-01-06"""str_1 = " Good morning, everyone! "new_str = str_1.strip().replace(" ", " ")print(new_str)...原创 2021-04-12 15:15:53 · 175 阅读 · 0 评论 -
Python编程练习17
任务要求:打印输此刻的日期、时间,输出如下格式知识点:datetime模块、time模块参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-01-29"""from datetime import datetimeimport time# 方法一today = datetime.today().strftime("%Y-%m-%d %H:%M:%S")print(today)# 方法二now = date.原创 2021-04-12 15:14:24 · 273 阅读 · 0 评论 -
Python编程练习16
任务要求:计算1到1000以内能被3或者7整除,但不能同时被3和7整除的数的个数,运行程序,这样的数有381个知识点:逻辑运算符or、and参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-01-15"""num = 1count = 0while num < 1000: if (num % 3 == 0 or num % 7 == 0) and num % 21 != 0: count +=原创 2021-04-12 15:12:38 · 277 阅读 · 0 评论 -
Python编程练习15
任务要求:列表[5, 6, 8, 15, 25, 36, 100, 200, 99],删除其中的偶数元素知识点:列表的逆序、元素删除注意:for循环正序删除列表中的偶数元素,会有遗漏。remove()删除一个元素之后,后边的元素会自动覆盖到上一个被删除的元素的位置上,此时for循环正向运行,正好会略过这个往前移的元素,继续往下走,造成遍历的遗漏。参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-26"""list_1原创 2021-04-12 15:10:54 · 111 阅读 · 0 评论 -
Python编程练习14
任务要求:将列表[1, 3, 5, 7, 9]和[2, 3, 5, 8, 9]合并为[1, 2, 3, 5, 7, 8, 9]知识点:列表的合并、排序、元素去重参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-26"""list_1 = [1, 3, 5, 7, 9]list_2 = [2, 3, 5, 8, 9]list_1.extend(list_2)list_1.sort()print(list(set(li原创 2021-04-12 15:09:13 · 109 阅读 · 0 评论 -
Python编程练习13
任务要求:将字符串”CoDechangEtest”分别转换成下列四种格式大驼峰命名法(全部单词首字母大写) 小驼峰命名法(第一个单词首字母小写,其余单词首字母大写) 单词全部小写,由下划线连接 单词全部小写,由减号连接打印输出如下:知识点:字符串切片、大小写转换参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-26"""str_1 = "CoDechangEtest"word_1 = str_1[0.原创 2021-04-12 15:07:25 · 105 阅读 · 0 评论 -
Python编程练习12
任务要求:1.对字典dict_1进行拷贝,生成新字典dict_22.只在新字典dict_2中添加新元素city,北京,字典dict_1中不添加city3.在字典dict_2、dict_1中均添加新课程cname_3,mysql课程dict_1 = { "name": "笨笨", "age": 18, "course": {"cname_1": "math", "cname_2": "python"}}知识点:浅拷贝参考答案:dict_...原创 2021-03-22 22:25:11 · 105 阅读 · 0 评论 -
Python编程练习11
任务要求:从以下文件路径中,找到文件名,并输出file_path = "D:\projects\demo\str_1.py"知识点:字符串查找、转义字符、字符串截取参考答案:# rfind()函数从字符串右侧开始查找# 查找\位置,\\转义pos = file_path.rfind("\\")# pos+1为str_1.py第一个字符s的位置file_name = file_path[pos+1:]print(file_name)...原创 2021-03-22 22:07:15 · 66 阅读 · 0 评论 -
Pyhton编程练习1
# coding=utf-8""" Author: 笨笨 Date: 2020-12-21"""print(" " * 7 + "《七律·长征》")print(" " * 7 + "作者:毛泽东")print( "红军不怕远征难,万水千山只等闲。\n" \ "五岭逶迤腾细浪,乌蒙磅礴走泥丸。\n" \ "金沙水拍云崖暖,大渡桥横铁索寒。\n" \ "更喜岷山千里雪,三军过后尽开颜。\n")...原创 2020-12-21 18:29:54 · 200 阅读 · 1 评论 -
Python编程练习2
题目要求:使用Python打印九九乘法表,实现以下效果图知识点:for循环、range()函数、字符串的格式化输出点击查看参考答案编程练习1原创 2020-12-22 15:30:33 · 104 阅读 · 0 评论 -
Python编程练习3
题目要求:求1000以内(包含1000)所有偶数的和、所有奇数的和知识点:if...else条件判断、数学运算点击查看参考答案编程练习2原创 2020-12-23 09:40:19 · 95 阅读 · 0 评论 -
Python编程练习4
任务要求:打印一颗圣诞树,实现以下效果图知识点:字符串连接、for循环、数值运算参考答案:stars = 1print(" " * 1 + "圣诞快乐,夜夜平安")for i in range(7): print((" " * (7-i)) + ("*" * stars)) stars += 2print((" " * 7) + '|')...原创 2020-12-24 18:04:01 · 105 阅读 · 2 评论 -
Python编程练习6
任务要求:定义函数,接收一个列表作为参数,找出传入的列表中长度大于5的元素,添加到新列表中并返回知识点:函数、列表、for循环、if判断、len()函数参考答案:def len_five(L): new_list = [] for i in L: if(len(i)>5): new_list.append(i) return new_listL = ["C/C++", "Python", "Vue", "Django",原创 2021-01-01 12:17:51 · 161 阅读 · 0 评论 -
Python编程练习5
任务要求:编写一段代码,统计字符串中单词出现的次数,将字符串及次数存储在字典中,打印实现以下效果知识点:字符串的分割、字典参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-23"""# 定义字典# key:字符串,value:次数dict_1 = {}str_1 = "Welcome to BenBen's home. Welcome to Python. But which Python version sh.原创 2020-12-25 09:49:21 · 272 阅读 · 2 评论 -
Python编程练习7
任务要求:编写一个函数,判断从键盘输入的字符串是否只由数字组成,如果是返回该字符串,否则返回False,实现如下效果图知识点:函数、input()、isdigit()方法参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-25"""def is_num(): str_1 = input("请输入字符串:") if str_1.isdigit(): print("您输入的字符串只由数字组成原创 2021-01-01 12:34:38 · 200 阅读 · 1 评论 -
Python编程练习8
任务要求:编写一个函数,判断从键盘输入的字符串是否为python,是python,等待3秒程序退出;不是python,重新输入再次判断,当判断次数超过3次,等待3秒,程序退出,效果图如下知识点:import语句、while循环、time.sleep()参考答案:# coding=utf-8""" Author: 笨笨 Date: 2020-12-26"""import timedef is_python(): count = 1 while(原创 2021-01-01 13:58:50 · 141 阅读 · 0 评论 -
Python编程练习9
任务要求:将字符串拆分,逐句写入文本文档,实现以下效果图知识点:文件写入、字符串分割、字符串首尾去空格参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-01-01"""f = open("demo.txt", "a")str_1 = "2020有毒, 已然过去, 山河无恙, 人间皆安, 千帆过尽, 向新而行"list_1 = str_1.split(",")for s in list_1: f.write(s.原创 2021-01-01 15:49:51 · 99 阅读 · 0 评论 -
Python编程练习10
任务要求:读取日志文件log.txt,过滤ERROR log,将出现错误的用户名、IP地址、使用的客户端版本号打印出来,输出内容如下知识点:文件读取、字符串查找&截取、json转换参考答案:# coding=utf-8""" Author: 笨笨 Date: 2021-12-30"""import jsonlog_file = open("log.txt", encoding='UTF-8')lines = log_file.readlines()m原创 2021-01-01 16:44:54 · 150 阅读 · 0 评论