Google News
logo
Python Program to Create an Array
In the following example of Python program create arrays using the `array` module. Here is an example program to create an array :
Program :
import array

# create an array of integers
int_arr = array.array('i', [1, 2, 3, 4, 5])

# create an array of floats
float_arr = array.array('f', [1.1, 2.2, 3.3, 4.4, 5.5])

# create an array of characters
char_arr = array.array('u', ['a', 'b', 'c', 'd', 'e'])

# print the arrays
print(int_arr)
print(float_arr)
print(char_arr)
Output :
array('i', [1, 2, 3, 4, 5])
array('f', [1.100000023841858, 2.200000047683716, 3.299999952316284, 4.400000095367432, 5.5])
array('u', 'abcde')
In the above program, we first import the `array` module. Then we create three different arrays of integers, floats, and characters using the `array` function.

The first argument to this function is the type code of the array, which is a string that specifies the type of the elements in the array.

In this example, we use 'i' for integers, 'f' for floats, and 'u' for Unicode characters. The second argument is a list of values to initialize the array.

We then print the arrays using the `print` function. Note that the output of the character array is displayed as a string, with the letter 'u' before the string to indicate that it is a Unicode string.