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

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

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

结果: 

2.Demo02 计算圆柱体的体积

h, r =map(float, input().split())

# 计算圆柱的底面积和体积
area = 3.14 * r * r
volume = area * h

# 输出结果
print("%.2f"%area)
print("%.2f"% volume)

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

A. map()函数实例

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

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

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

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

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

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

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

结果: 

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

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

结果:

4. 计算小费

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

结果:

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

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

 结果:

 6.计算年数和天数

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

结果:

 7.计算能量

M = float(input("enter a number:"))
initialTemperature = float(input("enter a number:"))
finalTemperature = float(input("enter a number:"))
Q = M * (finalTemperature - initialTemperature) * 4184
print(f"the Q is", Q)

 结果:

8.分割数字

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

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

结果:

 9.计算三角形的面积

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

结果:

10.显示当前时间

import time

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

结果:

 

11.计算三角形的三个角

import math
x1, y1, x2, y2, x3, y3 = map(float,input("输入:").split())
a = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
b = math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
c = math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
A = math.degrees(math.acos((a*a - b*b - c*c)/(-2*b*c)))
B = math.degrees(math.acos((b*b-a*a-c*c)/(-2*a*c)))
C = math.degrees(math.acos((c*c-b*b-a*a)/(-2*a*b)))
print("%.2f"%A)
print("%.2f"%B)
print("%.2f"%C)

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

结果:

 12.最小数量的硬币

money = float(input("请输入总金额:"))
a = money // 1
b = (money - a*1) // 0.25
c = (money - a*1 - b*0.25) // 0.1
d = (money - a*1 - b*0.25 - c*0.1) // 0.05
e = (money - a*1 - b*0.25 - c*0.1 - d*0.05) // 0.01
print("%d个硬币"%a) 
print("%d个硬币"%b) 
print("%d个硬币"%c) 
print("%d个硬币"%d) 
print("%d个硬币"%e) 

 结果:

 13.正多边形的面积

import math


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

结果: 

 14.计算身体质量指数

height = float(input("请输入身高:"))
weight = float(input("请输入体重: "))
BMI = weight/(height*height)
if BMI < 18.5:
    print("过轻!")
elif 18.5 <= BMI <24:
    print("正常!")
elif 24 <= BMI <27:
    print("过重!")
elif 27 <= BMI < 30:
    print("中度肥胖!")
elif 30 <= BMI < 35:
    print("中度肥胖!")
elif BMI >=35:
    print("重度肥胖!")

 结果:

 15.判定闰年

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

结果:

 16.中彩票

import random


num = random.randint(10,100)
a = int(input("请输入一个两位数:"))
a1 = num // 10 % 10
a2 = num % 10
#是代表num随机数中的第一位和第二位!
first = a // 10 % 10
second = a % 10
print(num)
if a == num:
    print("你太幸运了,奖励你10000!")
elif a1 == second and a2 == first:
    print("你也不错!奖励你3000!")
elif a1 == second or a2 == first or a1 == first or a2 == second:
    print("还不错!奖励你1000!")
elif a1 != second and a2 != first:
    print("你与奖励擦肩而过!奖励你0元!") 

17.解一元二次方程

import math


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

结果:

 18.解2×2线程方程

import math


a, b, c, d, e, f = map(float,input("请输入三个值:").split())
x = (e * d - b * f) / (a * d - b * c)
y = (a * f - e * c) / (a * d - b * c)
a * x + b * y == e
c * x + d * y == f
if a * d - b * c == 0:
    print("无解")
else:
    print(x)
    print(y)

19. 未来是周几

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

结果:

 20.本年中的第几天

year, month, day =  map(int,input("输入年月日:").split())
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    if month == 1:
        day1 = day
        print(day1)
    elif month == 2:
        day1 = 31 + day
        print(day1)
    elif month == 3:
        day1 = 31 + 28 + day
        print(day1)
    elif month == 4:
        day1 = 31 + 28 + 31 + day
        print(day1)
    elif month == 5:
        day1 = 31 + 28 + 31 + 30 + day
        print(day1)
    elif month == 6:
        day1 = 31 + 28 + 31 + 30 + 31 + day
        print(day1)
    elif month == 7:
        day1 = 31 + 28 + 31 + 30 + 31 + 30 + day
        print(day1)
    elif month == 8:
        day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day
        print(day1)
    elif month == 9:
        day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + day
        print(day1)
    elif month == 10:
        day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
        print(day1)
    elif month == 11:
        day1 = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day
        print(day1)
    elif month == 12:
        day1 =31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
        print(day1)
else:
    if month == 1:
        day1 = day
        print(day1)
    elif month == 2:
        day1 = 31 + day
        print(day1)
    elif month == 3:
        day1 = 31 + 29 + day
        print(day1)
    elif month == 4:
        day1 = 31 + 29 + 31 + day
        print(day1)
    elif month== 5:
        day1 = 31 + 29 + 31 + 30 + day
        print(day1)
    elif month == 6:
        day1 = 31 + 29 + 31 + 30 + 31 + day
        print(day1)
    elif month== 7:
        day1 = 31 + 29 + 31 + 30 + 31 + 30 + day
        print(day1)
    elif month == 8:
        day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day
        print(day1)
    elif month == 9:
        day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + day
        print(day1)
    elif month == 10:
        day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
        print(day1)
    elif month == 11:
        day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day
        print(day1)
    elif month == 12:
        day1 = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day
        print(day1)

 结果:

21 剪刀石头布

import random


a = random.randint(0,2)
b = int(input("请输入0-2之间的整数(0代表剪刀,1代表石头,2代表步):"))
print(a)
if b > 2 or b < 0:
    print("请正确输入!")

elif (a == 0 and b == 1) or (a == 1 and b == 2) or (a == 2 and b == 0):
    print("姐妹真棒哦!")
elif a == b:
    print("打平")
else:
    print("啊偶输掉啦!没关系的,我们可以再试试!")

结果: 

22.三角形的周长

a, b, c = map(int,input("请输入三边的长度:").split())
if a + b > c and a + c > b and b + c > a:
    perimeter = a + b + c
    print(perimeter)
else:
    print("error!")

 结果: 

 23.一周的星期几

 

相关推荐

  1. 蓝桥杯python比赛历届真99经典练习题 (8-12)

    2023-12-09 06:42:07       35 阅读
  2. Python入门60个基础练习(一)

    2023-12-09 06:42:07       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 06:42:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 06:42:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 06:42:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 06:42:07       20 阅读

热门阅读

  1. uniapp app端路由跳转时设置跳转过渡时间

    2023-12-09 06:42:07       42 阅读
  2. R语言读文件“-“变成“.“

    2023-12-09 06:42:07       30 阅读
  3. 一篇文章熟练掌握 Axios

    2023-12-09 06:42:07       42 阅读
  4. iOS app切换后台时添加模糊遮罩层

    2023-12-09 06:42:07       40 阅读
  5. Python函数的参数

    2023-12-09 06:42:07       45 阅读
  6. 128.最长连续子序列

    2023-12-09 06:42:07       31 阅读
  7. SQLite基本使用

    2023-12-09 06:42:07       40 阅读
  8. redis中序列化问题,value包含全路径类名解析

    2023-12-09 06:42:07       33 阅读
  9. ALLEGRO PCB 如何设置增加的过孔

    2023-12-09 06:42:07       38 阅读
  10. GDS Configuration File Changes to Support Dynamic Routing

    2023-12-09 06:42:07       44 阅读
  11. golang开发一个聊天系统例子

    2023-12-09 06:42:07       43 阅读