Why We All Should Teach If We Want To Become Better Learners

Python Methods and Functions

12 min read

Read Time

May 14, 2024

Published On

I was reading the news this morning, and apparently the final exams in high school in the Netherlands start today. I had my finals almost two decades ago. To put that in perspective, these kids who are probably having their most stressful moments in their lives right now weren’t even in the making. Jeez, what a depressing intro for a blog post. Ok, something else then.

Have you ever had a debate with your college professor and won?

Me neither.

I did, however, once upon a time, won a debate against my high school teacher. Now I know, that’s not nearly as impressive as winning a debate against a professor, but still, it felt good. And the reason it felt good is because for once, for just that one time, I knew it better than the teacher.

Why am I telling you this? Well, because it transitions nicely to the subject of today’s blog post, which is why every learner should teach. One of the reasons I created this blog is to learn by teaching, but teaching is a big word, so let’s just call it sharing. I believe that the ability to explain or share what you know about a topic is a pretty good indication of how well you understand it. If I write a blog post about something I’ve learned in my own words, then this does two things. One, it reinforces what I’ve learned, and two, it may give me another perspective about the topic, and the more you know, the more you know.

It would be cumbersome to explain literally everything I’ve learned, and my guess for you as the reader too. Instead, I just want to share some highlights, or just some interesting notes, according to me.

The topics in the DevOps track range from a variety of topics, which I will go into more detail in another blog post, but in this blog post, I want to share some of the more interesting topics in Python. Also and totally my opinion, Python itself is already interesting, but from what I understand, not the most crucial tool in the DevOps toolbox.

Nevertheless, since I’ve only learned the fundamentals of Red Hat Enterprise Linux, Networking, and Python, and because I’m still going through the Python course as of writing this blog post, it is still fresh in my memory. So with that said, let me share some notes about “Methods & Functions”.

First, let’s start by looking at the differences between the two, or perhaps just define what a function is versus what a method is.

A function is an independent block of code that can be called from anywhere. A block of code is simply one or more lines of statements that run sequentially from top to bottom within that block. The keyword here is “independent”, because that’s one of the things that differentiates a function from a method. So if a function is independent, then a method is dependent.

Now the question is, what is a method dependent on? I’m glad I asked.

Methods are tied to an object or class. Meaning that it cannot run by the method itself, it needs an object or class instance to be invoked, whereas a function is called by name.

def my_func():

Methods are accessed using dot notation, like .capitalize() which capitalizes the first character in a string.

def my_func(text):
    text.capitalize()

Now that we know the difference between the two, let’s focus from here on on functions.

When you first start to learn Python, you’ll probably learn about data types, comparison operators, and statements, and once you really understand these concepts, then you’ll fairly be able to solve basic problems, but once you get to functions, that’s when you really experience exponential growth in your ability to solve problems.

There are many exercises in the Python course I’m following to test your understanding of functions, but there’s one set of exercises that I want to share with you in this blog post.

The exercises are divided into 4 levels from warmup to challenging, and I must say, the warmup exercises were pretty doable. In terms of intensity probably similar to a warmup you do before you go for a run, like some light stretching, as opposed to the “warmup” you do before a WOD or Muay Thai.

I won’t go through all of the exercises, but I’ll just pick one or two from each level. Below are the exercises, and see if you can find the solution before heading to my explanation.

Warmup section:

  • Write a function that returns the lesser of two given numbers if both numbers are even, but returns the greater if one or both numbers are odd.
  • Write a function that takes a two-word string and returns True if both words begin with the same letter.

Level 1:

  • Write a function that capitalizes the first and fourth letters of a name.
  • Write a function that returns a sentence with the words reversed.

Level 2:

  • Write a function that returns True if an array contains a 3 next to a 3 somewhere in the array.
  • Write a function that returns the sum of 3 integers between 1 and 11 if their sum is less than or equal to 21, returns the sum minus 10 if the sum is more than 21 and there’s an eleven, or returns ‘BUST’ if the sum is greater than 21.

On second thought. Let’s save the challenging problems level for another blog post, since I have yet to do those.

Alright, were you able to find the solution to the 6 problems above? If you did, then you did it better than me, because I couldn’t solve the first exercise from level 2.

Also, this is probably not what I would do in a real life work situation. I would use Google, I would use Stackoverflow, I would use Copilot, but these are just fun exercises to test how much Python you know. So I didn’t look at the solution, although I did peek at GeeksforGeeks and W3Schools, because I may have forgotten one or two methods, but that’s it, I promise.

Alright, let’s take a look at the first two problems from the warmup section. First, we want to check if both numbers are even. So we already know that our function takes in 2 integers (also known as arguments), and that we can use an if-statement with a comparison operator. Furthermore, the mod % operator can help to check if a number is even or odd. And since it’s binary or like a boolean, we only need to check if the numbers are even or odd - we don’t have to explicitly check for both conditions.

If you’re not familiar with the mod % operator, it will basically return the remainder after a division.

4 % 2 will result in 0, because the number 2 fits 2 times in the number 4 without any remainder, or 0.

5 % 2 will result in 1, because the number 2 fits 2 times in the number 5 with a remainder of 1. This also tells us if a number is even or odd. If a number is divisible by 2 with no remainder, then that number is an even number. In other words, or in Python we can say, if number % 2 equals 0, then that number is even. This also shows how simple Python is to write, because that’s almost exactly the Python code we need to write to check if a number is even.

So we can now write:

def my_func(a,b):
    if a % 2 == 0 and b % 2 ==0:
        # rest of code

Remember, we need to check if both numbers are even, and we can use the Python and operator for that. If we only want to check for one of the two numbers, then we can use the Python or operator. Simple, right?

Next, if both numbers are even, we want to return the smaller number out of the two numbers, but if both numbers are not even, so either one or both numbers are odd, then we want to return the bigger number out of the two numbers.

We can use another if-statement to check which is the smaller number. So the statement is, if a is less than b, in other words, if a is the smaller number, then return that number. Otherwise or else, return b, which must then be the bigger number.

So we can now add to our code:

def my_func(a,b):
    if a % 2 == 0 and b % 2 ==0:
        if a < b:
            return a
        else:
            return b

Finally, to find the bigger number, which is essentially the opposite, we simply write the opposite statement, which is an else-statement to the if-statement, when we checked if both numbers are even.

So we can now add to our code:

def my_func(a,b):
    if a % 2 == 0 and b % 2 ==0:
        if a < b:
            return a
        else:
            return b
    else:
        if a > b:
            return a
        else:
            return b

Now there are different ways you can write your code and still arrive at the solution. The solution also wrote it like this, but they also gave an alternative version, which is more sleek.

However, I may have underestimated writing these blog posts, since I’ve only covered one problem, and it already is a pretty long blog post.

So I will end this blog post here, and I will show the more sleek solution together with the other solutions in another blog post.

Bye for now!