Python Day #2: Working with built-in Functions
Learn how to use Python’s built-in functions to write cleaner, faster code. Master the core tools every Python beginner needs to level up quickly.
Since last week, I began looking at new ways to provide value to my 8,000+ readers and make sure that everyone has a fair chance of success in Python.
Last article was the first step in the Python Roadmap to Success, it covered getting started with the Basics of Programming.
In this lesson we are going to cover what is a function? Why do we use Functions? And more importantly what are some of the most common built-in functions you should start using now to speed up and begin to optimize your code.
Over the last 8+ years I’ve been teaching full-time around the world which has given me the opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time.
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.
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.
Each week, I dive deep into Python and beyond, breaking it down into bite-sized pieces. While everyone else gets just a taste, my premium readers get the whole feast! Don't miss out on the full experience – join us today!
In this article we are going to assume you know the basics of programming, which we covered last week. Today I’ll touch briefly on what functions are and put a focus on certain popular and common built-in functions as these will continue to be the building blocks of your foundations.
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.
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.
👉 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!
What is a Function?
We have two types of functions, there are built-in functions that come with Python and other modules. Then we have functions that we can actually create and define ourselves.
Boiling it all down, a function is a reusable block of code. Since we are talking about built-in Python functions for step two in the Python Roadmap to Success, then a built-in function is a reusable block of code that has already been programmed to do a specific task by someone else, we just have to use them.
You can use these blocks of code, anywhere and at anytime in your code to complete a specific task. Python has a slew of built-in functions but you shouldn’t feel like you need to memorize them all, in fact, please don’t do that!
Just remember, a function is a reusable piece of code you can you when and where you want to streamline and optimize you structure and code.
Built-in Python Functions
Now, I want to begin this on day two where we know know the absolute basics. You now understand Python data types and variables. If you know someone who want’s to get into Python then share this article with them so they can follow along on a structured learning path that builds.
Since you now know all the different data types and how to store data types with variables we can build on this knowledge to begin working with data in different ways.
Below I’ve put seven super common Python functions that you will inevitably see often ands tart using early on in your Python career. I’ll break down what they do and why we use them. Then I’ll throw a few code examples at you to see them in action.
print: The most basic and common function in Python. You will use this more than you use your phone… Print allows you to output code in your terminal. This allows you to check the output of your code and debug along the way. So technically this function returns a value of None, it just allows us to read what’s happening.
int: As you know from Python Day #1, we have two types of numbers. int is a function that converts a string (text) into a whole number, an integer. So this function is used to switch to a different type of data.
float: On the other hand we also know that we have two types of numbers, the other type is a float which is just a decimal number. This function allows you to convert a string or an integer into a decimal number.
str: This function does the exact opposite of the former two, int and float. This function allows you to convert either type of number into a string format.
len: Len allows us to check the length of an iterable. Maybe you want to know how many characters are in a word or how many elements are in a list. This function returns an integer value that tells you the exact length of something. Also, you can’t use this on a number as returning the length of a number doesn’t make sense.
input: This function is great as it begins to allow your programs to become more interactive. Input allows a user to enter data from the keyboard and then use that data in the python programs. It’s important to note that no matter what the input function always returns a string type of data.
round: Another useful function for when you’re just starting out. Most of the time when we have a decimal number we don’t want to have a number like 3.956875. We pretty much just want to see 3.95. Round allows you to round a decimal number down to the hundredth of your choosing.
Before anyone jumps in or comments “Josh what about this function or that function?”. We will cover so many more in this series, but until we hit those checkpoints they shouldn’t be introduced now.
This article is just to get you understanding how functions work and what they are. As we progress in the Python Roadmap to Success we will uncover more gems and see how to put them into action.
👉 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!
Using What You’ve Learned in Python
Okay, so at this point in the article you now know some Python functions and what their purpose is. Building on last lesson or I guess article in this case you also know the different types of data as well as everything about variables.
Let’s try to start building on what we are learning and begin using these together to create different programs and see how these actually work.
Now here is our first introduction example on putting the pieces together. You can see somethings you now know such as different data types, variables and functions.
In the above I have a variable phrase with a string value, then I made a new variable phrase_length which holds the len()
function, since this function returns an integer the phrase_length
value is an integer.
Convert works similarly but the value is a string as we convert the length back to a string. Then I can use print to output data into my terminal for me to view. We can use the “+
” operator to add the strings together because they are all now the same type of data, strings.
Next up to the plate I wanted to show how we can use the other functions I discussed earlier in the lesson/article. We are using input to allow the user (probably you) to enter data from the keyboard, whatever you type is held as the value to the variable enter_pi
.
Then I take the value of enter_pi
and convert it into a decimal number, we do this because input always returns the value of a string to use. Even if you enter a number in input, it is not a number, it’s a string until you tell Python otherwise.
I want you to examine the code above, heck actually I want you to open a code editor like VS code and actually code out what you see above. Run the code and spend 10 minutes reading through it to really get a grasp on it.
👉 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!
Meshing Functions Together
Now in the first two code examples I did it line by line. There is nothing really wrong with this especially for learning. But as you become more advanced and learn more we want to find ways to optimize our code and make it run faster.
This is where nesting comes into play. Nesting is really just putting functions inside other functions so we can speed up and optimize our code. So in the code below I have written the same simple program. But this time instead of having five lines of code I wrote the same thing in three lines, I could have even done two lines.
The important thing to note when you are nesting functions is the outer most function is going to be the value you want to return. So for instance int he code above, I have float(input())
.
This is because we start with a string (input), then the final product I want is a float (decimal) number so the outer most function is a float. So always just ask yourself, what do you want the final value to be?
Only other thing I should point out is, never nest the print()
function. It doesn’t do anything and you will either get an error or None. Print can not be nested in anything.
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.
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 the first 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 Two
We’ve just hit step two on our Python journey — and it’s a big one. At this point, you should have a solid grip on what functions are and how to use some of Python’s most common built-in ones. You’ve learned how to clean up your code, make it more efficient, and even make it a little interactive.
If there’s one takeaway from this week, it’s this: learning these basic built-in functions is a game changer. They lay the groundwork for everything we’ll do next. And if things aren’t totally clicking yet, that’s okay. Go back through the examples, mess around with them, and try writing a few things on your own. That’s how it sticks.
Each week builds on the last, which is why it’s worth sticking with this series like it’s your personal roadmap. It’s been shaped by over 1,500 students and more than eight years of full-time teaching, so you’re in good hands.
And if you’re liking these lessons and want to take it further, check out the premium version. It helps support what I do, and you’ll get access to all the lessons, code, and hands-on projects to level up even faster.
Let’s keep moving forward with Day #3 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you get value from this article, please help me out, leave it a ❤️, and share this article to others. This helps more people discover this newsletter! Thank you so much!