11 – Json Handling and Numpy Basics

JSON

  • JSON stands for JavaScript Object Notation 
  • JSON is a lightweight format introduced for easy data exchange
  • It is very easy for both humans and machines to read and write
  • It is language-independent 
  • JSON is used in web applications to store and transfer data

SUPPORTED DATA TYPES:

  1. Number
  2. Array
  3. Object (Key-Value pair)
  4. Boolean
  5. String

EXAMPLE OF JSON:

1
2
3
4
5
6
7
#Basic example of JSON
{
    "website_name":"MYBTECHPROJECTS",
    "website_url":"www.mybtechprojects.tech",
    "no_of_posts":30
    "Categories": ["Arduino","Esp8266","python","NodeMcu","Raspberry PI"]
}

JSON PARSING IN PYTHON:

1
2
3
4
5
6
7
8
9
10
11
>>>#Parsing JSON data in Python
>>> import json
>>> j='{"website":"MYBTECHPROJECTS","author":"Gowtham","Category":"Python"}'
>>> mbp=json.loads(j)
>>> mbp["website"]
'MYBTECHPROJECTS'
>>> mbp["author"]
'Gowtham'
>>> mbp["Category"]
'Python'
>>>

CONVERT DICT TO JSON:

1
2
3
4
5
6
7
>>>#Converting Dict to JSON
>>> dict={}
>>> dict={'website':'MYBTECHPROJECTS','Tagline':'Learn what you like'}
>>> j=json.dumps(dict)
>>> print j
{"website": "MYBTECHPROJECTS", "Tagline": "Learn what you like"}
>>>

NUMPY :

NumPy is a library in python programming used for scientific programming. It provides

  1. Support for large N-dimensional array
  2. Sophisticated functions for easy calculations.
  3. Useful for performing algebra and Fourier Transform functions.

PROGRAMS:

Numpy Basics

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> import numpy as np
>>> a=np.array([1,2,3,4,5,6,7,8,9,10])
>>> print a[0]
1
>>> print a[9]
10
>>> print type(a)
<type 'numpy.ndarray'>
>>> print a.dtype
int32
>>> print a.shape
(10L,)
>>>

Create array with pre-filled values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
>>> #To create a array filled with  zeros
>>> a=np.zeros(10)
>>> a
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> a=np.zeros((2,2))
>>> a
array([[0., 0.],
       [0., 0.]])
 
>>>#To create a array filled with ones
>>> a=np.ones((2,2))
>>> a
array([[1., 1.],
       [1., 1.]])
 
>>># To create a Identity matrix
>>> a=np.eye(2)
>>> a
array([[1., 0.],
       [0., 1.]])
>>>
>>> a=np.eye(3)
>>> a
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
 
>>>#To create a array with a constant value
>>> a=np.full((2,2),10)
>>> a
array([[10, 10],
       [10, 10]])
 
>>># To fill a array with random no
>>> r=np.random.random((2,2))
>>> r
array([[0.52221867, 0.21183574],
       [0.07215369, 0.65550072]])
 
>>>
>>> a=np.arange(4)
>>> a
array([0, 1, 2, 3])
>>>

NumPy functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
>>> a=np.array([1,2,3,4,5,6,7,8])
>>> b=np.array([8,7,6,5,4,3,2,1])
>>>
>>>#Multiplication
>>> print np.multiply(a,b)
[ 8 14 18 20 20 18 14  8]
 
>>>#Addition
>>> print np.add(a,b)
[9 9 9 9 9 9 9 9]
 
>>>#Subtraction
>>> print np.subtract(a,b)
[-7 -5 -3 -1  1  3  5  7]
 
>>>#Division
>>> print np.divide(a,b)
[0 0 0 0 1 2 3 8]
 
>>>#Sqare Root
>>> print np.sqrt(a)
[1.         1.41421356 1.73205081 2.         2.23606798 2.44948974
 2.64575131 2.82842712]
 
>>> a=np.array([[1,1],[2,2],[3,3]])
>>> b=np.array([1,1])
>>> y=a+b
>>> y
array([[2, 2],
       [3, 3],
       [4, 4]])
>>> print y.shape
(3L, 2L)
 
>>>#Reshaping
>>> print np.reshape(y,(2,3))
[[2 2 3]
 [3 4 4]]
>>>
THANKYOU
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.