Professional Documents
Culture Documents
Practical List7
Practical List7
Practical List7
Practical-1
Aim: Write a program for converting a number from a user given format to other formats
Input:
if base == 2
dec = int(num, 2)
octal = oct(dec)
hexa = hex(dec)
binary = bin(num)
octal = oct(num)
hexa = hex(num)
elif base == 8:
dec = int(num, 8)
binary = bin(dec)
hexa = hex(dec)
binary = bin(dec)
octal = oct(dec)
else:
print(convert_num(num, base))
Output:
Practical-2
Aim: Write a python program to display Pascal’s Triangle using List
Input:
def pascal_triangle(n):
triangle = [[1]]
row = [1]
row.append(triangle[i-1][j-1] + triangle[i-1][j])
row.append(1)
triangle.append(row)
return triangle
# example usage
triangle = pascal_triangle(n)
print(row)
Output:
Practical-3
Aim: Python program for Nested Lists – Given the names and grades for each student in a class
of N students, store them in a nested list and print the name(s) of any student(s) having the
second lowest grade.
Input:
def second_lowest(records):
second_lowest = grades[1]
return students
students = second_lowest(records)
print(student)
Output:
Practical-4
Aim: Write a function named 'format_number' that takes a non-negative number as its only
parameter. Your function should convert the number to a string and add commas as a thousand
separators.
Input:
def format_number(num):
num_str = str(num)
if len(num_str) <= 3:
return num_str
else:
return format_number(num_str[:-3]) + ',' + num_str[-3:]
num = 1000000
formatted_num = format_number(num)
print(formatted_num)
Output:
Practical-5
Aim: Define a function named all_equal that takes a list and checks whether all elements in the
list are the same.
Input:
def all_equal(lst):
lst = [1, 1, 1]
print(all_equal(lst))
Output:
Practical-6
Aim: Write a program in Python – Group Similar items to Dictionary Values List.
Output: {4: [4, 4, 4], 6: [6, 6], 2: [2, 2], 8: [8, 8], 5: [5]}
Input:
def group_items(lst):
groups = {}
if item in groups:
groups[item].append(item)
else:
groups[item] = [item]
return groups
lst = [4, 6, 6, 4, 2, 2, 4, 8, 5, 8]
groups = group_items(lst)
print(groups)
Output:
Practical-7
Aim: Python program to get Kth Column and row of Matrix.
Input:
return matrix[k]
k=1
Output:
Practical-8
Aim: Python Program to take user given elements and order of matrix and display it.
Input:
def create_matrix(order):
matrix = []
for i in range(order):
matrix.append(row)
return matrix
def display_matrix(matrix):
print(*row)
matrix = create_matrix(order)
display_matrix(matrix)
Output:
Practical-9
Aim: Write program for Adding and Subtracting Matrices in Python.
Input:
def display_matrix(matrix):
print(element, end="\t")
print()
result = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] + matrix2[i][j])
result.append(row)
return result
result = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] - matrix2[i][j])
result.append(row)
return result
print("Matrix1:")
display_matrix(matrix1)
print("Matrix2:")
display_matrix(matrix2)
print("Addition result:")
display_matrix(add_matrices(matrix1, matrix2))
print("Subtraction result:")
display_matrix(subtract_matrices(matrix1, matrix2))
Output:
Practical-10
Aim: Write program for Multiplying Matrices in Python
Input:
if len(mat1[0]) != len(mat2):
print("Error: The number of columns in the first matrix should be equal to the number of
rows in the second matrix.")
return
for i in range(len(mat1)):
for j in range(len(mat2[0])):
for k in range(len(mat2)):
return result
if product:
for row in product:
print(row)
Output:
Practical-11
Aim: Write program to Transpose a matrix in Single line in Python.
Input:
def transpose_matrix(mat):
transposed_mat = transpose_matrix(mat)
print(row)
Output: