Home > Enterprise >  How can i find all 'friday the 13th' due year 2000 to 2020 by python
How can i find all 'friday the 13th' due year 2000 to 2020 by python

Time:05-05

How can i find all 'friday the 13th' due year 2000 to 2020 by python and have it print as below using a nested for loop:

print(i,k,13,"is friday the 13th!")

ex)

2000 10 13 is friday the 13th!
2001 4 13 is friday the 13th!
...
2020 11 13 is friday the 13th!

CodePudding user response:

Based on your code in the comments, this should work for you:

from datetime import date, timedelta

def friday_13_in_year(y): 
    day = date(y, 1, 1) 
    end = date(y, 12, 31) 
    one_day = timedelta(days=1) 
    while day < end: 
        if day.weekday() == 4 and day.day == 13: 
            return str(day) " is friday the 13th!"
        day  = one_day 

print([friday_13_in_year(y) for y in range(2000, 2022 1)])

CodePudding user response:

l=[i for i in ((datetime.datetime.strptime('20000101','%Y%m%d') datetime.timedelta(days=i)) for i in range(7306)) if i.day == 13 and i.weekday() == 4]

l lists all Fridays 13th from 2000 to 2020. 20 years in days is 20*365.25 1 = 7306

  • Related