Ishaan Fundametals of Computer Programming Lab
Ishaan Fundametals of Computer Programming Lab
Write a Python function that takes two numbers as input and returns a
string indicating whether the first number is greater than, less than, or
equal to the second number using comparison operators.
Code:
num_comp(num1, num2)
Output :
Experiment 2
Create a Python program that functions as a calculator. It should
support addition, subtraction, multiplication, division, and modulus
operations. Maintain a history of calculations performed and provide an
option to display the history. Implement error handling for division by
zero.
Code:
hist = []
while True:
if oper == 'history':
for i in hist:
print(i)
break
if oper == '+':
print("Output :", num1 + num2)
hist.append(f"{num1} + {num2} = {num1 + num2}")
elif oper == '-':
print("Output :", num1 - num2)
hist.append(f"{num1} - {num2} = {num1 - num2}")
elif oper == '*':
print("Output :", num1 * num2)
hist.append(f"{num1} * {num2} = {num1 * num2}")
elif oper == '/':
if num2 != 0:
print("Output :", num1 / num2)
hist.append(f"{num1} / {num2} = {num1 / num2}")
elif num2 == 0:
print("Can not divide by Zero.")
elif oper == '%':
print("Output :", num1 % num2)
hist.append(f"{num1} % {num2} = {num1 % num2}")
Experiment 3
Develop a Python function that takes a list of tuples as input, where
each tuple contains two numbers. For each tuple, compare the numbers
using all comparison operators and store the results in a dictionary.
Return a list of dictionaries for all comparisons.
Code:
def tuple_comp(l):
dict_list = [{} for _ in range(len(l))]
for i in range(len(l)):
return dict_list
given_list = [(1,2),(2,2),(2,1)]
print(tuple_comp(given_list))
Output:
Experiment 4
Write a Python program to simulate the operations of basic logic gates
(AND, OR, NOT, NAND, NOR, XOR) using functions. The program should
take two boolean inputs and the type of gate, then return the result of
the logical operation.
Code:
if input1.lower() == 'false':
input1 = 0
if input2.lower == 'false':
input2 = 0
if gate.upper() == 'AND':
Output:
Experiment 5
Develop a Python function that takes a string and a substring as input
and returns the number of times the substring appears in the string.
Implement this without using built-in string functions and optimize it
for large inputs.
Code:
if string[i] == substring[0]:
for j in range(len(substring)):
if string[i + j] != substring[j]:
break
else:
matches += 1
return matches
Output:
Experiment 6
Write a Python function that performs bitwise operations (AND, OR,
XOR) on two arrays of integers. The function should take two arrays
and an operator as input, perform the bitwise operation element-wise,
and return the resulting array.
Code:
input_list_1 = [1,2,3,4]
input_list_2 = [5,6,7,8]
Output:
Experiment 7
Create a Python function that takes a list of integers and returns a new
list containing only the unique elements of the original list. Use a set to
remove duplicates and maintain the order of elements as they appear in
the original list.
Code:
def remve_dups(int_list):
new_list = []
set_of_int_list = set(int_list)
for i in int_list:
if i in set_of_int_list:
new_list.append(i)
set_of_int_list.remove(i)
return new_list
input_list = [1,2,4,3,2,1,5]
print(remve_dups(input_list))
Output:
Experiment 8
Write a Python function that takes a string of text and returns a
dictionary with the frequency of each word in the text. The function
should handle punctuation and capitalization correctly and provide a
case-insensitive count.
Code:
def frequency_checker(text):
freq_dict = {}
l_text = text.split()
for i in range(len(l_text)):
if l_text[i][-1] == ',' or l_text[i][-1] == '.' or
l_text[i][-1] == ':' or l_text[i][-1] == ';':
l_text[i] = l_text[i][:-1]
return freq_dict
Output:
Experiment 9
Create a Python function that takes a tuple as an argument and returns
a new tuple with the elements reversed. Demonstrate the use of tuples
as function parameters and unpacking tuple elements within the
function.
Code:
def tuple_revesion(tup):
x,y = tup
new_tup = (y,x)
return new_tup
input_tuple = (11,22)
print(tuple_revesion(input_tuple))
Output:
Experiment 10
Create a Python function that finds all prime numbers up to a given
number n. Use nested loops to check for primality and optimize the
function to handle large inputs efficiently. Additionally, allow the user
to break the loop prematurely if they input a specific command.
Code:
def find_primes(n):
for x in range(2,n+1):
for i in range(2,(x//2)+1):
if x % i == 0 :
break
else:
print(x)
if input('quit?(y/n): ') == 'y':
break
find_primes(50)
output: