
-
python之禅
'''import this Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!'''python之禅————————————————
-
华氏温度转换为摄氏温度
f = float(input('请输入华氏温度:')) c = (f-32)/1.8 print('%.1f华氏度 = %.1f摄氏度' % (f,c))
-
输入半径计算圆的周长和面积
import math radius = float(input('请输入圆的半径')) perimeter = 2 * math.pi * radius area = math.pi * radius * radius print('周长:%2f' % perimeter) print('面积:%2f' % area)
-
输入年份判断是否是闰年
year = int(input('请输入年份')) is_leap = (year % 4 == 0 and year % 100 != 0 or year % 400 == 0) print(is_leap)
-
分段函数求值
3x - 5 (x > 1) f(x) = x + 2 (-1 <= x <= 1) 5x + 3 (x < -1)
x = float(input('请输入数字')) if x > 1: y = 3*x - 5elif x >= -1: y = x + 2else: y = 5*x + 3 print(y)
-
英制单位英寸和公制单位厘米互换
value = float(input('请输入长度:')) unit = input('请输入单位:') if unit == 'in' or unit == '英寸': print('%f英寸 = %f厘米' % (value,value*2.54)) elif unit == 'cm' or unit == '厘米': print('%f厘米 = %f英寸' % (value,value/2.54))else: print('请输入有效的单位')