Practical List7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

PRACTICAL LIST-7

Practical-1
Aim: Write a program for converting a number from a user given format to other formats

Input:

def convert_num(num, base):

if base == 2

dec = int(num, 2)

octal = oct(dec)

hexa = hex(dec)

return (dec, octal, hexa)

elif base == 10:

binary = bin(num)

octal = oct(num)

hexa = hex(num)

return (binary, octal, hexa)

elif base == 8:

dec = int(num, 8)

binary = bin(dec)

hexa = hex(dec)

return (dec, binary, hexa)

elif base == 16:

dec = int(num, 16)

binary = bin(dec)
octal = oct(dec)

return (dec, binary, octal)

else:

return "Invalid input"

num = input("Enter a number: ")

base = int(input("Enter its base (2, 10, 8, 16): "))

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]]

for i in range(1, n):

row = [1]

for j in range(1, i):

row.append(triangle[i-1][j-1] + triangle[i-1][j])

row.append(1)
triangle.append(row)

return triangle

# example usage

n = int(input("Enter the number of rows: "))

triangle = pascal_triangle(n)

for row in triangle:

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):

grades = sorted(set([r[1] for r in records]))

second_lowest = grades[1]

students = [r[0] for r in records if r[1] == second_lowest]


students.sort()

return students

records = [["chi", 20.0], ["beta", 50.0], ["alpha", 50.0]]

students = second_lowest(records)

for student in students:

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):

return all(elem == lst[0] for elem in 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.

Input: test_list = [4, 6, 6, 4, 2, 2, 4, 8, 5, 8]

Output: {4: [4, 4, 4], 6: [6, 6], 2: [2, 2], 8: [8, 8], 5: [5]}

Input:

def group_items(lst):

groups = {}

for item in lst:

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:

def get_kth_row(matrix, k):

return matrix[k]

def get_kth_column(matrix, k):

return [row[k] for row in matrix]

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

k=1

print("Kth row:", get_kth_row(matrix, k))

print("Kth column:", get_kth_column(matrix, k))

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):

row = list(map(int, input().split()))

matrix.append(row)

return matrix

def display_matrix(matrix):

for row in matrix:

print(*row)

order = int(input("Enter the order of matrix: "))

print("Enter the elements of matrix:")

matrix = create_matrix(order)

print("The matrix is:")

display_matrix(matrix)

Output:
Practical-9
Aim: Write program for Adding and Subtracting Matrices in Python.

Input:

def display_matrix(matrix):

for row in matrix:

for element in row:

print(element, end="\t")

print()

def add_matrices(matrix1, matrix2):

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

def subtract_matrices(matrix1, matrix2):

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

matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

matrix2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

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:

def matrix_multiplication(mat1, mat2):

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

result = [[0 for _ in range(len(mat2[0]))] for _ in range(len(mat1))]

for i in range(len(mat1)):

for j in range(len(mat2[0])):

for k in range(len(mat2)):

result[i][j] += mat1[i][k] * mat2[k][j]

return result

mat1 = [[1, 2], [3, 4], [5, 6]]

mat2 = [[7, 8, 9], [10, 11, 12]]

product = matrix_multiplication(mat1, mat2)

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):

return [[mat[j][i] for j in range(len(mat))] for i in range(len(mat[0]))]

mat = [[1, 2], [3, 4], [5, 6]]

transposed_mat = transpose_matrix(mat)

for row in transposed_mat:

print(row)

Output:

You might also like