莫愁前路无知己,天下谁人不识君。。。。

python 小知识2-----比较大小,日期比较

上一篇 / 下一篇  2017-10-27 09:28:32 / 个人分类:python 小编程

# 比较两个数大小
def compare_two(a, b):
    small = a if a < b else b
    return small
print compare_two(1, 2)
# 比较三个数大小
def biggest(a, b, c):
    if a > b:
        maxnum = a
    else:
        maxnum = b
    if c > maxnum:
        maxnum = c
    return maxnum
maxnum = biggest(10, 2, 23)
print(maxnum)
def compare_three(a, b, c):
    max1 = a if a > b else b
    max = c if c > max1 else max1
    return max
print compare_three(5, 8, 4)
#max = (a > b)?(a>c ? a : c) : (b > c ? b : c) java用法
时间比较:
import time
a = time.strftime('%Y-%m-%d')
print a
print type(a)
a1 = time.strptime(a, "%Y-%m-%d")
print a1
b = '2017-10-25'
b1 = time.strptime(b, "%Y-%m-%d")
print b1
c = '2017-10-26'
c1 = time.strptime(c, "%Y-%m-%d")
if b1 <= a1 <= c1:
    print 2
elif a1 > c1:
    print 2
else:
    print 1



TAG: Python 编程

 

评分:0

我来说两句

Open Toolbar