Python Functions: Because "Normal" is Overrated!

Photo by Elsa Noblet on Unsplash

Python Functions: Because "Normal" is Overrated!

·

5 min read

More than a year ago, Mr. Anil Kulkarni introduced a course entitled "Learn Python By Thinking in Types" through the widely acclaimed platform Freecodecamp. My engagement with the course extended to approximately three hours. As the curriculum delved into crafting Python Functions, I found it to be a reasonable subject for exploration. I'd like to point out that my intention is not to offer criticism; rather, I aim to juxtapose conventional methods of function composition with Mr. Kulkarni's unique approach.

For a refresher on the functionality of Python Functions (excuse the pun), please refer to my article titled "Scout's Honor: How Python Functions Earn Their Merit Badges and Save the Code Day!" available at https://jmn950-dev.hashnode.dev/python-functions-the-art-of-variable-teleportation

Additionally, I must express my decision to transition from Visual Studio due to recurrent instances of system crashes. Consequently, I have adopted Jupyter Notebook as my primary platform for Python code development. I extend my sincere apologies for this substantial alteration, especially considering my existing blog post detailing the installation process for Visual Studio and other Python packages. Although I remain open to the use of Visual Studio in the future, my current preference for Jupyter Notebook is rooted in its ease of use and my familiarity with the platform. Notepad++, on the other hand, served as a tool for writing conventional Python code, and the transition primarily pertains to my data analysis work, whereas Jupyter Notebook offers a more intuitive and familiar environment.

With that said, let the action begin.

Example 1

Let's create a function that just prints the details of a person. In my case, it will be William Wilberforce.

The program is designed to be self-explanatory, obviating the need for an exhaustive explanation. For a comprehensive understanding of Python functions and their functionality, kindly refer to my article dedicated to elucidating their intricacies. Nevertheless, please direct your attention to the ensuing example. Can you discern any notable distinctions?

Let's explain the program.

  1. The Python function name did change but everything else remains the same.

  2. I declare a function named biography_two. This function takes two parameters, name, and achievement, both of type string (str). The -> None syntax indicates that the function does not return any value (None).

  3. Inside the function, two print() statements are used to output information. The f"..." syntax is an f-string, allowing variables (name and achievement in this case) to be embedded inside strings.

  4. Now, I call the biography_two function with specific arguments. The provided arguments are "William Wilberforce" for the name parameter and "Ending the slave trade" for the achievement parameter.

  5. When the biography_two function is called with the specified arguments, it prints the information about the person (name) and their achievement to the console.

Example 2

In this example, we will establish a Python function designed to perform the addition of two numerical values.

The program is designed to be inherently comprehensible; therefore, emphasis will be directed toward elucidating the methodology of its implementation, following the instructional guidance provided by Mr. Kulkarni.

  1. I define a function named simple_add. The function takes two parameters (a and b), both of which are expected to be of type integer (int). The -> int annotation indicates that the function is expected to return an integer.

  2. Inside the function, a new variable c is assigned the sum of a and b. This line performs the addition operation.

  3. The function uses the return statement to provide output. It returns a formatted string using an f-string, indicating the total value of the addition. The value of c is embedded in the string, providing context to the result.

  4. This line calls the simple_add function with arguments 10 and 10. The result is stored in the variable result.

  5. When the function is called, it returns a string indicating the total value for the addition of 10 and 10, which is 20.

Example 3

Kindly reference Example 1, elucidating a Python function within the article titled "Python's Greenhouse: Nurturing Growth with Nested Functions," accessible at the following link: https://jmn950-dev.hashnode.dev/pythons-greenhouse-nurturing-growth-with-nested-functions. Subsequently, let's proceed to construct a function utilizing Mr. Kulkarni's methodology for writing Python Functions to explore its efficacy.

  1. The calculator function takes an operation as input and returns either the add or subtract function based on the specified operation. The nested add and subtract functions perform addition and subtraction, respectively. Both functions take two float parameters and return the result as a float.

  2. The calculator function checks the value of the operation parameter. If it's "add," it returns the add function; if it's "subtract," it returns the subtract function. If the operation is neither "add" nor "subtract," it prints an error message.

  3. The user is prompted to input the desired operation, either "add" or "subtract."

  4. Two functions, addition, and subtraction, are created using the calculator function, specifying the desired operation for each.

  5. The user is prompted to input two numbers.

  6. Based on the user's chosen operation, either addition or subtraction is performed using the corresponding function (addition or subtraction). The result and the operation symbol are determined accordingly.

  7. The program displays the result of the calculation using an f-string, including the numbers, operation symbol, and the calculated result.

The function works the same way as the example 1 function of the article titled "Python's Greenhouse: Nurturing Growth with Nested Functions".

Embracing the intricacies of crafting Python Functions in this distinctive manner is truly captivating. While it may initially present a challenge in contrast to conventional approaches, the learning curve is surmountable. I am committed to adopting this methodology in my Python Functions, albeit intermittently reverting to conventional styles. I encourage you to subscribe to Mr. Anil Kulkarni's YouTube channel and explore his Python tutorial on Freecodecamp, both serving as invaluable repositories for free and exceptional educational content spanning programming, computer science, mathematics, and beyond. This resource is an invaluable asset for comprehensive learning in technology. In the next article, we shall touch on Args and Kwargs. Stay tuned.