Python练习题
off999 2024-12-01 02:11 14 浏览 0 评论
- 题目1:计算1-2+3-4+...+99-100
# using while-loop
num = 1
sum_result = 0
while num <= 100:
if num % 2 == 1:
sum_result += num
else:
sum_result -= num
num += 1
print(f"The result of 1-2+3-4+...+99-100 is: {sum_result}")
# using for-loop
sum_result = 0
for num in range(1, 101):
if num % 2 == 1:
sum_result += num
else:
sum_result -= num
print(f"The result of 1-2+3-4+...+99-100 is: {sum_result}")
运行结果:
- 题目2:打印2000-2050年所有闰年
# using while-loop
year = 2000
while year < 2051:
is_leap_year = True
if not ((year % 4 == 0 and year % 100 != 0) or year % 400 == 0):
is_leap_year = False
if is_leap_year:
print(year, end=' ')
year += 1
# using for-loop
for year in range(2000, 2051):
is_leap_year = True
if not (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
is_leap_year = False
if is_leap_year:
print(year, end=' ')
运行结果:
- 题目3:打印九九乘法口诀表
# using nested while-loop
factor1 = 1
while factor1 < 10:
factor2 = factor1
while factor2 < 10:
product = factor1 * factor2
print(f"{factor1}x{factor2}={product}\t", end=' ')
factor2 += 1
print()
factor1 += 1
# using nested for-loop
for factor1 in range(1, 10):
for factor2 in range(factor1, 10):
product = factor1 * factor2
print(f"{factor1}x{factor2}={product}\t", end=' ')
print()
运行结果:
- 题目4:打印1-100以内所有素数
# using nested while-loop
num = 2
while num <= 100:
front_num = 2
is_prime = True
while front_num < num:
if num % front_num == 0:
is_prime = False
break
front_num += 1
if is_prime:
print(num, end=' ')
num += 1
# using nested for-loop
num = 2
for num in range(2, 101):
is_prime = True
for front_num in range(2, num):
if num % front_num == 0:
is_prime = False
break
if is_prime:
print(num, end=' ')
运行结果:
- 题目5:计算1!-2!+3!-4!+...+9!-10!
# using nested while-loop
num = 1
sum_result = 0
while num <= 10:
index = 1
factorial = 1
while index <= num:
factorial *= index
index += 1
if num % 2 == 1:
sum_result += factorial
else:
sum_result -= factorial
num += 1
print("The result of 1!-2!+3!-4!+...+9!-10! is: {}".format(sum_result))
# using nested for-loop
sum_result = 0
for num in range(1, 11):
factorial = 1
for index in range(1, num+1):
factorial *= index
if num % 2 == 1:
sum_result += factorial
else:
sum_result -= factorial
print(f"The result of 1!-2!+3!-4!+...+9!-10! is {sum_result}")
运行结果:
- 题目6:找出二维列表[[1, -4, 5, 13], [25, 2, 9, -11], [18, 2, 15, 24, -10]]中的最大值
# using nested while-loop
dyadic_list = [[1, -4, 5, 13], [25, 2, 9, -11], [18, 2, 15, 24, -10]]
max_value = dyadic_list[0][0]
i = 0
while i < len(dyadic_list):
j = 0
while j < len(dyadic_list[i]):
if dyadic_list[i][j] > max_value:
max_value = dyadic_list[i][j]
j += 1
i += 1
print("The maximum value in %s is: %d" % (dyadic_list, max_value))
# using nested for-loop
dyadic_list = [[1, -4, 5, 13], [25, 2, 9, -11], [18, 2, 15, 24, -10]]
max_value = dyadic_list[0][0]
for i in range(len(dyadic_list))
for j in range(len(dyadic_list[i])
if dyadic_list[i][j] > max_value
max_value = dyadic_list[i][j]
print("The maximum value in %s is: %d" % (dyadic_list, max_value))
运行结果:
相关推荐
- python入门到脱坑经典案例—清空列表
-
在Python中,清空列表是一个基础但重要的操作。clear()方法是最直接的方式,但还有其他方法也可以实现相同效果。以下是详细说明:1.使用clear()方法(Python3.3+推荐)...
- python中元组,列表,字典,集合删除项目方式的归纳
-
九三,君子终日乾乾,夕惕若,厉无咎。在使用python过程中会经常遇到这四种集合数据类型,今天就对这四种集合数据类型中删除项目的操作做个总结性的归纳。列表(List)是一种有序和可更改的集合。允许重复...
- Linux 下海量文件删除方法效率对比,最慢的竟然是 rm
-
Linux下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、findwithdelete、rsync、Python、Perl.首先建立50万个文件$testfor...
- 数据结构与算法——链式存储(链表)的插入及删除,
-
持续分享嵌入式技术,操作系统,算法,c语言/python等,欢迎小友关注支持上篇文章我们讲述了链表的基本概念及一些查找遍历的方法,本篇我们主要将一下链表的插入删除操作,以及采用堆栈方式如何创建链表。链...
- Python自动化:openpyxl写入数据,插入删除行列等基础操作
-
importopenpyxlwb=openpyxl.load_workbook("example1.xlsx")sh=wb['Sheet1']写入数据#...
- 在Linux下软件的安装与卸载(linux里的程序的安装与卸载命令)
-
通过apt安装/协助软件apt是AdvancedPackagingTool,是Linux下的一款安装包管理工具可以在终端中方便的安装/卸载/更新软件包命令使用格式:安装软件:sudoapt...
- Python 批量卸载关联包 pip-autoremove
-
pip工具在安装扩展包的时候会自动安装依赖的关联包,但是卸载时只删除单个包,无法卸载关联的包。pip-autoremove就是为了解决卸载关联包的问题。安装方法通过下面的命令安装:pipinsta...
- 用Python在Word文档中插入和删除文本框
-
在当今自动化办公需求日益增长的背景下,通过编程手段动态管理Word文档中的文本框元素已成为提升工作效率的关键技术路径。文本框作为文档排版中灵活的内容容器,既能承载多模态信息(如文字、图像),又可实现独...
- Python 从列表中删除值的多种实用方法详解
-
#Python从列表中删除值的多种实用方法详解在Python编程中,列表(List)是一种常用的数据结构,具有动态可变的特性。当我们需要从列表中删除元素时,根据不同的场景(如按值删除、按索引删除、...
- Python 中的前缀删除操作全指南(python删除前导0)
-
1.字符串前缀删除1.1使用内置方法Python提供了几种内置方法来处理字符串前缀的删除:#1.使用removeprefix()方法(Python3.9+)text="...
- 每天学点Python知识:如何删除空白
-
在Python中,删除空白可以分为几种不同的情况,常见的是针对字符串或列表中空白字符的处理。一、删除字符串中的空白1.删除字符串两端的空白(空格、\t、\n等)使用.strip()方法:s...
- Linux系统自带Python2&yum的卸载及重装
-
写在前面事情的起因是我昨天在测试Linux安装Python3的shell脚本时,需要卸载Python3重新安装一遍。但是通过如下命令卸载python3时,少写了个3,不小心将系统自带的python2也...
- 如何使用Python将多个excel文件数据快速汇总?
-
在数据分析和处理的过程中,Excel文件是我们经常会遇到的数据格式之一。本文将通过一个具体的示例,展示如何使用Python和Pandas库来读取、合并和处理多个Excel文件的数据,并最终生成一个包含...
- 【第三弹】用Python实现Excel的vlookup功能
-
今天继续用pandas实现Excel的vlookup功能,假设我们的2个表长成这样:我们希望把Sheet2的部门匹在Sheet1的最后一列。话不多说,先上代码:importpandasaspd...
- python中pandas读取excel单列及连续多列数据
-
案例:想获取test.xls中C列、H列以后(当H列后列数未知时)的所有数据。importpandasaspdfile_name=r'D:\test.xls'#表格绝对...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python人脸识别 (54)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)