Array in Python

Array in python are used to store number values .

Use of Array in Python:

import array as arr
a1=arr.array('i',[1,2,3,4])
print(a1)

  Output:
  array(‘i’, [1, 2, 3, 4])

Here i is the used for type integer similarly float may be declared as f and double as d.

import array as arr
a1=arr.array('d',[1.7,2.0,3.5,4.9])
print(a1)

   Output:
   array(‘d’, [1.7, 2.0, 3.5, 4.9])

To access an element of the array index is used
to store an element at given index
a1[1]=7.7
changes the value at index 1 so output now changes to

  Output:
  array(‘d’, [1.7, 7.7, 3.5, 4.9])

  All the elements of an array may be printed with the help of for loop.

import array as arr
a1=arr.array('i',[1,2,3,4])
for i in a1:
 print(i)

similarly an element of the array may also be printed as following

  print(a1[0]) # this prints the 0th index element of the array a1.

In python interesting this with array is we can direclty access the last element of the array with index

the index for the last element in array is [-1] , second last element index is [-2] and so on..

import array as arr
a1=arr.array('i',[1,2,3,4])
print(a1[-1])
print(a1[-2])

  Output:
   4
   3

Click Here

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial