Python Return Arguments From Function

5/5 - (1 vote)

πŸ’¬ Question: How to return one or multiple arguments from a Python function?

Basically, creating a function that reflects one or more arguments like a mirror:

Let’s find out! πŸ‘‡

Method 1: Return Single Argument

A function can return a single argument by passing that argument variable after the return keyword. If the argument name is arg, you can return that argument from the function using return arg.

Here’s an example:

def f(a):
    return a

print(f(1))
# 1

print(f(2))
# 2

print(f('Alice'))
# Alice

Method 2: Return Multiple Arguments

A function can return a fixed number of arguments as a tuple by passing the argument variables after the return keyword. If the argument names are a, b, and c, you can return those arguments from the function using return a, b, c. This implicitly creates a tuple, i.e., it’s the same as return (a, b, c).

Here’s an example where we return three arguments:

def f(a, b, c):
    return a, b, c

print(f(1,2,3))
# (1, 2, 3)

🌍 Recommended Tutorial: How to Return a Tuple from a Python Function?

Method 3: Use Asterisk Operator

A function can return a variable and dynamic number of arguments as a tuple by capturing all arguments using the *args asterisk approach in the parameter list and passing the argument variables after the return args keyword. This implicitly creates a tuple, i.e., it’s the same as return (a, b, c).

Here’s an example where we return an arbitrary number of arguments as decided dynamically at runtime by the caller of the function:

def f(*args):
    return args

a, b, c = f('hello', 42, 3.14)

print(a)
print(b)
print(c)

The output is:

hello
42
3.14

🌍 Recommended Tutorial: The Asterisk Operator in Python

What is the type of the function output? It’s a tuple like before:

print(type(f(1, 2, 3, 4, 5)))
# <class 'tuple'>

🌍 Recommended Tutorial: Multiple Assignment Operator

Method 4: Return Arbitrary Keyword Arguments From a Python Function

Now, this may be new for you if you are not already a super skilled Python coder.

To return an arbitrary number of keyword arguments from a Python function, you can capture the keyword arguments in a dictionary using the double-asterisk operator **kwargs and then return that dictionary using return kwargs.

Here’s what I mean:

def f(**kwargs):
    return kwargs

print(f(alice=18, bob=24, carl=30))
# {'alice': 18, 'bob': 24, 'carl': 30}

If this is all greek to you, I’d recommend you check out the fulling in-depth Python guide and keep improving your coding skills:

🌍 Recommended Tutorial: Python Double Asterisk Operator

Summary

You can return one or multiple argument values from a Python function by specifying them, comma-separated, right after the return keyword. This implicitly creates a tuple of argument values that can then be captured by a multiple assignment expression or by a single tuple assignment.


Thanks for reading through the whole article! Feel free to keep learning and improving your skills by joining our email academy. it’s free!

Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd