PYTHON - Record LAB WORD
PYTHON - Record LAB WORD
2. Update Package Lists: It’s a good practice to update your system’s package
lists before installing new software. Run the following command:
sudo apt update
3. Install Python:
For Python 3: Most modern Linux distributions come with Python 3 preinstalled.
To install latest version of Python 3, use the following command:
sudo apt install python 3
4. Verify Installation: After the installation is complete, you can verify it by
checking the installed version. Run the following command:
python3 –version
1|P a ge
Pranjal | 23011P0430
6. Verify pip Installation: After installing pip ,you can verify it by checking the
version. Run the following command:
pip3 –version
That’s it! You’ve successfully installed Python on your Linux system. Keep in mind
that the exact commands might vary slightly depending on your Linux
distributions.
Result: Hence downloaded and installed latest version of python on Linux.
2|P a ge
Pranjal | 23011P0430
b) Python 3 on windows
Aim : To download and install latest version of python on Windows.
Software Required :
1. Jupyter Notebook
2. Latest version of Python
Procedure :
Installing Python 3 on Windows is a straightforward process. Here are the steps:
1. Download Python Installer:
Visit official Python website https//www.python.org/downloads/windows/ and
download the latest version of Python 3 for Windows. Make sure to download the
executable installer that matches your system architecture Usually 32-bit or 6-
bit).
2. Run the Installer:
Once the installer executable is downloaded ,double-click o it to run it. You might
see a security warning; click “Run” or “Yes” to proceed.
3. Installation Options:
- If you choose “Install Now” the installer will proceed to install Python with
default settings. You can skip the next step.
3|P a ge
- If you choose “Customize Installation” you’ll be presented with a list of features.
Ensure that “Add Python to PATH” is checked. This allows you to use Python from
the command prompt without specifying its full path.
4|P a ge
Pranjal | 23011P0430
5|P a ge
Pranjal | 23011P0430
6|P a ge
Pranjal | 23011P0430
3. Install SciPy:
Run the following command to install the SciPy library:
pip3 install scipy
This command will download and install the SciPy library and its dependencies.
4. *Verify Installation*:
You can verify the installation of NumPy and SciPy by opening a Python
interpreter and importing the libraries:
7|P a ge
- Start a Python interpreter by typing 'python' (or 'python3' on Linux) in the
terminal and pressing Enter.
- Import NumPy and SciPy by typing the following lines one by one and pressing
enter:
python
import numpy
import scipy
If there are no errors, the libraries are successfully installed.
8|P a ge
e) Installing Jupyter Lab:
Aim : To download and install latest version of python on Windows.
Software Required :
1. Jupyter Notebook
2. Latest version of Python
Procedure:
You can install JupyterLab using the Command Prompt in Windows by utilizing the
'pip package manager. Jupyterlab is a powerful interactive development
environment for Jupyter notebooks. Here's how you can install it:
1. Open Command Prompt:
-Press 'Win+ R', type 'cmd', and press Enter to open the Command Prompt.
2. Install JupyterLab:
Run the following command to install JupyterLab using pip":
pip install jupyterlab
This command will download and install JupyterLab and its dependencies.
3. Verify Installation:
After the installation is complete, you can verify it by running the following
command in the Command Prompt:
Jupyter lab --version
This should display the version number of the installed JupyterLab.
4. Start JupyterLab:
Jupyter lab
To start Jupyterlab, run the following command in the Command Prompt:
Result: Hence the JupyterLab is installed performed.
9|P a ge
Pranjal | 23011P0430
10 | P a g e
Pranjal | 23011P0430
11 | P a g e
Pranjal | 23011P0430
c) Finding all the factors of a number and show whether it is a perfect number,
i.e., the sum
of all its factors (excluding the number itself) is equal to the number itself.
Aim: To print factors of a number and checking whether the number if perfect or
not
Software Required:
1. Jupyter notebook
2. Latest version of Python
Program:
num=int(input("enter the given number"))
sum=0
for i in range (1,num+1):
if (num%i==0):
print(i)
if(i!=num):
sum+=i
if sum==num:
print(num,'is perfect')
else:
print(num,'is not perfect')
Output:
Enter the given number 6
The factors of 6 are
1
2
3
Sum of factors of 6 is 6
Entered number 6 is a perfect number
12 | P a g e
Pranjal | 23011P0430
Result: Hence the factors of a given number have been printed and it is found out
whether the number is a perfect number or not.
13 | P a g e
Pranjal | 23011P0430
#function call
read_write()
Output:
This is the first function program on python GNU lab
Result: Hence, the file is read and displayed on the screen
14 | P a g e
Pranjal | 23011P0430
##call function
word=input('enter the word : ')
if is_palindrome(word):
print(word,'is palindrome')
else:
print(word,'is not palindrome')
Output:
Enter the word : Mahi
Mahi is not palindrome
Result: Hence it is verified with the string is palindrome or not.
15 | P a g e
Pranjal | 23011P0430
#call function
num=int(input('Enter a positive number'))
steps=1
while num!=1:
num=collatz(num)
steps +=1
16 | P a g e
Pranjal | 23011P0430
m=float(input('Enter m value'))
s=float(input('Enter s value, s should be less than m'))
nor_distr(m,s)
Output:
Enter m value 50
Enter s value, s should be less than m 10
17 | P a g e
Pranjal | 23011P0430
18 | P a g e
Pranjal | 23011P0430
matrix = np.random.randint(1,1000,size=(row,col))
print(matrix)
Output:
Enter the number of rows: 4
Enter the number of Columns: 5
A matrix of order 4*5 containing random numbers from 1 to 99999 is :
[[ 70 590 614 635 301]
[243 30 974 628 465]
[482 179 9 631 801]
[697 576 566 424 96]]
Result: Hence matrix is created.
19 | P a g e
Pranjal | 23011P0430
b) Write a program that adds, subtracts and multiplies two matrices. Provide an
interface such that, based on the prompt, the function (addition, subtraction or
multiplication) should be performed
Aim: To write a program to perform matrix addition, subtraction or multiplication
based on prompt.
Software Required:
1. Jupyter Notebook
2. Latest version of Python
Program:
import numpy as np
def operator(n,m1,m2):
if n==1:
result=m1+m2
elif n==2:
result=m1-m2
elif n==3:
result=np.dot(m1,m2)
elif n==4:
result =np.dot(m1,np.linalg.inv(m2))
else:
print("invalid operator")
print("result\n")
print(result)
print(""" \t\t\t MENU
1) Add
2)sub
3) mul
4) div
""")
20 | P a g e
Pranjal | 23011P0430
matrix1=np.random.randint(1,200,size=(row1,col1))
print(matrix1)
row2=int(input('enter no.of rows in matrix 2'))
col2=int(input('enter no.of colums in matrix 2'))
matrix2=np.random.randint(1,200,size=(row2,col2))
print(matrix2)
if (row1,col1)==(row2,col2):
operator(n,matrix1,matrix2)
else:
print("operation is not possible")
Output:
MENU
1) Add
2)sub
3) mul
4) div
22 | P a g e
c) Write a program to solve a system of n linear equations in n variables using
matrix inverse.
Aim: To write a program to solve system of n linear equations in n variables.
Software Required:
1. Jupyter Notebook
2. Latest version of Python
Program:
import numpy as np
order=int(input('enter order of matrix'))
matrix1=list(map(int,input('enter elements of A seperated by comma').split(',')))
matrix_A =np.array(matrix1).reshape(order,order)
print('matrix A =\n',matrix_A)
matrix2=list(map(int,input('enter elements of B seperated by comma').split(',')))
matrix_B =np.array(matrix2).reshape(order,1)
print('matrix B =\n',matrix_B)
A_inverse = np.linalg.inv(matrix_A)
X=A_inverse.dot(matrix_B)
print('the solution is X =\n',X)
Output:
enter order of matrix 2
enter elements of A seperated by comma 4,5,6,2
matrix A =
[[4 5]
[6 2]]
enter elements of B seperated by comma 5,6
matrix B =
[[5]
[6]]
23 | P a g e
the solution is X =
[[0.90909091]
[0.27272727]]
Result: Hence a system of n linear equations in n variables is solved using matrix
inversion method.
24 | P a g e
Pranjal | 23011P0430
25 | P a g e
Pranjal | 23011P0430
Result: Hence the mean value of two sets of data are found and compared.
26 | P a g e
b) Plotting data read from a file.
Aim : To write a program to plot data read from a file.
Software Required :
1. Jupyter Notebook
2. Latest verion of Python
Program :
import pandas as pd
from matplotlib import pyplot as plt
# filename=input("Enter filename with extension :")
df=pd.read_csv("pytho.csv")
print(df)
figure=plt.figure(figsize=(10,5))
x=df['item']
y=df['unit']
plt.bar(x,y,width=0.4)
plt.xlabel('item')
plt.ylabel('unit')
plt.title('plotting a bar graph')
plt.show()
Output :
item unit
0 apple 300
1 cherry 490
2 banana 250
27 | P a g e
YASHONANDAN | 23011MB608
28 | P a g e
YASHONANDAN | 23011MB608
29 | P a g e
YASHONANADAN | 23011MB608
30 | P a g e
YASHONANDAN | 23011MB608
31 | P a g e
YASHONANDAN | 23011MB608
b) Read text from a file and return a list of all n letter words beginning with a
vowel.
Aim : To write a program to return a list of all n letter words beginning with a
vowel.
Software Required :
1. Jupyter Notebook
2. Latest version of Python
Program :
filename = input("Enter the file name with extension")
file = open(filename, "r")
vowels= ('A','E','I','O','U','a','e','i','o','u')
matched_words = []
for line in file :
for word in line.split():
if word[0] in vowels:
matched_words.append(word)
print(matched_words)
file.close()
Output :
Enter the file name with extension ece.txt
['is', 'i', 'am', 'engineering']
Result : Hence list of all n letter words starting with a vowel are returned.
32 | P a g e
d) Plot a histogram of words according to their length from text read from a file.
Aim : To plot Normal distribution curve.
Software Required :
1. Jupyter Notebook
2. Latest version of Python
Program :
import matplotlib.pyplot as plt
filename=input('enter filename with extension')
words=[]
words_lengths=[]
with open(filename,"r") as file :
for line in file:
words.extend(line.split())
for word in words:
words_lengths.append(len(word))
print("the list of lengths of words read from file:",words_lengths)
figure=plt.figure(figsize=(10,5))
plt.hist(words_lengths)
plt.title('histogram of words length')
plt.xlabel('length of words')
plt.ylabel('number of words')
plt.xticks(ticks=range(max(words_lengths)+1))
plt.show()
Output :
enter filename with extension ece.txt
the list of lengths of words read from file: [2, 4, 2, 12, 1, 2, 8, 11, 5]
33 | P a g e
YASHONANDAN | 23011MB608
34 | P a g e
Pranjal | 23011P0430
35 | P a g e
Pranjal | 23011P0430
5. Now insert the SD card in the SD card slot of the raspberry pi.
6. Connect the HDMI cable to the monitor and the Raspberry pi.
7. Connect the keyboard, mouse, and the Ethernet cables.
36 | P a g e
Pranjal | 23011P0430
37 | P a g e
Pranjal | 23011P0430
38 | P a g e
Pranjal | 23011P0430
39 | P a g e
Pranjal | 23011P0430
40 | P a g e
Pranjal | 23011P0430
41 | P a g e
EXPERIMENT 9 : Collecting Sensor Data
a) DHT Sensor interface
Aim : To perform an experiment on functioning of DHT Sensor Data.
Software Required :
1. Jupyter Notebook
2. Latest version of Python
Program :
import Adafruit_DHT
import time
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4
while True:
humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
print("Temp={0:0.1f}C Humidity={1:0.1f}%".format(temperature, humidity))
else:
print("Sensor failure. Check wiring.");
time.sleep(2);
Output :
42 | P a g e
Pranjal | 23011P0430
43 | P a g e