To define an array in Python, you can use the built-in array
module or simply use a list.
Here is an example of defining an array using the array
module:
main.py124 chars8 lines
Output:
main.py28 chars2 lines
In the above example, we imported the array
module and then used the array()
function to create an array of integers. The first argument to the array()
function is the type code ('i'
for integers) and the second argument is a list of values to be stored in the array.
Alternatively, you can define a list and use it as an array in Python:
main.py93 chars6 lines
Output:
main.py16 chars2 lines
In the above example, we created a list of values and treated it as an array. This is because Python does not have a built-in array data type, but lists can be used as arrays in most cases.
gistlibby LogSnag