0% found this document useful (0 votes)
2 views3 pages

Python Practicals

The document contains five Python programs: one for counting command line arguments, one for matrix multiplication, one for computing the GCD of two numbers, one for finding the most frequent words in a text file, and one for finding the maximum number in a list. Each program includes user input handling and relevant logic to perform its specific task. The programs demonstrate fundamental programming concepts and operations in Python.

Uploaded by

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

Python Practicals

The document contains five Python programs: one for counting command line arguments, one for matrix multiplication, one for computing the GCD of two numbers, one for finding the most frequent words in a text file, and one for finding the maximum number in a list. Each program includes user input handling and relevant logic to perform its specific task. The programs demonstrate fundamental programming concepts and operations in Python.

Uploaded by

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

1.

To write a python program that takes in command line


arguments as input and print the number of arguments.
# Import sys module to access command line arguments
import sys

# Total number of arguments


num_args = len([Link])

# Print number of arguments


print("Total number of command line arguments:", num_args)

# Print all arguments


print("Arguments are:")
for i in range(num_args):
print(f"Argument {i}: {[Link][i]}")

2. To write a python program to perform Matrix Multiplication


# Matrix Multiplication Program

# Take matrix size input


r1 = int(input("Enter number of rows of first matrix: "))
c1 = int(input("Enter number of columns of first matrix: "))

r2 = int(input("Enter number of rows of second matrix: "))


c2 = int(input("Enter number of columns of second matrix: "))

# Check condition for multiplication


if c1 != r2:
print("Matrix multiplication not possible!")
else:
print("Enter elements of first matrix:")
A = []
for i in range(r1):
row = []
for j in range(c1):
[Link](int(input(f"A[{i}][{j}]: ")))
[Link](row)

print("Enter elements of second matrix:")


B = []
for i in range(r2):
row = []
for j in range(c2):
[Link](int(input(f"B[{i}][{j}]: ")))
[Link](row)

# Initialize result matrix with zeros


result = [[0 for j in range(c2)] for i in range(r1)]

# Matrix multiplication logic


for i in range(r1):
for j in range(c2):
for k in range(c1):
result[i][j] += A[i][k] * B[k][j]

# Print result
print("Resultant Matrix:")
for row in result:
print(row)

3. To write a python program to compute the GCD of two numbers.


# Program to compute GCD using Euclid's Algorithm

# Take input from user


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

# Store original values (optional)


x, y = a, b

# GCD calculation
while b != 0:
a, b = b, a % b

# Result
print(f"GCD of {x} and {y} is: {a}")

4. To write a python program to find the most frequent words in a


text file.

# Program to find most frequent words in a text file

from collections import Counter

# Open and read file


file = open("[Link]", "r")
text = [Link]().lower() # convert to lowercase
[Link]()

# Remove punctuation
for ch in ".,!?;:\"'()[]{}":
text = [Link](ch, "")

# Split into words


words = [Link]()
# Count frequency of each word
word_count = Counter(words)

# Find most common words


n = int(input("How many top frequent words to display? "))

print(f"Top {n} most frequent words are:")


for word, count in word_count.most_common(n):
print(word, ":", count)

[Link] write a python program find the maximum of a list of


numbers.

# Program to find maximum element in a list

# Take input from user


numbers = list(map(int, input("Enter numbers separated by space: ").split()))

# Assume first element is maximum


maximum = numbers[0]

# Loop to find maximum


for num in numbers:
if num > maximum:
maximum = num

# Print result
print("Maximum number in the list is:", maximum)

You might also like