Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What are type codes and methods in arrays in Python?

Abhishek Pusuluri

Arrays of different datatypes (type codes)

Type code C type Python type Minimum size in bytes
b signed char int 1
B unsigned char int 1
u Py_code unicode character 2
h signed short int 2
H unsigned short int 2
i signed int int 2
I unsigned int int 2
l signed long int 4
L unsigned long int 4
q signed long long int 8
Q unsigned long long int 8
f float float 4
d double float 8

Syntax

array(data_type,value_list)

Here, the data_type or type_code indicates the type of contents of the array.

For example:

import array from arr
arr=array("i",[1,2,3,4,5,6])
print(arr)

"i" indicates that the contents are of integer type.

Array methods

Python supports various built-in methods that can be used for arrays.

The methods that are supported are as follows:

Method Description
append(x) Adds a new item with value x to the end of the array
extend() Adds the elements of the list to the end of the current array
insert(i,x) Adds an item with value x in the array before position i
remove(x) Removes the first occurrence of x from the array
pop(i) Removes the item with index i from the array
clear() Removes all the elements from the array
copy() Returns a copy of the array
count(x) Returns the number of occurrences of x in the array
index(x) Returns the index_value(position) for the first occurrence of x in the array
reverse() Reverses the order of the items in the array
sort() Sorts the array

Code

import array as arr
a=arr.array("i", [1,2,3,4,5,6] )
print(len(a))

RELATED TAGS

python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring