{"id":27199,"date":"2022-04-18T00:47:39","date_gmt":"2022-04-17T19:17:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=27199"},"modified":"2022-04-18T00:47:39","modified_gmt":"2022-04-17T19:17:39","slug":"python-holidays-library","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-holidays-library\/","title":{"rendered":"Python Holidays Library"},"content":{"rendered":"
Let us see about the holidays library here.<\/p>\n
holidays library in Python:<\/strong><\/p>\n The holidays library will help to determine whether or not a particular day is a holiday in various countries. Only public holidays are visible to us.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n years:<\/strong><\/p>\n The years argument specifies an iterable list of integers that should be generated automatically by the holiday object. This is only used when the expand parameter is set to False. The years parameter’s default value is [].<\/p>\n expand:<\/strong><\/p>\n The expand is a boolean argument that specifies whether or not the holidays should be appended to the holidays object in new years. True is the default value.<\/p>\n observed:<\/strong><\/p>\n It is also a boolean argument. When set to True, it includes the observed day of a holiday that falls on a weekend, if applicable. True is the default value.<\/p>\n prov:<\/strong><\/p>\n It is a string that represents a province<\/p>\n state:\u00a0<\/strong><\/p>\n It is a string representing a state.<\/p>\n get(key, default = None):<\/strong><\/p>\n It returns the name of the holiday on the specified date in the key parameter. If more than one holiday falls on the same day, the names are separated by commas.<\/p>\n getlist(key): <\/strong><\/p>\n It returns a list of names for holidays.<\/p>\n pop(key, default = None): <\/strong><\/p>\n It removes the key from the holidays object.<\/p>\n Before we work with this module, we should first install it.<\/p>\n Installation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The get_list()<\/strong> methods returns the output as a list.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Let us see about the holidays library here. holidays library in Python: The holidays library will help to determine whether or not a particular day is a holiday in various countries. Only public holidays are visible to us. Syntax: class holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None) Parameters years: The years argument specifies an iterable list of …<\/p>\nclass holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)<\/pre>\n
Methods of holidays module<\/strong><\/h3>\n
pip\u00a0install\u00a0holidays<\/pre>\n
Python Holidays Library<\/h2>\n
Printing the holidays in the specified Year<\/strong><\/h3>\n
\n
# Import holidays module using the import keyword\r\nimport holidays\r\n\r\n# Instantiate the holidays.India() and pass some random year as an argument to it\r\n# to get the India holidays for the given year(here it returns as a dictionary).\r\nIndiaholidays = holidays.India(years=2021)\r\n# Loop in the above holidays dictionary using the items() function and the for loop\r\nfor holiday_date, occasion in Indiaholidays.items():\r\n # Print the holiday date and the occasion for the year given.\r\n print(f'{holiday_date}: {occasion}')<\/pre>\n
2021-01-14: Makar Sankranti \/ Pongal\r\n2021-01-26: Republic Day\r\n2021-08-15: Independence Day\r\n2021-10-02: Gandhi Jayanti\r\n2021-05-01: Labour Day\r\n2021-12-25: Christmas<\/pre>\n
Checking if the given day is a holiday or Not<\/strong><\/h3>\n
\n
\nif conditional statements.<\/li>\n# Import time from datetime module using the import keyword\r\nfrom datetime import time\r\n# Import holidays module using the import keyword\r\nimport holidays\r\n# Call the India() method of the holidays method to get the India holidays.\r\n# (here it returns as a dictionary).\r\nIndiaholidays = holidays.India()\r\n# Give the date as static input and store it in a variable.\r\ngvn_date = '2021-12-25'\r\n# Check if the given date is a holiday or NOT in the above holidays dictionary using in, \r\n# if conditional statements.\r\nif gvn_date in Indiaholidays:\r\n # If it is true, then print the Occassion\/holiday name of the given date using the get() method\r\n print(Indiaholidays.get(gvn_date))\r\nelse:\r\n # Else print the given date is NOT a Holiday\r\n print(\"The given date {\", gvn_date, \"} is NOT a Holiday\")<\/pre>\n
Christmas<\/pre>\n
To return the holiday date as a List<\/strong><\/h3>\n
\n
\nas a list.<\/li>\n# Import time from datetime module using the import keyword\r\nfrom datetime import time\r\n# Import holidays module using the import keyword\r\nimport holidays\r\n# Call the India() method of the holidays method to get the India holidays.\r\n# (here it returns as a dictionary).\r\nIndiaholidays = holidays.India()\r\n# Give the date as user input using the input() function and store it in a variable.\r\ngvn_date = input(\"Enter some random date:\")\r\n# Check if the given date is a holiday or NOT in the above holidays dictionary using in, \r\n# if conditional statements.\r\nif gvn_date in Indiaholidays:\r\n # If it is true, then print the Occassion\/holiday name of the given date using the get_list() method\r\n # as a list.\r\n print(Indiaholidays.get_list(gvn_date))\r\nelse:\r\n # Else print the given date is NOT a Holiday\r\n print(\"The given date {\", gvn_date, \"} is NOT a Holiday\")<\/pre>\n
Enter some random date:2021-01-26\r\n['Republic Day']<\/pre>\n
Enter some random date:2015-03-02\r\nThe given date { 2015-03-02 } is NOT a Holiday<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"