python programming exercises (2)

Table of contents

1. The user inputs two numbers a and b. If a is divisible by b or a plus b is greater than 1000, then output a; otherwise, output b.

2. Please enter a number and determine whether the number is even or odd. If you use an even number, please determine the multiples from 1 to 3. If it is an odd number, please determine the multiples from 1 to 5.

3. There are chickens and rabbits in the same cage. There are 35 heads when viewed from above and 94 legs when viewed from below. How many chickens are there and how many rabbits are there?

4. Print out the standard daffodil numbers and output these daffodil numbers

5. What are the prime numbers between 50 and 150?

6. A five-digit number, if you write a 7 after it, you get a six-digit number A. If you write a 7 in front of it, you get a six-digit number B. B is five times of A. Find the five-digit number number of digits.

7. There is a pile of coins, and you can only take one or two at a time. Find the minimum number of times to get all the coins [10, 8, 5, 3, 27, 99]

8. If the difference between two prime numbers is 2, such two prime numbers are called "twin numbers". Find all "twin numbers" within 100.

9. Given a list, find the maximum value (cannot use the system API), find the minimum value, average value, and sum

10. Deduplicate the duplicate data in the list and use at least two solutions

11. Merge two lists

12. Reversal of lists Sorting of lists

13. How to randomly store 0-10 into a list


1. The user inputs two numbers a and b. If a is divisible by b or a plus b is greater than 1000, then output a; otherwise, output b.

a = float(input("输入第一个数"))
b = float(input("输入第二个数"))
if b / a == 0 or a + b > 1000:
    print('%0.2f' % a)
else:
    print('%0.2f' % b)

 

2. Please enter a number and determine whether the number is even or odd. If you use an even number, please determine the multiples from 1 to 3. If it is an odd number, please determine the multiples from 1 to 5.

num = int(input('请输入一个数:'))
if num % 2 == 0:
    print(f'{num}为偶数,从1到{num}是3的倍数有:')
    for x in range(1, num + 1):
        if x % 3 == 0:
            print(x)
else:
    print(f'{num}为奇数,从1到{num}是5的倍数有:')
    for x in range(1, num + 1):
        if x % 5 == 0:
            print(x)

 

3. There are chickens and rabbits in the same cage. There are 35 heads when viewed from above and 94 legs when viewed from below. How many chickens are there and how many rabbits are there?

a = int(input('输入头的数量'))
b = int(input('输入脚的数量'))
print('鸡的数量是', int(a-((b-2 * a) / 2)), '只')
print('兔的数量是', int((b - 2 * a) / 2), '只')

4. Print out the standard daffodil numbers and output these daffodil numbers

def num1():
    for num in range(100, 1000):
        a = num // 100
        b = num // 10 % 10
        c = num % 10
        if num == a ** 3 + b ** 3 + c ** 3:
            print("水仙花数为:%s" % num)


num1()

5. What are the prime numbers between 50 and 150?

def a(num):
    for i in range(2, num // 2 + 1):
        if num % i == 0:
            return False
    return True


for i in range(50, 151):
    if a(i):
        print(f"{i}是质数")

6. A five-digit number, if you write a 7 after it, you get a six-digit number A. If you write a 7 in front of it, you get a six-digit number B. B is five times of A. Find the five-digit number number of digits.

def num():
    for i in range(10000,100000):
       num1=i * 10 + 7
       num2=i + 700000
       if num2==5*num1:
           print(i)
num()
    

7. There is a pile of coins, and you can only take one or two at a time. Find the minimum number of times to get all the coins [10, 8, 5, 3, 27, 99]

coin = [10, 8, 5, 3, 27, 99]
x = 0
for i in coin:
    if i % 2 == 0:
        x += i // 2
    else:
        x += i // 2 + 1

print(f'{x}')

8. If the difference between two prime numbers is 2, such two prime numbers are called "twin numbers". Find all "twin numbers" within 100.

print('0到100以内的孪生数对有:')
m = 2
for num in range(3,100,2):
    for i in range(2,num//2 + 1):
        if num % i == 0:
            break
    else:
        if num - m == 2:
            print(f'{m},{num}')
        m = num

9. Given a list, find the maximum value (cannot use the system API), find the minimum value, average value, and sum

n = int(input('请您输入您想输入的数字个数:'))
ls = []
for i in range(1,n+1):
    num = float(input(f'请输入第{i}个数:'))
    ls.append(num)
print(f'您输入的数字有:{ls}')
 
ma = ls[0]
mi = ls[0]
su = 0
for i in ls:
    su = su + i
    if i > ma:
        ma = i
    elif i < mi:
        mi = i
print(f'这些数的最大值为:{ma}')
print(f'这些数的最小值为:{mi}')
print(f'这些数的和为:{su}')
print(f'这些数的平均值为:{su/n}')

10. Deduplicate the duplicate data in the list and use at least two solutions

ls = [1, 2, 3, 4, 4, 5, 6, 2, 3]
print(f'原列表为:{ls}')

# 方案一:set
ls = list(set(ls))
print(f'方案一去重后的列表为:{ls}')

# 方案二:遍历循环
ls1 = []
for n in ls:
    if n not in ls1:
        ls1.append(n)
ls = ls1
print(f'方案二去重后的列表为:{ls}')

11. Merge two lists

ls1 = [1,2,3]
ls2 = [4,5,6]
print(f'ls1:{ls1}')
print(f'ls2:{ls2}')
 
ls1.extend(ls2)
print(f'将ls2合并到ls1中:{ls1}')
ls1 = [1,2,3]
ls2.extend(ls1)
print(f'将ls1合并到ls2中:{ls2}')

12. Reversal of lists Sorting of lists

ls = [1, 2, 4, 5, 3, 6]
print(f'ls:{ls}')

ls.reverse()
print(f'ls的反转:{ls}')

ls.sort()
print(f'ls的排序:{ls}')

13. How to randomly store 0-10 into a list

import random
ls = []
while True:
    i = random.randint(0,10)
    if i not in ls:
        ls.append(i)
    if len(ls) == 11:
        break
print(f'ls = {ls}')

Guess you like

Origin blog.csdn.net/weixin_62304542/article/details/129719590