How To Rewrite Python’s Max Function

Are you looking to rewrite the Max function present in Python to accommodate your requirement? In this article, I will show you how you can do that.

You can rewrite or create the max[1] function of Python from scratch. It is possible to use the max() built-in method to return the largest item in an iterable, as well as the largest of two or more parameters.

Creating or Rewriting the Max Function in Python

You can create a new function named max in Python and pass on the variable whatever you defined in the function to return the desired result.

Let me create a max function that returns the character with high precedence. For example, if I give two input characters as ‘a’ and ‘b’, ‘b’ should be returned as the max value of the function.

#defining the max function
def max(input1, input2):
    if input1 > input2:
        return input1;
    else:
        return input2;

#Initializing the input variables
input1 = 'a'
input2 = 'b'

#Printing the Maximum value returned from the Max Function
print("The Maximum is: " + max(input1, input2))

Output:

The Maximum is: b
How To Rewrite Python's Max Function

Wrap Up

As shown in the above code, I have rewritten the max function of the Python from the scratch and returned the maximum character based on its precedence. Similarly, you can rewrite the same function to any type of comparison you want to make and return the maximum out of it.

I hope the above tutorial helped you to make your own max function from scratch. Let me know if you have any better code or you want to set a function or modify it to something else and are not able to do it. I will be happy to help you people.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python: How To Check If List Is Empty
  2. How To List All Files Of A Directory in Python
  3. How To Remove A Specific item From An Array
  4. How to Remove Last Element From List In Python
  5. Equivalent to “source” in OpenBSD? Python Linux/Unix
  6. Trapping Rain Water Problem Python JAVA C++ Solution

Leave a Comment