Lab Report 1
Lab Report 1
Lab Report 1
ENGINEERING
SUBMITTED TO:
LE Ma’am Sundas Ashraf
SUBMITTED BY:
Uzair Waheed
Reg # 371828
DE- 43 CE
Submission Date:18/02/24
1|Page
Lab-01: Introduction to Python & Python Package
Lab Tasks
Task-1:
Write a function Sort_tuple() that sort a tuple of tuples by its 2nd item and return
tuple.
Input: tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Output: (('c', 11), ('a', 23), ('d', 29), ('b', 37)).
Code:
import numpy as np
import matplotlib as plt
import cv2
def Sort_tuple(tuple_1):
tuple_1 = list(tuple_1)
for i in range(len(tuple_1)-1):
for j in range(len(tuple_1)-1):
if tuple_1[i][1] > tuple_1[j+1][1]:
temp = tuple_1[j]
tuple_1[j] = tuple_1[j+1]
tuple_1[j+1]= temp
return tuple(tuple_1)
Output:
2|Page
Task-2:
Write a function to get the key of a minimum value from the dictionary data holder:
Input: Dict = {'Physics': 82, 'Math': 65,'history': 75}
Output: Math
Code:
return Dict[0][0]
Output:
Task-3:
Write a program that takes an integer as an input from the user and represents it in
binary, octal or hexadecimal. The user should specify the output number system i.e.
whether the number should be converted to binary, octal or hexadecimal.
Code:
3|Page
def represent(num,rep):
rep=rep.upper()
if(rep=='B'):
return bin(num)
elif(rep == 'O'):
return oct(num)
elif(rep=='H'):
return hex(num)
Output:
Task-4:
Min-Max normalization is one of the normalization techniques that can be used for
numerical data. It is performed using the following operation: Write a function
min_max_normlaization that takes a list as input and performs the min-max
normalization on it. The length of the list can vary.
Code:
import numpy as np
def normalization(l):
min_val = min(l)
max_val = max(l)
newList = np.array(l)
return (newList - min_val) / (max_val-min_val)
list_1 = [312,41,55,11,5,142,56,1,9]
print("List : ",list_1)
print("After Normalization: \n", normalization(list_1))
4|Page
Output:
Task-5:
Write a Python function that takes a 2D array of size 6*6 representing temperature
readings should be in range of (1 to 50) across the region and returns the average
temperature for each non-overlapping 2x2 grid within the region.
Code:
import numpy as np
matrix = np.random.randint(0,51,(6,6),dtype=int)
list_1.append((matrix[i][j]+matrix[i][j+1]+matrix[i+1][j]+matrix[i+1][j+1])/4
)
Output:
5|Page
6|Page