0% found this document useful (0 votes)
54 views7 pages

Python Lab Bhavya

The document describes 7 Python programming experiments involving taking command line arguments, matrix multiplication, GCD calculation, finding most frequent words in a text file, calculating square roots using Newton's method, exponentiation, and finding the maximum of a list.

Uploaded by

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

Python Lab Bhavya

The document describes 7 Python programming experiments involving taking command line arguments, matrix multiplication, GCD calculation, finding most frequent words in a text file, calculating square roots using Newton's method, exponentiation, and finding the maximum of a list.

Uploaded by

bhavya12mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT-1

Write a python program that takes in command line arguments as


input and print the number of arguments.

Code:

import sys

# Get the command line arguments (excluding the


script name)
arguments = sys.argv[1:]

# Print the number of arguments


print("Number of arguments: “,len(arguments))

Output:

Bhavya Mittal (2100910100058)


EXPERIMENT-2

Write a python program to perform matrix multiplication.

Code:
matrix1 = [[1, 2, 3], [4, 5, 6]]
matrix2 = [[7, 8], [9, 10], [11, 12]]

# Perform matrix multiplication


result_rows = len(matrix1)
result_cols = len(matrix2[0])
result = []
for i in range(result_rows):
result.append([0]*result_cols)

for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]

# Print the result


print("Matrix 1:")
for row in matrix1:
print(row)

print("Matrix 2:")
for row in matrix2:
print(row)

print("Result:")
for row in result:
print(row)

Output:

Bhavya Mittal (2100910100058)


EXPERIMENT-3

Write a python program to compute GCD of two numbers.

Code:

num1 = 36
num2 = 48

# Compute GCD
gcd = 1

for i in range(1, min(num1, num2) + 1):


if num1 % i == 0 and num2 % i == 0:
gcd = i

# Print the result


print(f"The GCD of “,num1,” and “,num2,” is: “,gcd)

Output:

Bhavya Mittal (2100910100058)


EXPERIMENT-4

Write a python program to find more frequent word in a text file.

Code:

file_path = input("Enter the text file to read : ")

word_count = {}

with open(file_path, 'r') as file:


for line in file:
words = line.strip().split()
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1

most_frequent_word = None
max_count = 0

for word, count in word_count.items():


if count > max_count:
most_frequent_word = word
max_count = count

# Print the result


print("Most frequent word:", most_frequent_word)

Output:

Bhavya Mittal (2100910100058)


EXPERIMENT-5

Write a python program to find square root of a number (using


Newton’s Method)

Code:

number = int(input("Enter a number : "))

# Find the square root


result_sqrt = number / 2 # Initial guess
precision = 0.001
while abs(result_sqrt**2 - number) > precision:
result_sqrt = (result_sqrt + number / result_sqrt) /
2

# Print the result


print("Square root of", number, "is:", result_sqrt)

Output:

Bhavya Mittal (2100910100058)


EXPERIMENT-6

Write a python program for exponentiation (power of a number).

Code:

a = int(input("Enter the value of A : "))


n = int(input("Enter the value of N : "))
r = a**n
print(a," to power ", n, "equals to ", r)

Output:

Bhavya Mittal (2100910100058)


EXPERIMENT-7

Write a python program to find the maximum of a list of numbers.

Code:

user_list = eval(input("Enter the list of numbers : "))


max_number = 0
for i in user_list:
if i > max_number:
max_number = i

print("Maximum number : ", max_number)

Output:

Bhavya Mittal (2100910100058)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy