Python练习题 python入门经典100题

liftword6个月前 (12-25)技术文章91
  • 题目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 1000 道练习题(8) python经典例题100道文库

在Python中,for循环用于打印各种种图案是最常见的编程问题。大多数打印模式都使用以下概念来控制输出:外部循环打印行数内部循环打印列数根据所需的空白位置,控制打印空白的变量1.输出简单的金字塔模型...

python100道真题题库 python题库及答案

正在学习Python数据分析的小伙伴可以做一下习题来巩固一下知识哦 ! 这100道python数据分析经典练习题根据难易程度分为三个等级,初、中、高级,大家可以结合自身对python的学习程度,以及学...

Python 100道必刷练习题??每日一题 ??

今天给大家准备100道编程练习这些题如果能坚持每天至少完成一道,一定可以帮大家轻松 get Python 编程技能,大家加油哦!!更多PDF获取:...

一些适合初学者的 Python 基础练习题,可以帮助您练习编程思维

编写一个程序,计算并输出 1 到 100 之间所有偶数的和。编写一个程序,输出 1 到 100 之间所有能被 3 整除但不能被 5 整除的数。编写一个程序,输出所有的“水仙花数”。所谓“水仙花数”是...

Python基础练习题?100道电子版及源码文件

Python基础练习题?,旨在帮助学习者巩固和提升Python编程技能。以下是一些精选的练习题目,包括但不限于:基础语法练习?:涉及变量定义、数据类型、运算符、条件语句、循环等基础语法结构的应用。例如...