Khichdi is a traditional Indian food made with rice and lentils
The Story Begins
One day, Emperor Akbar announced in his royal court that anyone who could stand in the freezing cold water of a pond throughout the night would receive a reward. A poor man accepted the challenge, hoping to earn the reward. He stood in the cold water all night.
The next morning, he was brought before Akbar and explained that he had managed to survive the freezingnight by looking at a distant lamp burning in the palace. Akbar assumed that the warmth of the lamp had helped the poor man and refused to give him the reward.
When Birbal learned about this injustice, he decided to teach Akbar a lesson through his cleverness.
The next day, Birbal arrived late at the royal court. Akbar asked, "Birbal! Why are you so late today?"
Birbal replied, Your Majesty! I was cooking khichdi, but it simply wouldn't cook.
Akbar was surprised and asked, "How is that possible? Where did you place the pot of khichdi?
Birbal answered, Your Majesty, I hung the pot very high above the ground and lit a small lamp underneath it. I thought the heat from the lamp would cook the khichdi.
Akbar burst into laughter and said, Birbal! How can the heat of a small lamp reach a pot hanging so high above it? Your khichdi will never cook that way!
Birbal politely replied, "Your Majesty, if a lamp cannot provide enough heat to cook a pot hanging so far above it, then how could a distant lamp provide warmth to that poor man standing in the freezing pond?"
Akbar immediately understood his mistake and ordered that the poor man should receive his reward.
The Python Connection: Modules
Now, just as Birbal used his intelligence to solve a problem instead of doing everything from scratch, Python also provides us with ready-made solutions so that we don't have to write every piece of code ourselves.
These ready-made Python files containing reusable functions, variables, and classes are called Modules. Whenever we need their functionality, we can bring them into our program using the import keyword.
For example, imagine Birbal needs to perform a mathematical calculation while preparing his khichdi. Instead of writing the entire mathematical logic himself, he can use Python's already available math module:
python
import math
print(math.sqrt(25))
Here, import math brings the math module into our program, while math.sqrt(25) uses its sqrt() function to calculate the square root of 25. The result will be 5.0.
Just as Birbal used an existing source of light to make his point, Python allows us to reuse functionality that has already been written.
Similarly, suppose Birbal doesn't need the entire collection of mathematical tools and only wants the square-root function. Instead of importing the complete module, he can write:
python
from math import sqrt
print(sqrt(36))
Here, from math import sqrt brings only the sqrt function from the math module into our program. Now Birbal can directly write sqrt(36) instead of math.sqrt(36), and Python will return 6.0.
Creating Birbal's Own Module
Now imagine Birbal wants to create his own special recipe for khichdi. Instead of keeping all his instructions in one place, he creates a separate Python file called birbal.py and writes his own functions inside it.
birbal.py
python
def cook_khichdi():
print("Birbal is cooking khichdi!")
def serve_khichdi():
print("Khichdi is ready to serve!")
This is called creating a Custom Module because Birbal has created his own reusable Python code.
Whenever another Python file needs these functions, it can simply write:
main.py
python
import birbal
birbal.cook_khichdi()
birbal.serve_khichdi()
Here, import birbal makes the custom module available, and birbal.cook_khichdi() calls the function defined inside that module.
Organizing Modules into Packages
Just as Birbal cleverly organized his ideas to solve Akbar's problem, Python also allows us to organize multiple related modules inside a folder called a Package.
For example, we could create a folder named spices containing separate files such as salt.py, turmeric.py, and chili.py. Each Python file is a module, while the folder containing these related modules is a package.
text
spices/
│
├── __init__.py
├── salt.py
├── turmeric.py
└── chili.py
The Final Lesson
And so, Birbal's clever khichdi teaches us an important lesson about Python: we don't always need to create everything from scratch. We can reuse existing code through Modules, create our own Custom Modules, and organize related modules into Packages.
Just as Birbal used intelligence instead of unnecessary effort, a good Python programmer uses modules to write cleaner, reusable, and organized code. 👑
Top comments (1)
so beautiful :)
I'm happy that you first told a story that I love and then connected it to the technical concept. ;)