Printing the output of a function in Python is a simple but important task. It allows you to see the results of your functions, helps with debugging, and is a helpful skill when learning Python.
In this tutorial, we will examine how to print a function’s output in Python. This can be done using Python’s built-in print()
function, which takes the function as an input and prints its output.
Step 1: Define a Function
In order to start, you need a function to work with. Let’s define a simple function named multiply()
that takes two arguments and returns their product:
1 2 |
def multiply(a, b): return a * b |
Step 2: Call the Function
Now, let’s call the function with two arguments. In this case, we will multiply the numbers 2 and 5.
1 |
result = multiply(2, 5) |
The variable result
now stores the product of the two numbers.
Step 3: Print the Output of the Function
Finally, we can use the print()
function to display the output of the multiply()
function.
1 |
print(result) |
This will print the value stored in the variable result
. Alternatively, you can skip the step of assigning a variable and directly print the function:
1 |
print(multiply(2, 5)) |
This will directly print the output of the function without the need for a separate variable.
Full Code
Here is the full example code that demonstrates all the steps:
1 2 3 4 5 6 7 |
def multiply(a, b): return a * b result = multiply(2, 5) print(result) print(multiply(2, 5)) |
Output
10 10
Conclusion
In this tutorial, we covered how to print out the output of a function in Python using the print()
function. By defining your function, calling it with the desired input, and using the print()
function, you can easily display the output of any function in Python.