{"id":26592,"date":"2022-04-08T23:31:07","date_gmt":"2022-04-08T18:01:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=26592"},"modified":"2022-04-08T23:31:07","modified_gmt":"2022-04-08T18:01:07","slug":"how-to-pass-an-array-to-a-function-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-pass-an-array-to-a-function-in-python\/","title":{"rendered":"How to Pass an Array to a Function in Python?"},"content":{"rendered":"
Here, in this article let us look at how to pass an array to a function in Python.<\/p>\n
In Python, any type of data, such as a text, list, array, dictionary, and so on, can be given as an argument to a function. When we send a numeric array as an argument to a Python method or function, it is passed by reference.<\/p>\n
Function in Python:<\/strong><\/p>\n A function is a block of code that only executes when it is invoked. Data, known as parameters, can be passed into a function. As a result, a function can return data.<\/p>\n A function is a set of related statements that accomplishes a certain task.<\/p>\n Functions help in the division of our program into smaller, modular parts. As our program increases in size, functions help to keep it organized and manageable.<\/p>\n It also avoids repetition and makes the code reusable.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Here, in this article let us look at how to pass an array to a function in Python. In Python, any type of data, such as a text, list, array, dictionary, and so on, can be given as an argument to a function. When we send a numeric array as an argument to a Python …<\/p>\nPassing an Array to a Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import all from array module using the import keyword\r\nfrom array import *\r\n# Create a function say printing_array() which accepts the given aray as an argument\r\ndef printing_array(arry): \r\n # Inside the function, loop in the above passed array using the for loop\r\n for k in arry:\r\n # Print each element of the given array. \r\n print(k)\r\n\r\n# Pass 'i', some random list as arguments to the array() function to create an array.\r\n# Store it in a variable.\r\n# Here 'i' indicates the datatype of the given array elements is integer\r\ngvn_arry = array('i', [1, 5, 3, 8, 2])\r\n# Pass the given array as an argument to the above created printing_array() function\r\n# to print an array\r\nprinting_array(gvn_arry)<\/pre>\n
1\r\n5\r\n3\r\n8\r\n2<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import all from array module using the import keyword\r\nfrom array import *\r\n# Create a function say printing_array() which accepts the given aray as an argument\r\ndef printing_array(arry): \r\n # Inside the function, loop in the above passed array using the for loop\r\n for k in arry:\r\n # Print each element of the given array. \r\n print(k)\r\n \r\n# Give the list as user input using the list(),map(),split(),int \r\n# functions and store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass 'i', above given list as arguments to the array() function to create an array.\r\n# Store it in another variable.\r\n# Here 'i' indicates the datatype of the given array elements is integer\r\ngvn_arry = array('i', gvn_lst)\r\n# Pass the given array as an argument to the above created printing_array() function\r\n# to print an array\r\nprinting_array(gvn_arry)<\/pre>\n
Enter some random List Elements separated by spaces = 11 3 6 2 4\r\n11\r\n3\r\n6\r\n2\r\n4<\/pre>\n