w3resource

Python: Check whether a given month and year contains a Monday 13th

Python Basic - 1: Exercise-130 with Solution

Write a Python program to check whether a given month and year contains a Monday 13th.

Sample Solution-1:

Python Code:

from datetime import date
def test(month, year): 
    return str(date(year,month,13).strftime("%A")=='Monday')

month = 11;
year = 2022;            
print("Month No.: ", month, " Year: ", year);
print("Check whether the said month and year contains a Monday 13th.: " + test(month, year));
month = 6;
year = 2022;            
print("\nMonth No.: ", month, " Year: ", year);
print("Check whether the said month and year contains a Monday 13th.: " + test(month, year)); 

Sample Output:

Month No.:  11  Year:  2022
Check whether the said month and year contains a Monday 13th.: False

Month No.:  6  Year:  2022
Check whether the said month and year contains a Monday 13th.: True

Flowchart:

Flowchart: Python - Check whether a given month and year contains a Monday 13th.

Sample Solution-2:

Python Code:

import calendar
def test(month, year): 
    return str(calendar.weekday(year, month, 13) == 0)

month = 11;
year = 2022;            
print("Month No.: ", month, " Year: ", year);
print("Check whether the said month and year contains a Monday 13th.: " + test(month, year));
month = 6;
year = 2022;            
print("\nMonth No.: ", month, " Year: ", year);
print("Check whether the said month and year contains a Monday 13th.: " + test(month, year));

Sample Output:

Month No.:  11  Year:  2022
Check whether the said month and year contains a Monday 13th.: False

Month No.:  6  Year:  2022
Check whether the said month and year contains a Monday 13th.: True

Flowchart:

Flowchart: Python - Check whether a given month and year contains a Monday 13th.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get the index number of all lower case letters in a given string.
Next: Write a Python program to count number of zeros and ones in the binary representation of a given integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Returns the symmetric difference between two iterables, without filtering out duplicate values:

Example:

def tips_symmetric_difference(p, q):
  _p, _q = set(p), set(q)
  return [item for item in p if item not in _q] + [item for item in q if item not in _p]
print(tips_symmetric_difference([2, 4, 6], [2, 4, 8]))

Output:

[6, 8]

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook