0% found this document useful (0 votes)
5 views9 pages

Python Basics with NumPy & Matplotlib

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Python Basics with NumPy & Matplotlib

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXP:1 DATE:13/12/23

Name :Vasanth Balaji.V Roll No.:21IR020

AI and Vision Systems Lab

EXP:1 BASICS OF PYTHON AND NUMPY

AIM:
1) Learn the procedure to Install Python, creating a Project in Pycharm and add packages
2) To code six examples for Python Programming
3) To code five Examples for using Numpy and Matplotlib Packages

SOFTWARE/ PACKAGE USED:


1. Pycharm IDE with Python 3.7
2. Libraries used:
a) NumPy
b) Matplotlib

PROCEDURE:

Install Python:
• Visitthe official PyCharm website at [Link]
• Download the appropriate version of PyCharm for your operating system
(Community or Professional).
• Follow the installation instructions provided by the installer.

Create a Project in Pycharm:


• Launch PyCharm and Set Up a New Project
• Inthe "New Project" window, you will be prompted to configure the project
interpreter.
• Choose the Python interpreter you installed earlier. If it's not automatically
detected, you can click on the gear icon and navigate to the Python executable
• Click "Create" to set up the project.

Add Packages:
• Click on the “Python Package” Icon
• Enter the name of Package example Numpy
• Click “Install Package
If Loop:
Program 1: (nested if)
x = 41
if x > 10:
print("Above ten")
if x > 20:
print("and also above 20")
else:
print("but not above 20")
else:
print("Below ten")

Output:
Above ten,
and also above 20

Program 2: (comparision with if )


a = 50
b = 10
if a>b:
print("Hello World")
else:
print("Change values")

Output:
Hello World

For Loop:

Program 1: (printing contents in a list with condition)


f = ["a", "b", "c"]
for x in f:
if x == "b":
continue
print(x)

Output:
a
b
Program 2: (printing values in a range with step value)
for x in range(1, 25, 3):
print(x)

Output:
1
4
7
10
13
16
19
22

While Loop:

Program 1: (while loop)


i=1
while i < 4:
print(i)
i += 1

Output:
1
2
3

Program 2:(while loop with break)


i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
Output:
1
2
3
Functions:
Program 1: (With Argument)

def my_function(*s):
print("The worker name is " + s[2])

my_function("ajith", "vijay", "raj")

Output:
The worker name is raj

Program 2 : (calling function)


def my_function():
print("Hello my friends")

my_function()

Output:
Hello my friends

Program 3 :(without any arguments)


def square():
a=int(input("Enter any no: "))
b=a*a
print("Square = ",b)

square()

Output:
Enter any no: 5
Square = 25

Program 4 :(without any arguments)


def cube():
a=int(input("Enter any no: "))
b=a*a*a
print("Cube = ",b)
cube()
Output:
Enter any no: 5
Cube = 125

NUMPY:
Program 1: (using linspace and one function of numpy)
import numpy as np
x1 = [Link](0, 2, 10, endpoint = False)
y1 = [Link](10)
print(x1)
print(y1)

Output:
[0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Program 2: (using repeat of numpy)


import numpy as np
arr = [Link](5)
print("arr : \n", arr)
repetitions = 2
a = [Link](arr, repetitions)
print("\nRepeating arr 2 times : \n", a)
print("Shape : ", [Link])
repetitions = 3
a = [Link](arr, repetitions)
print("\nRepeating arr 3 times : \n", a)

Output:
arr :
[0 1 2 3 4]

Repeating arr 2 times :


[0 0 1 1 2 2 3 3 4 4]

Repeating arr 3 times :


[0 0 0 1 1 1 2 2 2 3 3 3 4 4 4]
Program 3: (using standard of numpy)
import numpy as np
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("std of arr : ", [Link](arr))
print ("\nMore precision with float32")
print("std of arr : ", [Link](arr, dtype = np.float32))
print ("\nMore accuracy with float64")
print("std of arr : ", [Link](arr, dtype = np.float64))

Output:
arr : [20, 2, 7, 1, 34]
std of arr : 12.576167937809991

More precision with float32


std of arr : 12.576168

More accuracy with float64


std of arr : 12.576167937809991

Program 4: (using percentile of numpy)


import numpy as np
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("50th percentile of arr : ", [Link](arr, 50))
print("25th percentile of arr : ",[Link](arr, 25))
print("75th percentile of arr : ",[Link](arr, 75))

Output:
arr : [20, 2, 7, 1, 34]
50th percentile of arr : 7.0
25th percentile of arr : 2.0
75th percentile of arr : 20.0

Program 5: (using reshape of numpy)


import numpy as np
array1 = [Link](8)
print("Original array : \n", array1)
array2 = [Link](8).reshape(2, 4)
print("\narray reshaped with 2 rows and 4 columns : \n", array2)
array3 = [Link](8).reshape(4, 2)
print("\narray reshaped with 4 rows and 2 columns : \n",array3)
array4 = [Link](8).reshape(2, 2, 2)
print("\nOriginal array reshaped to 3D : \n",array4)

Output:
Original array :
[0 1 2 3 4 5 6 7]

array reshaped with 2 rows and 4 columns :


[[0 1 2 3]
[4 5 6 7]]

array reshaped with 4 rows and 2 columns :


[[0 1]
[2 3]
[4 5]
[6 7]]

Original array reshaped to 3D :


[[[0 1]
[2 3]]
[[4 5]
[6 7]]]

Program 6: (using swapaxes of numpy)


import numpy as np
arr = [Link]([[2, 4, 6]])
gg = [Link](arr, 0, 1)
print (gg)

Output:
[[2]
[4]
[6] ]
MATPLOT :
Program:
import [Link] as plt
year = [1972, 1982, 1992, 2002, 2012]
e_india = [100, 150, 300,400,690]
e_bangladesh = [10, 25, 50,125, 230]
[Link](year, e_india, color ='orange',marker ='o', markersize = 12,label ='India')
[Link](year, e_bangladesh, color ='g',marker ='o', linewidth = 2,label ='Bangladesh')
[Link]('Years')
[Link]('Power consumption in kWh')
[Link]('Electricity consumption per \capita of India and Bangladesh')
[Link]()
[Link]()

Output;
Result:
Thus, the Basics of Python were covered and Basic Coding was done in Pycharm , also libraries like
Numpy and MatplotLib were imported and used in Pycharm.

You might also like