Python练习题 python入门经典100题

liftword4个月前 (12-25)技术文章50
  • 题目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最全习题100道(含答案)题目,建议收藏

Python100道经典练习题当前计算机语言最火的python占据我们生活的各个方面,人工智能、云计算、5G发展、汽车工业、互联网加行业等。话不多说,所谓磨刀不误砍柴工,掌握一门编程语言的最佳方法就是...

Python期末助力,考试知识点、选择填空编程题汇总

1.引言为了辅助同学们Python期末复习,我们总结了Python知识的PDF文档,①首先对Python语言的基础知识进行了梳理,包括数据类型、条件语句、循环语句、函数、文件操作等方面,并给出了相应的...

加班熬夜整理出来的150道Python基础题,学到就是赚到!超级详细

Python练习题,每日一练,快速学会编程,玩转Python[机智]...

字节跳动大佬总结的Python必练100道真题 无偿分享

编程这个东西,学习理论知识固然是重要的,但是更加能使人得到提升的还是需要多练,多敲。这篇文章整理好了Python的100道真题,比较值得做,有意义的Python真题,无偿分享给大家,希望大家能赶快脱离...

Python100道练习真题 python一百题

相信能刷到我这篇笔记的朋友们,或多或少都对 Python 有一定的了解,今天呢,为大家带来了 Python 编程100道经典练习题,这100道 Python 编程经典练习题根据难易程度分为三等级,初级...