Python Day #7: The Step-by-Step Blueprint for Writing Python Functions
Learn how to write clean, reusable Python functions with type hints for error-free, easy-to-read code—perfect for beginners and pros alike.
Have you ever felt like you’re writing the same lines of code over and over? Let’s fix that.
Functions let you group code that does one thing into a little package you can use again and again. This saves time, helps you avoid mistakes, and makes your code easier to read and work with.
Welcome back to the Python Roadmap to Success, where I am giving all my readers a fair chance at learning Python the right way, following a proven roadmap that I’ve structured and taught for over the last four years.
In our last article we covered the sixth phase in the Roadmap to Success, it covered How to Get Started with For Loops. This lesson will help you reinforce everything you’ve been learning and take it to the next level.
In this lesson, we are going to begin the journey into reusable code. This is a core Python fundamental that you will inevitably use in your careers, functions. So breaking this down to a level you can understand is important to me.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python. Join us today!
Functions aren’t just about cutting down on repetitive code. They help you keep your code clean, organized, and reusable. Every function you write should do one specific thing. When you add type hints, you make your code easier for you and anyone else reading it to understand.
Over the last 8+ years I’ve been teaching full-time around the world which has given me the unique opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time. Breaking down complex topics, step by step.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
In this article, we’re going to build on what you’ve already learned. Last time, you worked on controlling the flow of your code. Now, it’s time to define your own functions and start writing clean, reusable code that makes you a better programmer.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
👉Only this next month for Mothers & Fathers Day, I am offering my readers a 30% off on all annual subscriptions! Show your parents you care about your career, thanks mom and dad…
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Reusable Blocks of Code
Congratulations, at this point you have a good understanding of the basics of programming and how you can mange the flow of your code. It’s time to take things to the next level, this concept will build perfectly on everything so far.
So far, you’ve worked with different built-in functions in Python. Can you name me a few? These are pieces of code, that have already been programmed to complete a certain task. All you need to do it call them and give them an argument.
In case you couldn’t name any, which shame on you if you couldn’t, you worked with most of these: print()
, len()
, input()
, str()
, int()
, range()
, float()
. These are just to name a few, but these functions all serve a specific task.
The real questions are, how do they work? What is going on behind the scenes? Can I make my own function? Yes, you can make functions of your own. This is an important concept to learn now as everything will continue to build from here.
Essentially a function is just a reusable block of code that you define one time, then you can use it to complete a certain tasks anywhere in your program. If you’re writing code, and you know that the code you’re writing can be used more than once, you should store it in a function block.
The goal of writing a function is to make your code reusable, so you don’t have to write it all again. Every function you define should serve one specific purpose. Not two, not three, one. This will make your code clearer and you’ll be able to reuse that function in more scenarios.
Once you define a function of your own, you can use it just like you would any of those other Python functions we talked about earlier. You need to call it, and give it any needed arguments.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
How to Define a Function in Python
We need to set the stage for how you can create your own functions, but throw the word “create” away as the word we want to use is “define”. You need to define a function, that is what you are doing.
In Python we have the keyword def
, this just means define and we use this at the start when we are going to define/declare a new function in our code.
After def
, we name the function, that’s then followed by a set of parentheses. You can name you function anything, but the same naming conventions apply that you used for variables. No capitalization, no spaces, don’t start with numbers, etc.
Inside the parentheses you can put any parameters that the function will use. A parameter is just a variable that you will use in the function. The value to the parameters are arguments. We give arguments when you call the function.
Indentation is also very important here, as the next line should be indented to tell Python that this is the code that’s wrapped in the function. Let’s take a look at a basic set up.
Above you can see I defined my own function, check_even_numbers
. I then gave this two parameters, num1
and num2
. Currently these parameters have no value, but we can write the function as if they already do, think ahead.
All this function does is check if added together, these parameters are an even or odd number, then based on this it will return True
or False
. The key thing here is to remember a function returns a value.
When a function returns a value, we can store that value in a variable. So the value to the variable results
is either going to be True
or False
, which ever the function returns to us.
We can then use that variable results in other parts of our code as it now has a value. Yes, you can have your function print
off things, but you can not use a printed value, this is just for you.
A function should most of the time return a value that you can then use at another part in your code.
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Ready to Take Your First Steps with Python?
Most people get stuck before they even start. But you don’t have to!
I’ve put together a Free Course: The Pure Basics of Python that covers the very basics of Python, made just for beginners like you.
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years!
Here’s what you’ll learn:
✅ Step-by-step help to set up VS Code and install Python.
✅ Learn how to use variables, built-in functions, and even nesting functions.
✅ A quick test to see how much you’ve picked up.
✅ My handcrafted Python Guide to keep you on track.
This free course is a perfect first step — you’ll get a feel for my teaching style and build confidence to keep going.
👉 Ready to get started?
P.S. — If you like the free course (and I think you will!), I’ll show you how to dive deeper with my full Python Masterclass.
🎁 - Get a free one-on-one coaching session with any Masterclass!
Go through the entire Masterclass, complete all material, and attend the Q&A's, if you still feel like your struggling I'll personally work with you one-on-one until you're confident!
Increase Code Readability
Okay, now is a get time to introduce a new Python topic that will make you a better developer. You will thank me and yourself later as this increase the readability of your code so at a quick glance you can see what a function is doing.
This new topic is called Type Hintings. These are basically additional hints and text you give to your code so you know exactly what type of data a function or variable accepts as well as what type of data a function returns.
Here is some new code that I’ve optimized for this purpose. You can see I start with three variables, then I can set the value to the variable so Python and I know what type of data should be the value.
This should also be done when we define a function, you can now see that I have three parameters, I have also set the data type value for them. If the function gets any other type of data than the type I specified we’d get an error.
Another point is we can use the → after the parameters, this says the type of data that the function will return in the end. Now when you read the function you know exactly the type of data it accepts and the type of data it will output!
This is one of the best habits you can pick up right now! You should start writing not only your functions but all your code with type hintings. You can see how much clearer everything becomes.
This function calculates the discounted price that I am offering on The Nerd Nook Premium Readerships! That’s 30% off annual subscriptions. You get everything I have here, from long-form articles, to monthly projects and more. Just join with the button below.
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass platform designed from 8 years teaching experience and that has helped over 1,500+ students succeed with Python. (All my readers unlock a limited time, 35% discount, use code: pythonsummer35)
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
The Nerd Nook: This is where you are right now! I write here multiple times per week with the sole goal of breaking down this larger than life topic to help you excel and learn Python faster than you thought was possible.
This article will be a piece to a larger roadmap but will go behind a paywall after 6 weeks! To help support my work in teaching others consider joining as a premium reader to unlock all these articles, monthly Python projects and in-depth dives.
Wrapping Up Lesson Seven
Alright, so now you know how to take that next big step in your coding journey—making and using your own functions.
Functions aren’t just there to avoid repeating the same code over and over. They help keep your code clean, organized, and easy to read, which makes it a lot simpler to understand and fix later on.
When you add type hints to your functions, you make it even easier to know what kind of stuff your code is expecting and what it gives back. Every function you write should really just do one thing, so it’s clear and simple.
Keep practicing writing your own functions with type hints—it’s one of the best things you can do to get better at coding. Best of luck nerds, you got this!
Let’s keep moving forward with Day #8 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.