Professional Documents
Culture Documents
Page No: 1
Aim:
Write a python program that takes command line arguments as input and print the number of arguments.
Source Code:
ID: 2100330130004
arguments.py
import sys
print('The number of arguments is:',len(sys.argv)-1 )
User Output
2021-2025-IT-A
The number of arguments is: 2
Test Case - 2
User Output
Aim:
Page No: 2
Write a Python program to perform multiplication of two matrices.
Source Code:
ID: 2100330130004
matrixMultiply.py
print("Enter values for matrix - A")
m = int(input("Number of rows, m = "))
n = int(input("Number of columns, n = "))
array1=[[0 for j in range (0,n)] for i in range (0,m)]
for i in range(0,m):
for j in range (0,n):
print("Entry in row:", i+1, "column:",j+1)
array1[i][j]=int(input())
print("Enter values for matrix - B")
p=int(input("Number of rows, m = "))
q=int(input("Number of columns, n = "))
2021-2025-IT-A
array2=[[0 for j in range (0,q)] for i in range(0,p)]
for i in range(0,p):
for j in range(0,q):
print("Entry in row:",i+1,"column:",j+1)
array2[i][j] = int(input())
result=[[0 for j in range(0,q)]for i in range(0,m)]
User Output
Enter values for matrix - A
Number of rows, m =
2
Number of columns, n =
2
Entry in row: 1 column: 1
1
2
Entry in row: 2 column: 1
3
Entry in row: 2 column: 2
4
Page No: 3
Enter values for matrix - B
Number of rows, m =
2
Number of columns, n =
ID: 2100330130004
2
Entry in row: 1 column: 1
1
Entry in row: 1 column: 2
2
Entry in row: 2 column: 1
3
Entry in row: 2 column: 2
4
Matrix - A = [[1, 2], [3, 4]]
2021-2025-IT-A
Matrix - B = [[1, 2], [3, 4]]
Matrix - A * Matrix- B = [[7, 10], [15, 22]]
Test Case - 2
User Output
Page No: 4
Entry in row: 2 column: 2
5
Entry in row: 2 column: 3
6
ID: 2100330130004
Matrix - A = [[1, 2, 3], [4, 5, 6]]
Matrix - B = [[1, 2, 3], [4, 5, 6]]
Cannot multiply the two matrices. Incorrect dimensions.
Matrix - A * Matrix- B = None
Test Case - 3
User Output
Enter values for matrix - A
Number of rows, m =
2021-2025-IT-A
3
Number of columns, n =
3
Entry in row: 1 column: 1
1
Entry in row: 1 column: 2
Page No: 5
Entry in row: 3 column: 2
2
Matrix - A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Matrix - B = [[5, 7], [9, 6], [3, 2]]
ID: 2100330130004
Matrix - A * Matrix- B = [[32, 25], [83, 70], [134, 115]]
2021-2025-IT-A
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a program to find the GCD of two
S.No: 3 Date: 2023-04-19
given numbers
Aim:
Page No: 6
Write a program to find the GCD of two numbers x and y , print the result to the console.
Constraints:
• If one of the inputs is zero print the message to the console as shown in the Sample Test Cases.
ID: 2100330130004
Source Code:
gcd.py
2021-2025-IT-A
a = int(input("Please enter an integer: "))
b = int(input("Please enter another integer: "))
if a==0 or b==0:
print("Numbers must be non zero")
User Output
Please enter an integer:
20
Please enter another integer:
40
The GCD of 20 and 40 is 20
Test Case - 2
User Output
Please enter an integer:
0
Please enter another integer:
23
Numbers must be non zero
Test Case - 3
User Output
Please enter an integer:
89
Page No: 7
Please enter another integer:
2
The GCD of 89 and 2 is 1
ID: 2100330130004
2021-2025-IT-A
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a python program to find the most
S.No: 4 Date: 2023-04-18
frequent words in a text file
Aim:
Page No: 8
Write a python program to find the most frequent words in a text file
The name of the files are
1. out.txt
2. out1.txt
3. out2.txt
ID: 2100330130004
Source Code:
wordFrequency.py
count = 0;
word = "";
maxcount = 0;
words =[];
filename=input("Enter the file name: ")
file = open(filename,"r")
for line in file:
string = line.lower().replace(',','').replace(',','').split(" ");
2021-2025-IT-A
for s in string:
words.append(s);
for i in range(0,len(words)):
count = 1;
for j in range(i+1,len(words)):
if(words[i]==words[j]):
count=count+1;
out.txt
out1.txt
Fifty years ago this January, Mahatma Gandhi was shot down in a prayer garden in New Delhi.
He was seventy-nine years old, and had lived to see India win independence from Britain. His
leadership of India's masses reverberated on the world stage, not least in the United
States, and changed profoundly how protesters dealt with those in power. How well his
adaptors in the West understood Gandhi's message, or how faithfully they adopted his
Page No: 9
philosophy, is a matter still being debated by historians. Revolutionary ideologies cross
the world in steerage or even as stowaways, passing from place to place unheeded by those in
power. Only afterward, when the actions and the actors are forgotten, can they come to
appear as an inevitable and obvious flow of the culture.
In the 1950s Gandhi's legacy passed to the Rev. Martin Luther King, Jr., who took up the
ID: 2100330130004
philosophy of nonviolence in his long march for civil rights. With King came the Congress on
Racial Equality and other civil rights groups, then the Berrigans, Dellinger, and anti-
Vietnam activists; Cesar Chavez and his farm workers; Mitch Snyder and the homeless; and the
rights advocates for ecology, animals, reproduction, and gays today. However, what happens
if we look at a time before King, and if we set aside the after-the-fact certainty that
Gandhian nonviolence was inevitable? The methodology is one that Clifford Geertz calls
"doing history backwards."
Gandhi and his methods were easily misinterpreted by Westerners. In order to fathom what he
was about, westerners fluctuated between hyper-difference, in which Gandhi was seen as the
inexplicable product of a foreign culture; and over-likeness, in which they found
similarities that were not really there. The real Gandhi lay somewhere in between. Gandhi
departed from Hindu orthodoxy in two significant ways: on nonviolence and on caste. Ahimsa,
2021-2025-IT-A
or nonviolence, maintained that all killing should be avoided to accrue spiritual merit.
Gandhi, who had encounters with poisonous snakes in South Africa and rabid dogs in India,
redefined the concept and mandated killing for humanitarian purposes, as in the euthanasia
of rabid dogs. If some Hindus were alienated by his lack of orthodoxy on ahimsa, many more
fell out with him over his championing of the untouchables, the lowest of India's castes. In
traditional Hindu belief, an untouchable's contact with the person, food, or drink of a
member of a higher caste would defile that person. For orthodox Hindus, it was a
out2.txt
When she went to out she got her friend her friend got their friend annd their friend went
out suddenly hi hello ji she is she is good good is better to say than hi hi hello hello
User Output
Enter the file name:
out.txt
Most repeated word in the given text file is : hi
Exp. Name: Write a python program to find the square
S.No: 5 Date: 2023-04-18
root of a number by Newton’s method
Aim:
Page No: 10
Write a python program to find the square root of a number by Newton’s method
Source Code:
newtonSquareRoot.py
ID: 2100330130004
def newton_method(number, end = 500):
a=float(number)
for i in range(end):
number = 0.5 * (number +a /number)
return number
a=int(input("Enter a number to find its square root:"))
print("The square root of the number is:",newton_method(a))
2021-2025-IT-A
Test Case - 1
User Output
Enter a number to find its square root:
9
The square root of the number is: 3.0
User Output
Enter a number to find its square root:
8
The square root of the number is: 2.82842712474619
Test Case - 3
User Output
Enter a number to find its square root:
216
The square root of the number is: 14.696938456699069
S.No: 6 Exp. Name: Program to find exponential of a number. Date: 2023-04-18
Aim:
Write a python program to calculate exponentiation (power) of a number.
Page No: 11
Constraints:
• Range of input number: [-1000, 1000].
• Maximum value of exponentiation should not exceed 10.
Source Code:
ID: 2100330130004
exponent.py
2021-2025-IT-A
User Output
Enter any integer:
5
Enter the exponential:
2
Test Case - 2
User Output
Enter any integer:
2
Enter the exponential:
0
The result of 2 to the power of 0 is: 1
Test Case - 3
User Output
Enter any integer:
1000
Enter the exponential:
5
The result of 1000 to the power of 5 is: 1000000000000000
Test Case - 4
Enter any integer:
-25
Enter the exponential:
5
The result of -25 to the power of 5 is: -9765625
Page No: 12
Test Case - 5
User Output
ID: 2100330130004
Enter any integer:
0
Enter the exponential:
1
The result of 0 to the power of 1 is: 0
2021-2025-IT-A
Raj Kumar Goel Institute Of Technology
S.No: 7 Exp. Name: Program to find maximum number in a list Date: 2023-04-18
Aim:
Write a python program find the maximum of a list of numbers.
Page No: 13
Source Code:
max.py
a=list(map(int,input("Enter numbers separated by a comma: ").split(",")))
ID: 2100330130004
print(f"The largest number in the input list is {max(a)}")
User Output
Enter numbers separated by a comma:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2021-2025-IT-A
The largest number in the input list is 20
Test Case - 2
User Output
Enter numbers separated by a comma:
Test Case - 3
User Output
Enter numbers separated by a comma:
0,-1,-3,-5,-7,-9,-111
The largest number in the input list is 0
Test Case - 4
User Output
Enter numbers separated by a comma:
-111,-1111,-11111,-11,-1
The largest number in the input list is -1
Exp. Name: Write a program to find the given element is
S.No: 8 Date: 2023-04-18
present in the input list or not by using linear search
Aim:
Page No: 14
Write a python program to find the given element is present in the input list or not by using linear search .
Source Code:
linearSearch.py
ID: 2100330130004
x=input("Enter the list of numbers: ")
list= x.split(" ")
s = int(input("Enter the number to be search: "))
found=False
for el in list:
if int(el)==s:
print(f"Element {s} was found at index {list.index(el)}.")
found=True
break
if not found:
print(f"Element {s} was not found.")
2021-2025-IT-A
Execution Results - All test cases have succeeded!
Test Case - 1
Test Case - 2
User Output
Enter the list of numbers:
0 1 2 52 85 0 8
Enter the number to be search:
0
Element 0 was found at index 0.
Test Case - 3
User Output
Enter the list of numbers:
25 40 20 10 80
Enter the number to be search:
100
Exp. Name: Write a program to find the given element is
S.No: 9 present in the sorted input list or not by using binary Date: 2023-04-18
search
Page No: 15
Aim:
Write a python program to find the given element is present in the input list or not by using binary search .
Source Code:
binarySearch.py
ID: 2100330130004
n = int(input("Enter size of list: "))
list = []
for i in range(n):
x=int(input("Enter your number: "))
list.append(x)
list.sort()
print("After sorting list is: ",list)
s=int(input("The number to search for: "))
l=0
2021-2025-IT-A
r=n
found=False
while(l<r):
mid = (l+r)//2
if list[mid]==s:
print(f"{s} was found at index {mid}.")
if not found:
print(f"{s} was not found.")
User Output
Enter size of list:
4
Enter your number:
10
Enter your number:
20
Enter your number:
78
5
After sorting list is: [5, 10, 20, 78]
The number to search for:
20
20 was found at index 2.
Page No: 16
Test Case - 2
User Output
ID: 2100330130004
Enter size of list:
3
Enter your number:
12
Enter your number:
24
Enter your number:
36
After sorting list is: [12, 24, 36]
The number to search for:
2021-2025-IT-A
36
36 was found at index 2.
Test Case - 3
User Output
Aim:
Page No: 17
Write a python program to convert the given list of numbers in ascending order using selection sort .
Source Code:
selectionSort.py
ID: 2100330130004
def Selection_Sort(array):
for i in range(0, len(array) -1):
smallest = i
for j in range(i+1, len(array)):
if array[j]<array[smallest]:
smallest = j
array[i], array[smallest] = array[smallest], array[i]
2021-2025-IT-A
print(array)
User Output
Enter the list of numbers:
23 56 85 74 12 2 0
Sorted list is: [0, 2, 12, 23, 56, 74, 85]
Test Case - 2
User Output
Enter the list of numbers:
1233215
Sorted list is: [1, 1, 2, 2, 3, 3, 5]
Test Case - 3
User Output
Enter the list of numbers:
10 20 3 30 4 40 50 5
Sorted list is: [3, 4, 5, 10, 20, 30, 40, 50]
Exp. Name: Write a program to convert the given list of
S.No: 11 Date: 2023-04-19
numbers in ascending order using insertion sort
Aim:
Page No: 18
Write a python program to convert the given list of numbers in ascending order using insertion sort .
Source Code:
insertionSort.py
ID: 2100330130004
def insertion_sort(array):
for i in range(1,len(array)):
key = array[i]
j=i-1
array[j+1] =key
array = input("Enter the list of numbers: ").split()
2021-2025-IT-A
array=[int(x) for x in array]
insertion_sort(array)
print("Sorted list is:", array)
User Output
Enter the list of numbers:
22 56 12 13 1 2 85 100
Sorted list is: [1, 2, 12, 13, 22, 56, 85, 100]
Test Case - 2
User Output
Enter the list of numbers:
100 200 2 1 1 5 900
Sorted list is: [1, 1, 2, 5, 100, 200, 900]
Test Case - 3
User Output
Enter the list of numbers:
0 0 9 5 4 4 1 2 8 1 1 10 78
Sorted list is: [0, 0, 1, 1, 1, 2, 4, 4, 5, 8, 9, 10, 78]
S.No: 13 Exp. Name: Program on prime numbers Date: 2023-04-19
Aim:
Write a program to print the first N prime numbers .
Page No: 19
Constraints:
• N should always be greater than 0.
Source Code:
ID: 2100330130004
primeNumber.py
num=int(input("Enter the number of primes required: "))
c = 2
while num!=0:
for i in range(2,c):
if c%i==0:
break
else:
print(c, end =" ")
num -= 1
c+= 1
2021-2025-IT-A
print("\n")
User Output
Enter the number of primes required:
5
2 3 5 7 11
Test Case - 2
User Output
Enter the number of primes required:
10
2 3 5 7 11 13 17 19 23 29
Test Case - 3
User Output
Enter the number of primes required:
12
2 3 5 7 11 13 17 19 23 29 31 37
Test Case - 4
User Output
Enter the number of primes required:
30
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109
113
Page No: 20
ID: 2100330130004
2021-2025-IT-A
Raj Kumar Goel Institute Of Technology
Exp. Name: Compute area and circumference of a
S.No: 14 Date: 2023-04-20
triangle
Aim:
Page No: 21
Write a Python program to compute area and circumference of a triangle. Take input from user.
Source Code:
triangle.py
ID: 2100330130004
a=float(input("Enter length of side-1 of triangle: "))
b=float(input("Enter length of side-2 of triangle: "))
c=float(input("Enter length of side-3 of triangle: "))
p=(a+b+c)
s=(a+b+c)/2
area=(s*(s-a)*(s-b)*(s-c))**0.5
print("The Perimeter of Triangle = {:.2f}".format(round(p,2)))
print("The Semi Perimeter of Triangle = {:.2f}".format(round(s,2)))
print("The Area of a Triangle = {:.2f}".format(round(area,2)))
2021-2025-IT-A
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter length of side-1 of triangle:
Test Case - 2
User Output
Enter length of side-1 of triangle:
10
Enter length of side-2 of triangle:
12
Enter length of side-3 of triangle:
13
The Perimeter of Triangle = 35.00
The Semi Perimeter of Triangle = 17.50
The Area of a Triangle = 57.00
Test Case - 3
User Output
Enter length of side-1 of triangle:
112
Enter length of side-2 of triangle:
113
Page No: 22
Enter length of side-3 of triangle:
114
The Perimeter of Triangle = 339.00
The Semi Perimeter of Triangle = 169.50
ID: 2100330130004
The Area of a Triangle = 5528.27
2021-2025-IT-A
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a Program to check whether the given
S.No: 15 Date: 2023-04-20
number is an Even number or Odd
Aim:
Page No: 23
Write a program to check whether the given number is an even number or odd .
ID: 2100330130004
So we need to typecast it to integer after getting the input.
At the time of execution, the program should print the message on the console as:
Enter an integer:
Enter an integer: 14
2021-2025-IT-A
For example, if the user given the input as 141 , then the program should print the result as "Given number 141
is odd".
Source Code:
Lab3a.py
User Output
Enter an integer:
156
Given number 156 is even
Test Case - 2
User Output
Enter an integer:
159
Given number 159 is odd
115692
User Output
Enter an integer:
Raj Kumar Goel Institute Of Technology 2021-2025-IT-A ID: 2100330130004 Page No: 24
S.No: 16 Exp. Name: Leap year Date: 2023-04-20
Aim:
Write a Python program to check if a given year is a leap year or not.
Page No: 25
Sample Input and Output:
ID: 2100330130004
2020 is a leap year
Source Code:
leapYear.py
a=int(input("Enter a year: "))
2021-2025-IT-A
if a%4==0:
print(a,"is a leap year")
else:
print(a,"is not a leap year")
User Output
Enter a year:
1994
1994 is not a leap year
Test Case - 2
User Output
Enter a year:
2006
2006 is not a leap year
Test Case - 3
User Output
Enter a year:
2020
2020 is a leap year
2006
User Output
Enter a year:
Raj Kumar Goel Institute Of Technology 2021-2025-IT-A ID: 2100330130004 Page No: 26
S.No: 17 Exp. Name: Python Program to print Fibonacci series Date: 2023-04-20
Aim:
Take an integer n as input from the console using input() function. Write a program to calculate the
Page No: 27
Fibonacci series i.e., 0 1 1 2 3 5 8 13 21....., up to the given limit n , print the result to the console as
shown in the example.
ID: 2100330130004
n: 5
0
1
1
2
3
5
Hint: By definition, the first two numbers in the Fibonacci sequence are 0 and 1 , and each subsequent
number is the sum of the previous two.
2021-2025-IT-A
Source Code:
FibonacciSeries.py
num=int(input("n: "))
if num==0:
print(n1)
print(n2)
for i in range(i,num+1):
n3=n1+n2
n1=n2
n2=n3
if (n3<=num):
print(n3)
i=i+1
User Output
n:
0
0
3
2
1
1
0
8
5
3
2
1
1
0
4
9
n:
n:
User Output
User Output
Test Case - 3
Test Case - 2
Raj Kumar Goel Institute Of Technology 2021-2025-IT-A ID: 2100330130004 Page No: 28