- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to pass the tuple as function arguments
Tuples are an important data type in Python and are often used to store a fixed set of elements. In this article, we will be discussing how to pass a tuple as function arguments in Python. We will go over the syntax for passing tuple arguments and will provide examples of how to do so.
Let us first understand the basics that, we will be needing to begin approaching the problem. The problem asks us to pass a tuple as a function argument, for that we need to know what a function in python is, what are function arguments, and what are tuples in python.
What is a function in python?
A function in any programming language is a piece of code containing a block of statements that perform a specific function or task and may be required more than once, during the execution. It is important as they provide us with code reusability and are helpful in maintaining a code that is less prone to bugs, due to reduced redundancy.
Functions in python can be declared using the “def” keyword, all the code inside the function needs to be properly indented so that the compiler knows the proper definition of the function. A function in python can be defined as follows −
def functionName(): statement 1 statement 2 # End of function
A function is normally called using the name of the function followed by round braces. Given below is an example of a function call.
The function defined above is the simplest form of function that does not take any input and does not return anything. But python provides us a way for us to do those things as well.
Arguments of a function
There are times when we need to have a function that takes some form of input along with it for processing. So, how can we achieve this? We can do so using function arguments.
A function argument is an input that a function needs to have whenever it is called. The function arguments are defined within the circular braces of a function call. For example −
# Function definition def functionWithArgs(argument1, argument2): statement 1 statement 2 functionWithArgs(arg1, arg2) # function call
We are not limited by any datatype or number of arguments we can pass to a function. We can pass any built-in datatypes as well as user-defined datatypes, and instances of the class. Along with passing input parameters, we can return values too.
Now we know what a function in python is and how we can use arguments to give input to a function for further processing. Let us now look at what a tuple is.
Tuple in Python
A tuple in python is a pre-defined datatype that acts as a container and can hold different types of, heterogenous, data within it. The point to note is that they are immutable, hence they cannot be modified after creation. All the elements of the tuple are stored in parentheses separated by a comma. The syntax for defining a tuple is given below.
A = (1, 2, 3)
Now we have all the basics that we need to know to solve the problem. Let us now discuss the ways to approach it.
Passing static tuples as arguments
As we know that we can use arguments to pass input to a function. To pass a tuple we just have to specify a tuple object within the round braces at the time of function definition.
Do note that we just have to specify a variable name, its value will be the tuple we define at the time of the function call.
Step 1 – Define a function with a variable as a parameter
Step 2 – Within the function, print the value the variable holds
Step 3 – Make a function value with a tuple inside the parentheses
def tupleArg(inputTuple): print("Tuple argument passed as input to the function is: ", inputTuple) tupleArg((1, 2, 3))
Tuple argument passed as input to the function is: (1, 2, 3)
Using user input tuples
The program above discussed how we can pass a static tuple as an argument. But in many cases these tuples are dynamic in nature. This is why we will now be focusing on taking a tuple as input from user and passing it as an argument to a function.
Step 1 – Create a variable to store the tuple
Step 2 – Take a string of values from user as input
Step 3 – Split the input string, map every element to an integer and convert the whole map object to a tuple
Step 4 – Create a function that takes a variable as parameter
Step 5 – Within the function print the input tuple
Step 6 – Call the function with the user input tuple as argument
A = tuple(map(int, input("Enter the elements of tuple : ").split())) def tupleArg(inputTuple): print("Tuple argument passed as input to the function is: ", inputTuple) tupleArg(A)
Enter the elements of tuple : 12 33 776 339 Tuple argument passed as input to the function is: (12, 33, 776, 339)
In this article, we focused on three different ways to pass the tuple as a function argument. We learned what a function in python is, what is meant by a function argument and how can we pass both, static as well as dynamic, tuples
- Related Articles
- How to pass arguments by value in Python function?
- How to pass arguments by reference in Python function?
- How to pass the individual members as arguments to function using structure elements?
- How to pass arguments by reference in a Python function?
- Java Program to pass method call as arguments to another method
- How to pass Python function as a function argument?
- Java Program to Pass ArrayList as the function argument
- Swift Program to Pass Dictionary as the function argument
- How to pass individual members of structure as arguments to function in C language?
- How to pass a dictionary as argument in Python function?
- How to pass command line arguments to a python Docker container?
- How to pass a json object as a parameter to a python function?
- Haskell Program to pass an array to the function
- Haskell Program to pass a string to the function