Python programming exercises 1-10

1. Write a program that finds all such numbers that are divisible by 7 but not multiples of 5 (between 2000 and 3200 (both inclusive)). The obtained numbers are printed out in comma-separated order.

l = []
#创建一个空列表 l,用于存储符合条件的数字。

for i in range(2000, 3201):   
#使用 for 循环遍历从2000到3200(包含2000和3200)的每个数字。range(2000, 3201)生成一个序列,其中包含2000到3200之间的所有整数。
   if (i % 7 == 0) and (i % 5 != 0):   
   #在循环内部,使用条件语句判断当前数字是否满足条件
      l.append(str(i))  
    #如果当前数字满足上述两个条件,则将其转换为字符串并添加到列表 l 中。

print(','.join(l)) 
#循环结束后,使用 ','.join(l) 将列表 l 中的所有元素以逗号分隔的形式连接成一个字符串。

2. Write a program that can calculate the factorial of a given number and print out the result.

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n-1)
n = int(input("请输入一个整数:"))
result = factorial(n)
print("阶乘结果为:",result)
'''
首先定义了一个名为 factorial 的函数,用于计算阶乘。
在函数内部,使用条件语句判断输入的数字是否为 0 或 1。如果是,则直接返回 1,因为 0 的阶乘和 1 的阶乘都为 1。
如果输入的数字不是 0 或 1,那么会递归调用 factorial 函数,并将当前数字减去 1 的结果作为参数传入。然后将当前数字与递归调用的结果相乘,得到最终的阶乘结果。
接下来,通过用户输入函数 input 获取一个整数,并将其赋值给变量 n。
调用 factorial 函数,以输入的整数 n 作为参数,计算阶乘的结果,并将结果赋值给变量 result。
最后,使用 print 函数将阶乘的结果打印输出。
'''

3. With a given integer n, write a program to generate a dictionary containing (i, ixi) as integers between 1 and n (both inclusive). The program should then print the dictionary.

def generate_dict(n):
    squared_dict = {}
    for i in range(1,n+1):
        squared_dict[i] = i * i
    return squared_dict
n = int(input("请输入一个整数:"))
result_dict = generate_dict(n)
print(result_dict)

'''
首先定义了一个名为 generate_dict 的函数,用于生成包含 (i, i*i) 的字典。
在函数内部,创建一个空字典 squared_dict,用于存储键值对。
使用 for 循环遍历从 1 到 n(包括 n)的所有整数。
在循环中,将当前整数 i 的平方作为值,将 i 作为键,并将它们添加到字典 squared_dict 中。
循环结束后,返回生成的字典 squared_dict。
接下来,通过用户输入函数 input 获取一个整数,并将其赋值给变量 n。
调用 generate_dict 函数,以输入的整数 n 作为参数,生成包含 (i, i*i) 的字典,并将结果赋值给变量 result_dict。
最后,使用 print 函数将生成的字典 result_dict 打印输出。
'''

4. There are four numbers: 1, 2, 3, 4, how many different three-digit numbers can be formed without repeating numbers? How much is each?

for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if (i !=k ) and (i !=j) and (j != k):
                print(i,j,k)
'''
外层的第一个 for 循环使用变量 i 遍历从 1 到 4(不包括 4)的所有整数。
内层的第二个 for 循环使用变量 j 遍历从 1 到 4(不包括 4)的所有整数。
内层的第三个 for 循环使用变量 k 遍历从 1 到 4(不包括 4)的所有整数。
在最内层的 if 条件语句中,判断 i、j、k 三个数字是否两两都不相等。如果满足条件,则执行下面的代码块。
在满足条件的情况下,使用 print 函数将当前的 i、j、k 三个数字打印输出。
'''

5. The bonus issued by the enterprise is based on the profit commission. When the profit is less than or equal to 100,000 yuan, the bonus can be increased by 10%. 7.5% commission; 5% commission for the portion above 200,000 yuan between 200,000 and 400,000 yuan; 3% commission for the portion above 400,000 yuan between 400,000 and 600,000 yuan; 600,000 yuan When it is between 1 million yuan, the part above 600,000 yuan can be commissioned at 1.5%.

profit = float(input("请输入当月的利润(单位:万元):"))
bonus = 0.0 #奖金总额
#计算奖金
if profit <= 10:
    bonus = profit * 0.1
elif profit <= 20:
    bonus = 10 * 0.1 + (profit - 10) * 0.075
elif profit <= 40:
    bonus = 10 * 0.1 + 10 * 0.075 + (profit - 20) * 0.05
elif profit <= 60:
    bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profit - 40) * 0.03
elif profit <=100:
    bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (profit - 60) * 0.015
else:
    bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (profit - 100) * 0.01
print("应发放的奖金总数为:",bonus,"万元")

6. An integer, it is a perfect square number after adding 100, and it is a perfect square number after adding 168, what is the number?

import math

for x in range(1, 10000):
    # 判断 x + 100 是否为完全平方数
    if math.isqrt(x + 100) ** 2 == x + 100:
        # 判断 x + 100 + 168 是否为完全平方数
        if math.isqrt(x + 100 + 168) ** 2 == x + 100 + 168:
            print("满足条件的整数是:", x)
            break

7. Enter a certain year, month and day, and judge whether this day is the day of the year?

year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))

# 定义每个月份的天数列表(不考虑闰年)
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# 判断是否为闰年并更新二月的天数
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    days_in_month[1] = 29

# 计算总天数
total_days = sum(days_in_month[:month - 1]) + day

print(f"{year}年{month}月{day}日是这一年的第{total_days}天。")

8. Input three integers x, y, z, please output these three numbers from small to large.

x = int(input("请输入第一个整数 x:"))
y = int(input("请输入第二个整数 y:"))
z = int(input("请输入第三个整数 z:"))
# 创建一个列表,包含输入的三个整数
numbers = [x, y, z]
# 使用内置的排序函数对列表进行排序
numbers.sort()
# 输出排序后的结果
print("由小到大排序的结果为:", numbers)

9. Fibonacci sequence.

def fibonacci(n):
    fib_list = [0, 1]  # 前两个斐波那契数列数值
    for i in range(2, n):
        fib_list.append(fib_list[i-1] + fib_list[i-2])  # 计算下一个斐波那契数并添加到列表中
    return fib_list

# 输入需要生成的斐波那契数列的长度
n = int(input("请输入斐波那契数列的长度:"))

# 生成并输出斐波那契数列
fib_sequence = fibonacci(n)
print("斐波那契数列:", fib_sequence)

10. Copy data from one list to another.

# 原始列表
list1 = [1, 2, 3, 4, 5]

# 使用切片将数据复制到新列表
list2 = list1[:]

# 打印结果
print("复制后的新列表:", list2)

Guess you like

Origin blog.csdn.net/m0_74972727/article/details/131413817