Pract 3 Libs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

In-built Packages in Python

> Matplotlib
In [5]:
# Line plot example

import matplotlib.pyplot as plt


x=[1,2,3,4,5,6]
y=[10,20,30,40,50,60]
plt.plot(x,y)
plt.show()

In [6]:
# Scatter plot example

import matplotlib.pyplot as plt


x=[1,2,3,4,5,6]
y=[10,20,30,40,50,60]
plt.scatter(x,y)
plt.show()
> Pandas
In [17]:
# Series example

import pandas as pd
num = [9, 7, 6, 10, 4]
tbl = pd.Series(num)
print(tbl)

0 9
1 7
2 6
3 10
4 4
dtype: int64

In [15]:
# Dataframe example

import pandas as pd
dset = {'Names': ["Aditya", "Prem", "Harsh", "Sumit"],
'Marks': [81, 79, 80, 79]}
tbl = pd.DataFrame(dset)
print(tbl)

Names Marks
0 Aditya 81
1 Prem 79
2 Harsh 80
3 Sumit 79
> Numpy
In [31]:
# nparray/nd array example

import numpy as np
arr1 = np.array([10,20,30])
print(arr1)
arr2 = np.array([[1,2,3],[4,5,6]])
print(arr2)

# Operations on nd array

print("\n1st Array:-")
print("\nNo. of dimension: ",arr1.ndim)
print("Shape of array: ",arr1.shape)
print("Size of array: ",arr1.size)
print("Type of elements in array: ",arr1.dtype)
print("No. of bytes: ",arr1.nbytes)

print("\n2nd Array:-")
print("\nNo. of dimension: ",arr2.ndim)
print("Shape of array: ",arr2.shape)
print("Size of array: ",arr2.size)
print("Type of elements in array: ",arr2.dtype)
print("No. of bytes: ",arr2.nbytes)

[10 20 30]
[[1 2 3]
[4 5 6]]

1st Array:-

No. of dimension: 1
Shape of array: (3,)
Size of array: 3
Type of elements in array: int32
No. of bytes: 12

2nd Array:-

No. of dimension: 2
Shape of array: (2, 3)
Size of array: 6
Type of elements in array: int32
No. of bytes: 24
> Scipy
In [13]:
# constants

from scipy import constants as const


print("Pi constant: ", const.pi)
print("Gravity constant: ", const.G)
print("Acceleration due to Gravity: ", const.g)
print("Gas constant: ", const.R)
print("Avogadro no.: ", const.Avogadro)
print("Speed of light: ", const.c)
print("Planck's constant: ", const.h)
print("Golden ratio: ", const.golden)

Pi constant: 3.141592653589793
Gravity constant: 6.6743e-11
Acceleration due to Gravity: 9.80665
Gas constant: 8.314462618
Avogadro no.: 6.02214076e+23
Speed of light: 299792458.0
Planck's constant: 6.62607015e-34
Golden ratio: 1.618033988749895

You might also like