Python:创建函数判断闰年

今天在HackerRank刷Python题,记录下Python用function判断闰年

首先

在Python中函数定义语法如下:

def 函数名([参数列表]):

'''注释'''

函数体

其次

闰年的判断:

能被4整除但不能被100整除或者能被400整除。

然后,相关代码如下:

def is_leap(year):

    leap = False

    leap=(year%400==0 or (year%4==0 and year%100!=0))

    return leap

调用的代码

year = int(input())

print(is_leap(year)

其中,可以直接return (year%400==0 or (year%4==0 and year%100!=0)) 不需要定义leap变量。

你可能感兴趣的:(Python,python)