The Battle of The Function’s

-Python Edition-

Azzarya Chrisayudha
8 min readMar 18, 2024
Source : https://unsplash.com/photos/turned-on-macbook-pro-wit-programming-codes-display-f77Bh3inUpE?utm_content=creditShareLink&utm_medium=referral&utm_source=unsplash

You can’t live your python-coding life without a function, don’t we all agree on that? Whether to simply print a “Hello World” in string or to make an advanced-sophisticated program, we all rely our code on function’s. But let’s dive more into the world of function’s. Built-in, regular, and lambda. We’ll build from the ground-up, starting with the foundation of function, then we’ll compare them in detail. So, without further ado let the competition begin!

Function 101.

A block of code that performs a specific task which only runs when it is called and can be used repeatedly. That is exactly what you should answer when someone randomly asks you “Hey, what is a function?” There are 3 of function in python, such as built-in function, user-defined functions, and anonymous functions (also known as the lambda function)

Built-In Functions

Python 3.10 has a total of 68 pre-defined built-in functions in the standard library. These functions perform a specific task and can be used in any program, depending on the requirement of the user. It has a wide range of use, starting from the OG print() function to complex data manipulation. Here are TOP 5 most used python built-in function:

5. Input()

Let’s start our countdown with input(). We used this particular function to easily request any input from the user via the console. We can also assign the input to a variable. FYI: any input from the user will be returned as a string’s data type. Example:

When the code is run, this is the output on the console:

We should click on the console and type in an input, then this is what you’ll see:

Now lets print the input and check the data-type

Output:

Do you notice that even if we input a number, the data type of the input is still a string. This is an important note: any input will always be a string data-type. But what if we need other data types? Don’t you worry about a thing, we can use casting. Casting is a process of converting a data-type into another data-type. Let’s say we want the ‘year’ variable in the example as an integer data-types, here’s how you can do it:

Below the input variable we can make a new variable to save our casted input, then we can cast the data types by simply type what data types we want to cast into, in this case we want it to be an integer, so we simply type int(input_variable_name). And this is the output:

Now the data-type is already converted into an integer, so now we can apply it to a math operation for example. Casting is also applicable to any other data types. Please note that we can convert a data-type if it meets the requirements of that specific data type, for example : if you try to convert an alphabet character to an integer or float, it will return an error, the same goes for any other data-types.

4. Range()

This is a function to generate a sequence of numbers. We can input up to 3 arguments inside this function. range(start:stop:step). The ‘start’ argument is used to specify the starting point of the numbers sequence.

By default, the ‘start’ value is 0. Then we have the ‘stop’ arguments to determine the ending point of the sequence (Please note that the ‘stop’ argument is exclusive, meaning that the ending point will not be included in the sequence). And, lastly we can input the ‘step’ arguments to specify the difference of each number in our sequence. By default, the ‘step’ value is 1. Here is an example to help you understand it more:

Make a numbers sequence starting from 0–10 with the difference of 2.

Output:

Notice that even though we input 10 as the stop parameters, it’s not included. That’s why the stop is exclusive (in other words it won’t be included)

3. Len()

Now we enter the top 3 of this list. len() takes the third place, while it is simple, it is very useful and important. Because this function can help us to get the length of an object. The object can be string, tuple, dictionary, list, or any other iterable. Imagine if we’re working with a dataset contains thousand’s of data in a list, and we have to know how many data’s are there in that particular list, Rather than counting it manually 1 by 1 that will takes a very long time and it would be a very stressful task, we could simply use len(). Here are some examples of the functions implemented in our code:

Let’s say we want to know how many data are there on the ‘x’ variable then we use the len() function. It is most valuable when we’re working with an unknown numbers of data. Or we can also use it in a for loop statement to loop the whole data on a variable.

2. Type()

Next the runner up is none other than type() function. We use it to determine the data type of an object. It returns the data <class ‘type’> whether it’s a simple string, an integer, boolean, a collection data types, etc. Example:

  1. Print()

Isn’t it obvious who will take the number 1 spot in the most used python function? Without a glimpse of doubt, it is surely taken by the one and only, print() function. It is used to make an output of text or variables, and it can take one or more arguments. This is the starting function of all the beginners out there trying to learn python.

print(“Hello World”)

From the simplest of programs, to a sophisticated program built by a complicated block of code, print() will always be there. It’s robust, timeless, and efficient. We can also use formatting to output variables in a specific format:

User-Defined Functions

If the built-in function is already predefined, unchangeable, and stricted by its requirements. You can also define your own function by using the def keyword, It offers immersive flexibility. The limit is your imagination (and your skills of course). And that’s where this type of function excels and even dominates the built-in type. It can also be called many, many times. So if you have a set of code that you will use multiple times in your program, regular function’s is your go-to solution. All you need to do is to call the function names. So how do we actually make our own function? Here are some examples with tutorials:

Let’s make a simple function to determine whether a number is odd or even:

So, if we call the function and enter the parameter, this will be the output:

Note that we can call the functions multiple’s times for as much as we need.

Lambda Functions

A lambda function is also a user-defined function, but it’s a small anonymous function. It can take any number of arguments, but can only have one expression. True to its name, the keyword for this function is “lambda”. It is not defined by its name, rather it is anonymous/nameless.

It is typically used for a much shorter function, that’s why we can call this type of function as a one-liner function based on its characteristic. Have you wondered how to create a lambda function? I’ll show you how:

Let’s make a simple lambda function to return the multiplication result of 2 numbers:

We can also use lambda function to filter our data without altering the number of data in the list:

Then we can alter the data by using map and lambda function:

Last Functions Standing

And now, enough talk, let the action begin. This is where the real show started. Who will prevail? Who will suffer? Only time and fact’s will tell. So,let the show begin and may the best function’s type win!

Conclusion

After a series of hard-fought battles, there’s no clear winner. At the end of the day, function is only a tool that can help us achieve victory through hundreds of bugs and errors, and make a maintainable program. This article is never about the function’s. Rather, it’s us, the programmers, who can unleash the true power of function’s. Many times, a simple built-in function is all we need. Simplicity at its finest. Some other time we may need to make a block of code as its own function, which we can use multiple times through the program. And on another day a simple one-liner, lambda function could be our go-to. When it is used correctly, function’s will be a powerful weapon for us to achieve glory. GLORY, GLORY, FUNCTIONS!

“An idiot admires complexity, a genius admires simplicity” — Terry Davis

--

--