All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Define A Function In Python

Last Updated : Mar 11, 2024

How To Define A Function In Python

In this article we will show you the solution of how to define a function in python, a Python function is defined with the def keyword, followed by the function's identifier (name) followed by parentheses and a colon.

After that, you need to indent with a tab or four spaces, and then you can specify what you'd like the function to do.

My goal with this article is to teach you how to define a Python function and call it, so you can break down the code of your Python application into smaller chunks.

By putting arguments inside the parenthesis, you are able to pass arguments into a Python function.

We will now discuss the idea of how to define a function in python with an example.

Step By Step Guide On How To Define A Function In Python :-

# Python code to demonstrate the use of variable-length arguments
# Defining a function
def function( *args_list ):
    ans = []
    for l in args_list:
        ans.append( l.upper() )
    return ans
# Passing args arguments
object = function('Talkers', 'Code', 'tutorial')
print( object )
# defining a function
def function( **kargs_list ):
    ans = []
    for key, value in kargs_list.items():
        ans.append([key, value])
    return ans
# Passing kwargs arguments
object = function(First = "Talkers", Second = "Code", Third = "Tutorial")
print(object)
  1. The first step is to define two functions that accept variable-length arguments args_list and *kargs_list, respectively.
  2. An empty list called and is initialized in the function that takes the argument list from the caller.
  3. In each loop, we convert the arguments in args_list to uppercase, append them to ans, and then loop through them again.
  4. Finally, we return the ans list.
  5. We call the above-defined function function with arguments as strings - 'Talkers', 'Code', and 'tutorial'. We store the output in the variable object.
  6. We then print the output stored in the object variable. This returns a list with all the input strings in uppercase.
  7. In the function that takes **kargs_list, we initialize an empty list called ans to store the output.
  8. We loop through each key-value pair in the kargs_list dictionary using the items() function.
  9. We append a list containing the key-value pair to the ans list.
  10. As a final step, we return the list of answers.
  11. As a result of the above definition, we call the above-defined function with a keyword argument as follows: First = "Talkers", Second = "Code", and Third = "Tutorial". A variable object is used to store the output.
  12. Our next step is to print the output stored in the object variable. It returns a list of lists containing key-value pairs from the input dictionary, with each list containing one key-value pair.

Conclusion :-

As a result, we have successfully learned how to define a function in python with an example.

The Python function is a reusable piece of code that performs a single, related operation. The purpose of this article is to discuss a Function defined by the def statement.

A colon (:) should be placed inside each code block within a function and the block should be indented (space).

Whenever you use an argument or an input parameter, you should place them in parentheses.

I hope this article on how to define a function in python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪