0% found this document useful (0 votes)
11 views

Exp.1

The document outlines the procedure for installing Python and setting up Visual Studio Code, including the installation of NumPy and Matplotlib libraries. It provides coding examples for both Python and the mentioned libraries, demonstrating various functionalities such as array creation, operations, and plotting. The experiment concludes with successful implementation and output verification of the basic functions of the libraries.

Uploaded by

harinythangavelu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Exp.1

The document outlines the procedure for installing Python and setting up Visual Studio Code, including the installation of NumPy and Matplotlib libraries. It provides coding examples for both Python and the mentioned libraries, demonstrating various functionalities such as array creation, operations, and plotting. The experiment concludes with successful implementation and output verification of the basic functions of the libraries.

Uploaded by

harinythangavelu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT – 1 22IR005

BASICS OF PYTHON AND NUMPY


Aim:
• Learn the procedure to Install Python, creating a Project in Visual studio code and
add packages
• To code six examples for Python Programming
• To code five Examples for using Numpy and Matplotlib Packages

Software/ Package Used:


1. Visual Studio Code
2. Libraries used:
a) NumPy
b) Matplotlib

Procedure:
1) Download the installer for Visual Studio Code from https://code.visualstudio.com
2) Run the installer and setup the software.
3) Install the Python extension provided by Microsoft.
4) Likewise, download the latest Python installer from
https://www.python.org/downloads/
5) Run the installer and select ‘Add Python to PATH', before proceeding with the
installation.
6) Once Python is installed, the other library packages can be installed through the
command line using pip.
a. Open command prompt, and type ‘pip install numpy’ and press enter.
b. Once the installation is complete, use ‘pip install matplotlib’ and press
enter.
7) Create a folder for the python files in Visual Studio Code.
8) Under this folder, python files can be created, for example ‘array.py.’
9) Once created, the code can be typed as required into the file.
10) To execute, press F5 and observe the output. Rectify the errors, if there are any
and repeat until the output is obtained.
Programs:
1. Numpy:
import numpy as np
arr = np.array([1,2,3,4])
print(arr)
print(type(arr))

Output:
[1 2 3 4]

2. import numpy as np
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([6,7,8,9,10])
arr = np.dot(arr1,arr2)
print(arr)

Output:
130

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

Output :
[2 3 4 5]

4. #2D array
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print(arr)
#3D array
import numpy as np
arr = np.array([[[5,2,3],[7,5,6]],[[8,2,3],[8,5,6]]])
print(arr)
array_1 = np.array([1,2,3,4])
array_2 = np.array([5,6,7,8])

Output:
[[1 2 3]
[4 5 6]]
[[[5 2 3]
[7 5 6]]

[[8 2 3]
[8 5 6]]]

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

Output:
[1 2 3 4 5 6]

6. import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9,10])
newarr = np.array_split(arr,4)
print(newarr)

import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9,10])
newarr = np.array_split(arr,4)
print(newarr[0])
print(newarr[1])
print(newarr[2])
print(newarr[3])

Output:
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8]), array([ 9, 10])]
[1 2 3]
[4 5 6]
[7 8]
[ 9 10]

7. import numpy as np
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([6,7,8,9,10])
arr = np.hstack((arr1,arr2))
print(arr)

import numpy as np
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([6,7,8,9,10])
arr = np.vstack((arr1,arr2))
print(arr)

Output:
[ 1 2 3 4 5 6 7 8 9 10]
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]

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

Output :
32
Matplotlib:

1. import matplotlib.pyplot as plt


import numpy as np
xpoints = np.array([0,6])
ypoints = np.array([0,250])
plt.plot(xpoints,ypoints)
plt.show()

Output:
2. import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0,25])
ypoints = np.array([0,250])
plt.plot(xpoints,ypoints,"o")
plt.show()

Output:
3. import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,1,3,5])
ypoints = np.array([6,1,3,1])
plt.plot(xpoints,ypoints)
plt.show()

Output:
4. import matplotlib.pyplot as plt
import numpy as np

#plot 1
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y)

#plot 2
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()

Output :
5. import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(150,15,250)
plt.hist(x)
plt.show()

Output:
Result:
Thus, the required packages and software has been installed and set up,
as well as the basic functions of numpy and matplotlib library have been
implemented and the outputs have been obtained.

You might also like