python学习笔记——数值

获取圆周率不同的精度

import math
for precision in range(10):
    print round(math.pi,precision)

int,round,math.floor的不同之处

  • 函数int()直接截去小数部分,返回整型。
  • 函数floor()得到最接近原数但小于原数的浮点数
  • 函数round()接四舍五入的方式取精确度,返回浮点数。
import math for n in (.2, .7, 1.2, 1.7, -.2, -.7, -1.2 ,-1.7): print "int(%.1f)\t%+.1f" % (n, int(n)) print "floor(%.lf)\t%+.lf" % (n,math.floor(n)) print "round(%.lf)\t%+.lf" % (n,round(n)) print '-' * 20

输入一个测试成绩,根据下面的标准,输出他的评分成绩(A—E)

  • A:90~100
  • B:80~89
  • C:70~79
  • D:60~69
  • E:<60

# coding='utf-8'
n = raw_input("请输入一个0~100的分数")
n = int(n)
if 90 <= n <= 100:
    print "A"
elif 80 <= n:
    print "B"
elif 70 <= n:
    print "C"
elif 60 <= n:
    print "D"
else:
    print "E"

输入一个年份,判定其是否闰年


year = raw_input('请输入要检测的年份')
year = int(year)
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print year ,'是闰年'
else:
    print year ,'不是闰年'

取一个任意小于1美元的金额,然后计算可以转换成最小多少枚硬币.硬币有1美分,5美分,10美分,25美分4种.1美元等于100美分.举例说,0.76美元换算结果应该为3枚25美分,1枚1美分.类似76枚1美分,2枚25美分+2枚10美分+1枚5美分+1枚1美分的结果是不合要求的!


# coding='utf-8'

n = raw_input("请输入一个小于1美元的金额")
n = int(float(n) * 100)
ret = ''
for i in (25, 10, 5, 1) :
    if i <= n :
        temp = divmod(n, i)
        if temp[0]:
            ret += str(temp[0]) + "枚"+str(i) +"美分"
        if temp[1]:
            print temp[1]
            n = temp[1]
        
print ret

JS是数组join字符串,python是字符串join数组

原文地址:https://www.cnblogs.com/rubylouvre/p/2081197.html