what is an array in python...

# agenda


Q.1 what is an array in python ?

ans. An array is basically a data structure which can hold more than one value at a time . It is a collection or ordered series of elements of the same type.

Q.2 is python list same as an array ?

ans. python array and list have the same way of storing data.

Array take only a single data type elements but lists can have type of data.

therefore, other than a few operation the kind of operations performed on than are different.

Q.3 How to create an array in python ?

ans. array is python can be created offer importing the array module.

  1. without alias -->>>import array
  2. using Alias-->>>import array as arr
  3. using *--->>>from array import*
Q.4 Accessing array elements .
ans. 
  1. Access elements using index value 
  2. indexing starts at 0 and from 1 . Hence, the index number is always 1 less than the length of the array .
  3. Negative index value can be used as well . the point to rember is that negative indexing starts from the reverse order of traversal i.e from to right to left .
Q.5 Basic array operations in python .
ans.
1.finding the length of an array 
  • length of an array is the number of elements that are actually present in an array .
  • you mean make use of len() fuctionto achieve this .
  • the len()function returns an integer value that is equal to the number of elements present in that array.
2. Adding elements to an array.

functions used to add elements to an array
.append()
used when you want to add a single element at the end of an array .
.extend()
used when you want to add more than one element at the end of an array.
.insert()
used when you want to add an element at a specific position in an array. 

the code snippet below implements the append() , extend() and insert() functions.

3. Removing elements of an array

function used to remove elements of an array :
.pop()
used when you want to remove an element an return it
.remove()
used when you want to remove an element with a specific value without returning it

4. Array concatenation ( + symbol )

Array concatenation can be done as follows using the '+' symbol

5.slicing an array .

An array can be sliced using the : symbol  this returns a range of elements that we have specified by the index number.

6. looping through an array 

we can loop through an array easy the for and while loop.
 
for loop 
iterates over the items of an array specified number of times.

while loop 
iterates over the elements until a certain condition is met










Comments

Post a Comment

Popular Posts