• python练习题(markdown中的60道题)


    1.Demo01 摄氏温度转化为华氏温度

    1. celsius = float(input('输入摄氏温度:'))
    2. fahrenheit = (9/5)*celsius + 32
    3. print('%0.1f 摄氏温度转为华氏温度为 %0.1f' % (celsius, fahrenheit))

    结果: 

    2.Demo02 计算圆柱体的体积

    1. h, r =map(float, input().split())
    2. # 计算圆柱的底面积和体积
    3. area = 3.14 * r * r
    4. volume = area * h
    5. # 输出结果
    6. print("%.2f"%area)
    7. print("%.2f"% volume)

    注意:map()函数是Python中的一个内置函数,它的功能是:将指定的函数,依次作用于可迭代对象的每个元素,并返回一个迭代器对象。这个可迭代对象,可以是一个也可以是多个。

    A. map()函数实例

    案例1:可迭代对象传递给map()函数,然后map()函数将这个可迭代对象传入自定义函数。

    1. #自定义一个函数a,返回x*2的值
    2. def a(x):
    3. return x*2
    4. #定义列表
    5. lis1=[1,3,5,7,9]
    6. #对列表中的每个数运用函数a,返回迭代器
    7. lis1_a=map(a,lis1)
    8. #输出迭代器中的值
    9. for num in lis1_a:
    10. print(num)
    11. #输出结果
    12. '''
    13. 2
    14. 6
    15. 10
    16. 14
    17. 18
    18. '''

    案例2:可迭代对象传递给map()函数,然后map()函数将这个可迭代对象进行数据类型转换。

    1. #定义一个列表a
    2. a = ['1','2','3']
    3. #将列表中的每个字符串,转换成浮点型,并返回迭代器
    4. b = map(float,a)
    5. #输出迭代器中的值
    6. for num in b:
    7. print(num)
    8. #输出结果
    9. '''
    10. 1.0
    11. 2.0
    12. 3.0
    13. '''

            第二题用的就是案例2里面的map()函数;

    案例3多个可迭代对象传递给map()函数,然后map()函数将这2个迭代对象传入自定义函数a()。注意:这个自定义函数a()的参数个数,要与传入的可迭代对象数量一致。

    1. # 定义一个函数
    2. # 该函数采用2参数
    3. def a(x,y):
    4. return x * y
    5. # 定义列表1
    6. lis1 = [1, 3, 5, 7, 9]
    7. # 定义列表2
    8. lis2 = [2, 4, 6, 8, 10]
    9. # 将两个列表中的值,传入函数a,并返回可迭代器
    10. lis_a = map(a, lis1, lis2)
    11. #输出迭代器中的值
    12. for num in lis_a:
    13. print(num)
    14. #输出结果
    15. '''
    16. 2
    17. 12
    18. 30
    19. 56
    20. 90
    21. '''

    结果: 

    3.Demo03 将英尺数转换为米数

    1. feet = eval(input("Enter a value for feet:"))
    2. meters=feet*0.305
    3. print(feet,"feet is",meters,"meters")

    结果:

    4. 计算小费

    1. tip, rate = eval(input("enter the tip and a rate:"))
    2. rate = tip * rate / 100
    3. total = rate + tip
    4. print("The rate is", rate, "and the total is", total)

    结果:

    5.对一个整数中的各位数字求和

    1. num = eval(input("enter a number between 0 and 1000:"))
    2. gewei = num % 10
    3. baiwei = num // 100
    4. shiwei =(num - baiwei*100) // 10
    5. print("the sum of the digits is", int(gewei+baiwei+shiwei))

     结果:

     6.计算年数和天数

    1. minute = int(input("enter a number of minute:"))
    2. year = minute // (24*365*60)
    3. day = minute % (24*60*365) // (24*60)
    4. print("the year is", year)
    5. print("the day is", day)

    结果:

     7.计算能量

    1. M = float(input("enter a number:"))
    2. initialTemperature = float(input("enter a number:"))
    3. finalTemperature = float(input("enter a number:"))
    4. Q = M * (finalTemperature - initialTemperature) * 4184
    5. print(f"the Q is", Q)

     结果:

    8.分割数字

    1. num =input("please enter a number:")
    2. for i in range(len(num) - 1, -1, -1):
    3. # print(num[i],end = "\n")
    4. print(num[i])

    注意:len()函数只能用于可以迭代的对象;例如:列表list,元组tuple,字符串str,等;不能用于整数、浮点数等其他类型!

    结果:

     9.计算三角形的面积

    1. import math
    2. x1, y1, x2, y2, x3, y3 = map(float,input("输入:\n").split())
    3. first = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
    4. second = math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
    5. third = math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
    6. s = (first + second + third) / 2
    7. area = float(math.sqrt(s*(s - first)*(s - second)*(s - third)))
    8. print("%.1f"%area)

    结果:

    10.显示当前时间

    1. import time
    2. print (time.strftime("%Y-%M-%d %A %X %Z",time.localtime(time.time())))

    结果:

     

    11.计算三角形的三个角

    1. import math
    2. x1, y1, x2, y2, x3, y3 = map(float,input("输入:").split())
    3. a = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
    4. b = math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
    5. c = math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
    6. A = math.degrees(math.acos((a*a - b*b - c*c)/(-2*b*c)))
    7. B = math.degrees(math.acos((b*b-a*a-c*c)/(-2*a*c)))
    8. C = math.degrees(math.acos((c*c-b*b-a*a)/(-2*a*b)))
    9. print("%.2f"%A)
    10. print("%.2f"%B)
    11. print("%.2f"%C)

    注意:math.degrees()函数是将弧度转换为角度;

    结果:

     12.最小数量的硬币

    1. money = float(input("请输入总金额:"))
    2. a = money // 1
    3. b = (money - a*1) // 0.25
    4. c = (money - a*1 - b*0.25) // 0.1
    5. d = (money - a*1 - b*0.25 - c*0.1) // 0.05
    6. e = (money - a*1 - b*0.25 - c*0.1 - d*0.05) // 0.01
    7. print("%d个硬币"%a)
    8. print("%d个硬币"%b)
    9. print("%d个硬币"%c)
    10. print("%d个硬币"%d)
    11. print("%d个硬币"%e)

     结果:

     13.正多边形的面积

    1. import math
    2. n,s = map(float,input("请输入长和宽:").split())
    3. area = (n * s * s) / (4 * math.tan(math.pi/n))
    4. print("%.2f"%area)

    结果: 

     14.计算身体质量指数

    1. height = float(input("请输入身高:"))
    2. weight = float(input("请输入体重: "))
    3. BMI = weight/(height*height)
    4. if BMI < 18.5:
    5. print("过轻!")
    6. elif 18.5 <= BMI <24:
    7. print("正常!")
    8. elif 24 <= BMI <27:
    9. print("过重!")
    10. elif 27 <= BMI < 30:
    11. print("中度肥胖!")
    12. elif 30 <= BMI < 35:
    13. print("中度肥胖!")
    14. elif BMI >=35:
    15. print("重度肥胖!")

     结果:

     15.判定闰年

    1. year=int(input("请输入年份:"))
    2. if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    3. print("该年为闰年")
    4. else:
    5. print("该年是平年")

    结果:

     16.中彩票

    1. import random
    2. num = random.randint(10,100)
    3. a = int(input("请输入一个两位数:"))
    4. a1 = num // 10 % 10
    5. a2 = num % 10
    6. #是代表num随机数中的第一位和第二位!
    7. first = a // 10 % 10
    8. second = a % 10
    9. print(num)
    10. if a == num:
    11. print("你太幸运了,奖励你10000!")
    12. elif a1 == second and a2 == first:
    13. print("你也不错!奖励你3000!")
    14. elif a1 == second or a2 == first or a1 == first or a2 == second:
    15. print("还不错!奖励你1000!")
    16. elif a1 != second and a2 != first:
    17. print("你与奖励擦肩而过!奖励你0元!")

    17.解一元二次方程

    1. import math
    2. a, b, c = map(float,input("请输入三个值:").split())
    3. r1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
    4. r2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)
    5. if b * b - 4 * a *c > 0:
    6. print("%.2f"%r1)
    7. print("%.2f"%r2)
    8. elif b * b - 4 * a * c == 0:
    9. print("%.2f"%r1)
    10. elif b * b - 4 * a * c < 0:
    11. print("无实数解!")

    结果:

     18.解2×2线程方程

    1. import math
    2. a, b, c, d, e, f = map(float,input("请输入三个值:").split())
    3. x = (e * d - b * f) / (a * d - b * c)
    4. y = (a * f - e * c) / (a * d - b * c)
    5. a * x + b * y == e
    6. c * x + d * y == f
    7. if a * d - b * c == 0:
    8. print("无解")
    9. else:
    10. print(x)
    11. print(y)

    19. 未来是周几

    1. a, b= map(int,input("请输入今天星期几和你想要计算的日子:").split())
    2. if a + b % 7 == 1:
    3. print("星期一")
    4. elif a + b % 7 == 2:
    5. print("星期二")
    6. elif a + b % 7 == 3:
    7. print("星期三")
    8. elif a + b % 7 == 4:
    9. print("星期四")
    10. elif a + b % 7 == 5:
    11. print("星期五")
    12. elif a + b % 7 == 6:
    13. print("星期六")
    14. elif a + b % 7 == 0:
    15. print("星期天")

    结果:

     20.本年中的第几天

    1. year, month, day = map(int,input("输入年月日:").split())
    2. if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    3. if month == 1:
    4. day1 = day
    5. print(day1)
    6. elif month == 2:
    7. day1 = 31 + day
    8. print(day1)
    9. elif month == 3:
    10. day1 = 31 + 28 + day
    11. print(day1)
    12. elif month == 4:
    13. day1 = 31 + 28 + 31 + day
    14. print(day1)
    15. elif month == 5:
    16. day1 = 31 + 28 + 31 + 30 + day
    17. print(day1)
    18. elif month == 6:
    19. day1 = 31 + 28 + 31 + 30 + 31 + day
    20. print(day1)
    21. elif month == 7:
    22. day1 = 31 + 28 + 31 + 30 + 31 + 30 + day
    23. print(day1)
    24. elif month == 8:
    25. day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day
    26. print(day1)
    27. elif month == 9:
    28. day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + day
    29. print(day1)
    30. elif month == 10:
    31. day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
    32. print(day1)
    33. elif month == 11:
    34. day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day
    35. print(day1)
    36. elif month == 12:
    37. day1 =31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
    38. print(day1)
    39. else:
    40. if month == 1:
    41. day1 = day
    42. print(day1)
    43. elif month == 2:
    44. day1 = 31 + day
    45. print(day1)
    46. elif month == 3:
    47. day1 = 31 + 29 + day
    48. print(day1)
    49. elif month == 4:
    50. day1 = 31 + 29 + 31 + day
    51. print(day1)
    52. elif month== 5:
    53. day1 = 31 + 29 + 31 + 30 + day
    54. print(day1)
    55. elif month == 6:
    56. day1 = 31 + 29 + 31 + 30 + 31 + day
    57. print(day1)
    58. elif month== 7:
    59. day1 = 31 + 29 + 31 + 30 + 31 + 30 + day
    60. print(day1)
    61. elif month == 8:
    62. day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day
    63. print(day1)
    64. elif month == 9:
    65. day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + day
    66. print(day1)
    67. elif month == 10:
    68. day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
    69. print(day1)
    70. elif month == 11:
    71. day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day
    72. print(day1)
    73. elif month == 12:
    74. day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
    75. print(day1)

     结果:

    21 剪刀石头布

     

    1. import random
    2. a = random.randint(0,2)
    3. b = int(input("请输入0-2之间的整数(0代表剪刀,1代表石头,2代表步):"))
    4. print(a)
    5. if b > 2 or b < 0:
    6. print("请正确输入!")
    7. elif (a == 0 and b == 1) or (a == 1 and b == 2) or (a == 2 and b == 0):
    8. print("姐妹真棒哦!")
    9. elif a == b:
    10. print("打平")
    11. else:
    12. print("啊偶输掉啦!没关系的,我们可以再试试!")

    结果:

  • 相关阅读:
    Class文件结构和字节码指令集
    Node.js精进(6)——文件
    并发基础(四):线程池
    C语言循环结构 for循环
    python网络编程:通过socket实现TCP客户端和服务端
    网络通信知识地图
    wxWidgets Here
    华为云云耀云服务器L实例评测|拉取创建canal镜像配置相关参数 & 搭建canal连接MySQL数据库 & spring项目应用canal初步
    一文概览NLP句法分析:从理论到PyTorch实战解读
    Vue2:组件及组件通信
  • 原文地址:https://blog.csdn.net/m0_74543941/article/details/134491632