Python: Introduction to Numpy and To create several types of Array

What is Numpy?





Numpy is a module that contains several classes, functions or variables, etc. To do with scientific calculations in Python. Numpy is useful to create and also process single and multidimensional arrays.

An array is an object that store a group of elements of same data type it means that in case of numbers we can store only integer or only float but not one integer and one is a float. If you want to work with Numpy we need to import the Numpy module.

Example 1:

import numpy
arr=numpy.array([1,2,3,4,5,6,7])
print(arr)
output: 
array([1,2,3,4,5,6,7])

Example 2:

import numpy np
arr=np.array([1,2,3,4,5,6,7])
print(arr)
output: 
array([1,2,3,4,5,6,7])

Example 3:

from numpy import*
arr=array([1,2,3,4,5,6,7])
print(arr)

output: 
array([1,2,3,4,5,6,7])

Creating an array can be done in several always:

1.Using array()function

2.Using arrange()function

3.Using Zeroes() and once() function

1.Using array()function:

>>arr=array([1,2,3,4,5,6,7],int)

>>type(arr)

<class 'numpy.nparray'>

>>arr

array([1,2,3,4,5,6,7]

>>arr=array([1.5,2.4,3.7,4.3,5.8,6.9,7.1],int)

>>arr

array([1,2,3,4,5,6,7])

>>arr=array([1.5,2.4,3.7,4.3,5.8,6.9,7.1],float)

>>arr

array([1.5,2.4,3.7,4.3,5.8,6.9,7.1])

For String no need to specify data type

>arr=array(['a','b','c'])

>>print(arr)

['a' 'b' 'c']

2.Using arrange()function:

arrange(start,stop,steppoint)
>>from numpy import*
>>a=arrange(2,11,2)
>>>a
array([2,4,6,8,10])

3. Creating an array using zeros() and ones() functions:

zeros(n, datatype) --> create array with all zeros
ones(n,datatype)--> create array with all 1's
>> from numpy import*
>> a=zeros(5, int)
>>a
array([0,0,0,0,0])
>> b= ones(5,int)
>>b
array([1,1,1,1,1])

Mathematical Operations on arrays:

>>arr = array([1,2,3,4,5])
>>arr1=arr+5
>>arr1
array([6,7,8,9,10])




SCALA Iterators & Arrays With Examples

SCALA ITERATORS:

Scala iterator is not a collection, but rather a way to access the elements of a collection one by one.




Here two basic operations on iterators

1.next

2. hasNext

A Call to it.next() will return the element of the iterator and advance the state of the iterator.

A Call to it.hasNext() will return more elements of the iterator

Example:

scala > val itObject = Iterator (“Hadoop”, “Scala”, “Spark”);

itObject: Iterator[String] = non – empty iterator

scala > while (itObject.hasNext) {

println(” ” + itObject.next())}

Hadoop

Scala  Spark

scala > println (“Size of Iterator : ” +itObject. size)

Size of Iterator : 3

SCALA ARRAYS:

Scala array is a normal array. To create an array of integers below like this:

scala > var arrayEle = Array (1,2,3,4,5,6,7,8) ;

arrayEle: Array[Int] = Array (1,2,3,4,5,6,7,8)

scala > for (x < – arrayEle) {

println(“Array elements”+ x)}

scala > println (arrayEle. length)

O/p: 8