Python练习题 python入门经典100题

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

运行结果:

相关文章

130道python练习题 完整版PDF python训练题

今天跟大家分享一些干货,在学python的朋友可以动起来了!python基础知识练习题,常见常用的,可以作为参考,挺不错的,也有许许多多的讲解,适合python巩固基础知识和入门130道练习题,涵盖基...

这是我见过最好的python100道练习题(附带答案链接)

今天看到一位博主发了100道练习题,仔细看了下,觉的很是不错!难度细分也规划的很好,于是就搬运过来了,以下只列出了其中的10道练习题,一起来看看吧!详细答案链接:https://github.com/...

Python 选择题,程序填空题,安徽对口高考分析复习,字典,循环

训练题:一、选择题1、下列选项中,不属于python语言特点的是( C )A、简单易学 B、开源 C、面向过程 D、可移植性2、运行下方代码段,输出的结果是( D )a=()print(type(a)...

吐血整理python最全习题100道(含答案)题目,建议收藏

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

150道Python练习题,三天练完 python入门100题

很多正在学习数据分析或人工智能的小伙伴,很苦恼Python到底要这么学?这150道Python题做完以后相信你会有很大的改变的!完整版已经打包好了↓↓↓...