What is Python List?

      List is one of the data structure in python. You can add items inside a list separated by commas. All the items are enclosed within a pair of square brackets.

Declaring a list in Python:

List of integers:

     i = [ 1 , 2 , 4 , 6 , -7 ]

List of string items:

     s = [ 'hi' , 'abc' , 'hello' ]

List of mixed items:

     m=[ 1 , 2.0 , 'hi' , 'z' , [1,2,3] ]

So python lists can take in any type of items.

Numpy Array :

     Python List is like a single dimensional array. Numpy array is a multidimensional array.

     Numpy arrays can only hold items which are of same type unlike lists.

Declaring a numpy array:

 

import numpy as np

#integer array example:
intNpArray = np.array([ [1,-2,5] , [3,7,1] ])

#float array example:
fltNpArray = np.array([ [1.0,-2.1,5.3] , [3.2,7.3,1.1] ])

#string array
strNpArray = np.array([ ["fg","hello"] , ["a","cd"] ])

 

print(intNpArray)


print(fltNpArray)


print(strNpArray)                                                                  

Output:

[[ 1 -2 5] [ 3 7 1]]
[[ 1. -2.1 5.3] [ 3.2 7.3 1.1]] [['fg' 'hello'] ['a' 'cd']]