Python judges leap Year, judging date is the day ordinal of the current

Source: Internet
Author: User

Http://www.cnblogs.com/vamei/archive/2012/07/19/2600135.html

Python small topic for quick tutorial Job answer

Write a program to determine if 2008 is a leap year.

Write a program that calculates October 1, 2008 is the day of the year? (January 1, 2008 is the first day of the year)

1 #Judging Leap years2 defis_leap_year (year):3     return(year% 4 = = 0 andYear% 100! = 0)orYear% 400 = =04 #Judging is the day of the year5 defgetdayinyear (year,month,day):6Month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]7     ifis_leap_year (year):8month_day[1]=299     returnSUM (month_day[:month-1]) + DayTen  One Print(Getdayinyear (2008,1,1)) A Print(Getdayinyear (2008,10,1)) - Print(Getdayinyear (2009,10,1))

There are also ready-made methods for Time strftime () refer to this method description click Link http://www.runoob.com/python/att-time-strftime.html

Import datetime def Getdayinyear (year, Month, day):     = Datetime.date (year, month, day)    return date.strftime ('%j' )print(getdayinyear (2008,1,1))print(getdayinyear (2008,10,1))  Print(getdayinyear (2009,10,1))

Python judges leap Year, judging date is the day ordinal of the current

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.