Signed and Unsigned Integer Arrays in Python

In this tutorial, we will learn how we can declare, use unsigned and signed integers arrays in python.

Python Array

Array is a critical data structure for most of the programming languages. In Python, they are containers that enable us to hold multiple items at the same time.

Precisely, arrays are an ordered arrangement of the elements where every value is of the same type. That is the main characteristic of arrays in Python – they only can contain multiple items in a sequence and that they value must have the same data type.

Syntax for importing array in python

In python, array’s can be declared by using the “array” module. Let us now see it’s syntax:

import array as array_name
  • import : command to import module
  • array : name of the module
  • array_name : name of the array

Declaring array in python

Syntax for declaration of array is:

arr_name=array_name.array(type_code,elements)
  • arr_name = array’s name
  • array_name = name of the array which we used in importing it
  • type_code = it specifies what kind of elements can be stored in the array
  • elements = any elements which we want to provide

Signed & Unsigned integers in python

Signed integer :

It is defined by using type_code “i” (small alphabet “i”) and it contains negative and posited integers.

Unsigned integer :

It is defined by using type_code “I” (Capital alphabet “I”) and it contains only positive integers

Finding the Length of an Array in Python

We can easily find the length of an array by using the len() method.It is an built-in method.

program:
import array as arr
x = arr.array("I",[5, 10, 15])
print(len(x))
output:
#output
#3

Array Indexing in Python

Each item from an array has its address. To access any of them, its index is indicated. In Python, just as in any programming languages and in computing in general, counting starts from 0. When addressing any item, it is important to remember this and not refer to the number 1.

That is, to get the first item from the array, it is necessary to specify 0 as an index. To access any element, first the name of the array is written, and after the square brackets, there is mentioned the order number of the element:

array_name[index_value_of_item]
import array as arr
x=arr.array("I",[5, 10, 15])
print(x[0])
print(x[1])
print(x[2])
Output :
5

10

15

Program for signed and unsigned integer array

# importing array class to use array 
import array as arr

# an unsigned int type of array 
#declaring and assigning elements to the array
x = arr.array ("I", [5, 10, 15, 20, 40] )
# print type of a 
print("Type of x: ", type(x))
# print array 
print("Array x is: ")
print(x)

# an signed int type of array 
# declaring and assigning elements to the array
y = arr.array ("i", [5, -30, 10, -20, 40] )
# print type of y 
print("Type of y: ", type(y))
# print array 
print ("Array y is: ")
print(y)

Output

Type of x : <class 'array.array'>
Array x is:
array("I", [5, 10, 15, 20, 40])
Type of y : <class 'array.array'>
Array y is:
array("i", [5, -30, 10, -20, 40])

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top