π Chapter 1: A New Mission from King Nyaomaru!
Remember my previous adventures on Necoz?
I survived the mysterious UFO, explored the UFO Kingdom, learned Python loops, and even experienced the biggest earthquake on Necoz!
I thought I had finally earned a peaceful vacation.
But suddenly...
BEEP! BEEP! BEEP! π¨
My communicator started ringing.
KING NYAOMARU: STAR! EMERGENCY! REPORT TO THE ROYAL PALACE IMMEDIATELY!
I rushed to the palace.
When I arrived, I saw hundreds of robots standing in a line.
Some robots were printing welcome messages. Others were calculating food bills, while some were preparing delicious cat food.
I looked at the king.
STAR: Your Majesty, why are all these robots doing the same tasks again and again?
KING NYAOMARU: Because every time we need to repeat a task, our engineers write the same code again! Our robot factory is becoming too slow!
I smiled.
STAR: Your Majesty, don't worry! I know a magical Python trick that can solve this problem.
KING NYAOMARU: A magical trick? Tell me immediately!
STAR: It's called PYTHON FUNCTIONS! π
βοΈ Chapter 2: The Magic of Python Functions
I opened my laptop and explained:
A function is a reusable block of code that performs a specific task.
Instead of writing the same code repeatedly, we can write it once and use it whenever we need it.
Let's create our first function!
Python Code:
def greet():
print("Welcome to Necoz!")
greet()
Output:
Welcome to Necoz!
King Nyaomaru jumped from his throne!
KING NYAOMARU: WAIT! You wrote the message only once, but you can use it whenever you want?
STAR: Exactly, Your Majesty!
Let's understand the code:
-
defβ The keyword used to define a function. -
greetβ The name of our function. -
()β Parentheses used for parameters. -
:β Starts the function body. - Indentation β Identifies the code inside the function.
-
greet()β Calls the function and executes its code.
Remember: Defining a function does not execute it. We must call the function to run it!
King Nyaomaru immediately ordered all robots to learn this trick.
Unfortunately, one robot misunderstood the instruction.
It started greeting itself!
ROBOT: Hello, Robo! Nice to meet you, Robo!
STAR: Okay... I think that robot needs a software update. π
π Chapter 3: Hungry Cats and Function Parameters
Just when I thought everything was under control...
LAZY CAT: STAR! I WANT FOOD!
Then Milo appeared.
MILO: I WANT FISH!
And Luna appeared.
LUNA: I WANT Chicken!
I looked at the king.
STAR: Your Majesty, we can use something called function parameters!
I opened my laptop and wrote:
Python Code:
def feed_cat(cat_name, food):
print(cat_name, "received", food)
feed_cat("Lazy Cat", "Fish")
feed_cat("Milo", "Chicken")
feed_cat("Luna", "Tuna")
Output:
Lazy Cat received Fish
Milo received Chicken
Luna received Tuna
King Nyaomaru looked surprised!
KING NYAOMARU: So one function can serve different cats with different food?
STAR: Exactly!
Let's understand two important terms:
- Parameters: Variables written inside the function definition.
- Arguments: Actual values passed to the function when calling it.
In our example:
-
cat_nameandfoodare parameters. -
"Lazy Cat"and"Fish"are arguments.
Now we can use the same function for hundreds of cats!
Lazy Cat raised its paw.
LAZY CAT: Can I receive five meals?
STAR: YOUR MAJESTY, WE HAVE A NEW PROBLEM! π
π£ Chapter 4: The Mystery of the Missing Fish β Python return
Suddenly, the royal kitchen reported a serious problem.
ALL THE FISH SUPPLIES HAD DISAPPEARED!
King Nyaomaru shouted:
KING NYAOMARU: STAR! FIND OUT HOW MANY FISH ARE LEFT!
I smiled.
STAR: Don't worry! Python has another magical keyword called return.
The return statement sends a value back from a function.
Let's create a function:
Python Code:
def remaining_fish(total_fish, eaten_fish):
return total_fish - eaten_fish
result = remaining_fish(100, 35)
print("Remaining fish:", result)
Output:
Remaining fish: 65
KING NYAOMARU: AMAZING! So return sends the result back?
STAR: Exactly, Your Majesty!
Remember the difference:
-
print()displays information on the screen. -
returnsends a value back so that we can store it or use it in another calculation.
King Nyaomaru was finally able to calculate the remaining fish.
Lazy Cat looked disappointed.
LAZY CAT: ONLY 65 FISH? THAT'S TERRIBLE NEWS!
π€ Chapter 5: The Robot Factory Gets Smarter!
The royal engineers were now excited.
They wanted to create a function that could calculate the power level of every robot.
I showed them another example.
Python Code:
def calculate_power(battery):
power = battery * 2
return power
robot1 = calculate_power(50)
robot2 = calculate_power(80)
print("Robot 1 Power:", robot1)
print("Robot 2 Power:", robot2)
Output:
Robot 1 Power: 100
Robot 2 Power: 160
STAR: See? We can reuse the same function with different arguments!
The engineers cheered.
ENGINEERS: WOW! WE DON'T NEED TO WRITE THE SAME CODE AGAIN AND AGAIN!
King Nyaomaru stood up.
KING NYAOMARU: FROM TODAY, EVERY ROBOT MUST USE PYTHON FUNCTIONS!
But suddenly...
BOOOOOOM! π₯
Bad Cat entered the factory.
It pressed a mysterious red button.
The entire factory stopped working!
BAD CAT: HAHAHA! Let's see who can save the factory now!
π¨ Chapter 6: The Final Challenge β Save the Factory!
The factory computer displayed an error.
The robots needed a function to calculate their final energy after charging.
But the engineers had forgotten to write it!
King Nyaomaru looked at me.
KING NYAOMARU: STAR! THIS IS YOUR FINAL TEST!
I quickly opened my code editor.
Python Code:
def charge_robot(current_energy, extra_energy):
total_energy = current_energy + extra_energy
return total_energy
robot_energy = charge_robot(40, 60)
if robot_energy >= 100:
print("Robot fully charged!")
else:
print("Robot needs more energy!")
Output:
Robot fully charged!
Suddenly...
THE FACTORY STARTED WORKING AGAIN!
All the robots began dancing.
Even Bad Cat joined them!
King Nyaomaru stood up and announced:
KING NYAOMARU: CONGRATULATIONS, STAR! YOU HAVE MASTERED THE MAGIC OF PYTHON FUNCTIONS!
I smiled.
STAR: Thank you, Your Majesty. But I think I deserve a vacation now!
Suddenly, Lazy Cat shouted:
LAZY CAT: WAIT! WHO WILL WRITE A FUNCTION TO FEED ME?
I immediately ran towards the UFO.
Some missions are simply too dangerous! π
Top comments (26)
Really good @star123 I really liked the story. I also really think you should make like a web publication and turn this into a story. But anyway, What are you planning on making with python first?
Thanks, I'm thinking about it.π Maybe I'll make it soon, so I think it might take a little time. By the way, I haven't thought about a Python project yet.π
π
ππ»
Please keep the stories! π± They make Python easy and fun to follow, especially for young developers, and that's exactly what makes your posts special. nyaomaru does the same with cats, and it works. Your article about languages yesterday was great too. You're doing amazing, keep going your way! π
Thanks for the support β€οΈ
Bad Cat is still a cat after all, so of course heβs part of the family too!
Letβs all dance together, eat some fish, and forget about the mysterious red button for a while. ππ
Go fish!! πΈπ
Even bad cats can be good too! It's true that everyone fights sometimes, but in the end, we all live together happily as one family. πΈπ
Hey Tara! Loved your fun way of explaining concepts π
Thank you didi!π
@technogamerz π€
What?
Nothing? π€
Means
Ok leave it lol...
Both Didiπ
Interesting article! I think this is good for the kids or teenager to read to learn Python.Which part of Python have you learned? Why not try writing Python in AI? Looking forward to your next great idea!
Love the visual way you broke down functions hereβsuper approachable for anyone just getting comfortable with Python! Clean, modular functions make such a huge difference once you start building real-world automation scripts or parsing files. Thanks for sharing this!
thank you π
The print versus return part is the one I would point a beginner at first. Lazy Cat only sees 65 fish left because return sends the number back to use, while print just shows it.
Awesome Thank you bro!π
And that you are also a teenager so you have the best chance to know what the teenager want to know and what they like!
Thanks π
can you explain things without stories next time
okay broπ
Some comments may only be visible to logged-in visitors. Sign in to view all comments.