程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

Python练习题(python训练题库)

hfteth 2025-03-18 22:34:29 技术文章 9 ℃
  • 题目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}")

运行结果:

P1


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

运行结果:

P2


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

运行结果:

P3


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

运行结果:

P4


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

运行结果:

P6


  • 题目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 < lendyadic_listi: if dyadic_listij> 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))

运行结果:

P6

Tags:

最近发表
标签列表