百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Python练习题

itomcoil 2025-03-03 18:58 17 浏览

  • 题目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))

运行结果:

相关推荐

编程学子看过来,竞赛刷题网站推荐

2022年编程竞赛已经公布,想要在今年取得竞赛成绩的学生,一定要把握寒假时间,学习知识的同时通过刷题,巩固所学知识,提升解题能力。小编为大家推荐几个刷题网站,想要竞赛的学生一定不要错过。USACO美国...

给大家推荐些好的c语言代码的网站

C语言,那就来推荐几个吧,部分含有C++:1、TheLinuxKernelArchives(kernel.org)Linux内核源码,仅限于C,但内核庞大,不太适合新手;2、redis(redi...

推荐几个编程入门学习网站_比较好的编程自学网站

有一些刚上大学的朋友和想对编程感兴趣的朋友经常会让我推荐学习网站,下面几个是我认为零基础学编程比较好的网站,希望大家都有收获!1.W3schoolhttp://www.w3school.com.c...

10个最值得收藏的编程学习网站_有什么学编程的网站

程序员是一个需要不断学习的职业。幸运的是,在这个互联网时代,知识就在那里,等着我们去获取。以下我列举一些免费的编程学习网站包含多个开发语言Java、php、html、javascript等多个。1、h...

6个超酷的练习算法,学习编程的网站

在不了解算法的前提下,您无法通过Google或Facebook的采访。那么为什么不现在学习。我是一位拥有15年以上经验的程序员。从高中开始的第一年,我在算法上学习和工作很多。在我毕业之前,我一直...

在线 python 编程的网站_python3在线编程,python3在线编译器,在线编辑器

以下是一些提供在线Python编程环境的网站:1.Repl.it:Repl.it提供了一个多语言在线编程平台,您可以使用它在任何地方编写、运行、共享代码。Repl.it支持多种编程语言,包括Pyth...

推荐 7 个能过招全球程序员的编程挑战网站,欢迎挑战!

作为程序员的你,是不是经常估不准自己的编程水平?下面推荐7个能过招全球程序员的编程挑战网站,助你磨练技巧,提升技能,最终问鼎代码江湖!1.HackerRank你可以参加各种编码竞赛,比如算法、数学...

盘点 20 个编程学习教程网站,建议收藏

欢迎关注@程序员柠檬橙私信回复「1024」获取海量编程学习资源!如果你想学习编程,现在互联网这么方便,不用着急报名培训班,有很多高质量的编程学习资源网站可供你学习,程序员日常浏览的技术教程网站有哪些...

Flask 数据可视化_flourish数据可视化

数据可视化是数据处理中的重要部分,前面我们了解了Flask的开发和部署,如何用Flask做数据可视化呢?今天我们来了解一下。Python语言极富表达力,并且拥有众多的数据分析库和框架,是数据...

【python 工具】selenium 浏览器操作

selenium的安装步骤:1.安装selenium,打开cmd控制台pipinstallselenium2.安装驱动程序(我这里安装的是chromedriver),用来启动chrome浏览器...

可视化爬虫工具,EasySpider软件体验

现在提起爬虫,大家可能会联想到Python语言,然后就是各种使用无头浏览器去网页上爬取数据,使用Python的过程相较于使用其他语言来说,简单了不少。但毕竟是编程语言,也需要去学习来适配各种网...

cursor+mcp+playwright,让AI给你推荐五一旅游胜地

阅读本文前提当你已了解mcp是什么,若不知,猛击:https://github.com/modelcontextprotocol/servers。最近有个小需求,根据用户输入内容,使用大模型来理解用户...

Cursor+Claude+Playwright:AI 让自动化测试效率暴涨,快到飞起!

一、引言随着AI时代的到来,软件测试变得越来越复杂,如何高效、准确地进行自动化测试成了每一个开发团队必须面对的问题。在日常工作中,测试工作常常面临各种挑战,比如功能复杂、需求频繁变更、时间紧迫等。传统...

推荐一个检测 JS 内存泄漏的神器_js内存泄漏的几种情况

大家好,我是Echa哥。作为一名Web应用程序开发者,排查和修复JavaScript代码的内存泄漏一直是最困扰我的问题之一。最近,Meta开源了一款检测JavaScript代码内存泄漏...

Python+Playwright自动化实战:高效爬虫全攻略

一、为什么选择Playwright?在信息爆炸的时代,数据获取能力直接决定内容生产效率。Playwright作为微软开源的新型自动化工具,凭借以下优势成为技术创作者的新宠:支持Chromium/Web...