
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Python holidays Library
What is holidays Library in Python?
Python holidays library is used for determining whether a specific date is a holiday or not in the specific country. The holidays library provides the simples way to check if a specific date is a holiday, list holidays for a year, or even get the name of a holiday for a given date.
Usage of holidays Library
The holidays library can be applied to a wide range of uses −
- This library can be useful to create workdays Vs. holidays scheduling.
- It can be used for designing non-holiday calendars.
- It can be used for rescheduling deadlines around holidays.
- It can be used for creating reports with holiday.
- You can explore country specific holidays.
- It can be used for merging sets of country holiday items when handling over multiple zones.
- It is useful to create project-based holiday lists along with your customized holidays.
Installation of holidays Library
To start using the holidays library, you need to install it first using the PIP, below is the command syntax to install it −
pip install holidays
Importing holidays Library
After installing the library, you need to import it in your Python code. Below is the import statement to import the holidays library −
import holidays
Specifying Country
To work with holidays for a specific country, create an instant of holidays library. Use the following statement to create an instance of holidays library for a specific country −
uk_holidays = holidays.UnitedKingdom()
Checking if a Date is a Holiday
You can use the holidays library to check whether a given date is a holiday in the specific country or not.
Example
In this example, we are checking a few dates whether they are holidays for not in united Kingdome.
# Importing holidays library import holidays # Specifying country uk_holidays = holidays.UnitedKingdom() # Checking dates print('01-01-2018' in uk_holidays) print('02-01-2018' in uk_holidays)
Output
True False
Getting Holiday Name
To get the name of the specific holiday based on the given date, use the .get() method with the object name by passing the date as the parameter.
Example
In this example, we are getting the name of the holidays on the specified dates −
# Importing holidays library import holidays # Specifying country uk_holidays = holidays.UnitedKingdom() # Getting Holiday Name print(uk_holidays.get('01-01-2018')) print(uk_holidays.get('02-01-2018'))
Output
New Year's Day None
Listing All Holidays for a Year
You can also print all holidays in the specific country. To print all holidays for a particular year, iterate the loop with .items() method.
Example
In this example, we are printing the dates and names of the holidays in year 2018 in the United Kingdom.
# Importing holidays library import holidays # Specifying country uk_holidays = holidays.UnitedKingdom() # Listing All Holidays for a Year for ptr in holidays.UnitedKingdom(years=[2018]).items(): print(ptr)
Output
(datetime.date(2018, 1, 1), "New Year's Day") (datetime.date(2018, 1, 2), 'New Year Holiday [Scotland]') (datetime.date(2018, 3, 17), "St. Patrick's Day [Northern Ireland]") ...
Combining Countries
You can combine holiday lists from multiple countries.
Example
The following example demonstrates how you can combine the countries −
# Importing holidays library import holidays north_america = holidays.CA() + holidays.US() + holidays.MX() print(north_america.country) print(north_america.get('07-01-2018')) print(north_america.get('07-04-2018'))
Output
['CA', 'US', 'MX'] Canada Day Independence Day
Creating Custom Holidays
By using holidays library, you can create custom holiday lists.
Example
In this example, we are creating custom holidays list −
# Importing holidays library import holidays import datetime in_holidays = holidays.HolidayBase() # Add a single date in_holidays.append('26-01-2019') # Add a date with description in_holidays.append({'26-01-2019': 'Republic Day India'}) # Add multiple dates in_holidays.append(['02-10-2018', '15-08-2018']) # Add a date object in_holidays.append(datetime.date(2018, 12, 25)) print('26-01-2019' in in_holidays) print(in_holidays.get('26-01-2019'))
Output
True Holiday; Republic Day India
Common Methods of holidays Library
Some of the commonly used methods of holidays library are −
Method | Description |
---|---|
get(key, default=None) | This method returns the holiday name(s) for a given date. |
get_list(key) | This method is same as get, but it returns a list instead of a comma-separated string |
pop(key, default=None) | This method removes and returns the holiday for a given date. |