
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































if rem == 0: print(num,"is not a prime number") else: print(num,"is a prime number")
else: print(num,"is not a prime number") Output: Enter a number: 5 5 is a prime number (c). Write a python program to find factorial of the given number? Code: def recur_factorial(n): """Function to return the factorial of a number using recursion""" if n == 1: return n else: return n*recur_factorial(n-1)
#num = 5
num = int(input("Enter a number: "))
if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else:
print("The factorial of",num,"is",recur_factorial(num)) Output: Enter a number: 5 The factorial of 5 is 120
2. Write a python program to implement simple Chatbot? Code: print("Simple Question and Answering Program") print("=====================================") print(" You may ask any one of these questions") print("Hi") print("How are you?") print("Are you working?") print("What is your name?") print("what did you do yesterday?") print("Quit") while True: question = input("Enter one question from above list:") question = question.lower() if question in ['hi']: print("Hello") elif question in ['how are you?','how do you do?']: print("I am fine") elif question in ['are you working?','are you doing any job?']: print("yes. I'am working in KLU") elif question in ['what is your name?']: print("My name is Emilia") name=input("Enter your name?")
Nice name and Nice meeting you sachin Enter one question from above list:quit
3. (a) Write a python program to implement list operations (Nested List, Length, Concatenation,Membership,Iteration,Indexing and Slicing)? Code: my_list = ['p','r','o','b','e'] # Output: p print(my_list[0]) print("Length",my_list,":",len(my_list)) # Output: o print(my_list[2]) # Output: e print(my_list[4]) # Error! Only integer can be used for indexing #my_list[4.2] # Nested List n_list = [[1,3,5,7], [2,4,6,8,10]] # Nested indexing # Output: 3 print(n_list[0][1]) # Output: 8 print(n_list[1][3]) # Nested List n_list2 = ["Happy", [2,0,1,5]]
print(n_list2[0][1])
print(n_list2[1][3]) concat1=[1,3,5] concat2=[7,8,9] print("Concatenation:",concat1,concat2,":",concat1+concat2) repetion_string="Hello" print("Repetition:",repetion_string,repetion_string * 4) Output: p Length ['p', 'r', 'o', 'b', 'e']: 5 o e 3 8 a 5 Concatenation: [1, 3, 5] [7, 8, 9] : [1, 3, 5, 7, 8, 9] Repetition: Hello HelloHelloHelloHello
print(myList) print(len(myList)) #you can append a sublist to the current list using append myList.append(["The","earth","rotates"]) print(myList) print(len(myList)) #delete a element in the list using element myList.remove("The") #delete a element in the list using index myList.remove(myList[3]) print(myList) Output ['Earth', 'revolves', 'around', 'Sun'] 4 Earth Sun ['revolves', 'around'] Sun ['The', 'Earth', 'revolves', 'around', 'Sun'] 5 ['The', 'Earth', 'revolves', 'around', 'the', 'Sun'] ['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously'] 7 ['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously', 'for', 'sure'] 9 ['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously', 'for', 'sure', ['The', 'earth', 'rotates']]
['Earth', 'revolves', 'around', 'Sun', 'continuously', 'for', 'sure', ['The', 'earth', 'rotates']] 4 (a). Write a python program to Illustrate Different Set Operations? Code:
E = {0, 2, 4, 6, 8}; N = {1, 2, 3, 4, 5};
print("Union of E and N is",E | N)
print("Intersection of E and N is",E & N)
print("Difference of E and N is",E - N)
print("Symmetric difference of E and N is",E ^ N) Output: Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8} Intersection of E and N is {2, 4} Difference of E and N is {0, 8, 6} Symmetric difference of E and N is {0, 1, 3, 5, 6, 8} (b). Write a python program to generate Calendar for the given month and year? Code:
import calendar
return x + y def subtract(x, y): ""This function subtracts two numbers""" return x - y def multiply(x, y): """This function multiplies two numbers""" return x * y def divide(x, y): """This function divides two numbers""" return x / y
print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4):") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Invalid input") Output: Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): Enter first number: 21 Enter second number: 22 21 + 22 = 43
5. (a). Write a python program to Add Two Matrices. Code: # Program to add two matrices using nested loop X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] Z = [[0,0,0], [0,0,0], [0,0,0]]
print(r) Output: [12, 4, 3] [7, 5, 8] 6.Write a python program to implement Breadth first search Traversal? Code: """ This file contains an implementation of breadth-first search (BFS) for traversing a graph, and for getting the shortest path between 2 nodes of a graph. """
def bfs_connected_component(graph, start):
explored = []
queue = [start]
while queue:
node = queue.pop(0) if node not in explored:
explored.append(node) neighbours = graph[node]
for neighbour in neighbours: queue.append(neighbour) return explored
def bfs_shortest_path(graph, start, goal):
explored = []
queue = [[start]]
if start == goal: return "That was easy! Start = goal"
while queue:
path = queue.pop(0)
node = path[-1] if node not in explored: neighbours = graph[node]
for neighbour in neighbours: new_path = list(path) new_path.append(neighbour) queue.append(new_path)
bfs_connected_component(graph, 'A')) print("\nHere's the shortest path between nodes 'D' and 'F':", bfs_shortest_path(graph, 'D', 'F')) OUTPUT: - HERE'S THE NODES OF THE GRAPH VISITED BY BREADTH-FIRST SEARCH, STARTING FROM NODE 'A': ['A', 'B', 'C', 'D', 'E', 'F'] HERE'S THE SHORTEST PATH BETWEEN NODES 'D' AND 'F': ['D', 'B', 'A', 'C', 'F']
7. Write a python program to implement Water Jug Problem? A Water Jug Problem: You are given two jugs, a 4-gallon one and a 3-gallon one, a pump which has unlimited water which you can use to fill the jug, and the ground on which water may be poured. Neither jug has any measuring markings on it. How can you get exactly 2 gallons of water in the 4-gallon jug? Let X represent the content of the water in 4-gallon jug. Let Y represent the content of the water in 3-gallon jug. Write a program in python to define a set of operators (Rules) that will take us from one state to another: Start from initial state (X=0, Y=0) Reach any of the Goal states (X=2, Y=0) (X=2, Y=1) (X=2, Y=2) (X=2, Y=3) Find the minimum number of steps to reach any the above mentioned goal states. Code: print("Water Jug Problem") x=int(input("Enter X:")) y=int(input("Enter Y:")) while True: rno=int(input("Enter the Rule No")) if rno==1: if x<4:x= if rno==2: if y<3:y=
if rno==5: if x>0:x= if rno==6: if y>0:y= if rno==7: if x+y>= 4 and y>0:x,y=4,y-(4-x) if rno==8: if x+y>=3 and x>0:x,y=x-(3-y), if rno==9: if x+y<=4 and y>0:x,y=x+y, if rno==10: if x+y<=3 and x>0:x,y=0,x+y print("X =" ,x) print("Y =" ,y) if (x==2): print(" The result is a Goal state") break Output: Water Jug Problem Enter X: Enter Y: Enter the Rule No X = 0 Y = 3 Enter the Rule No X = 3 Y = 0 Enter the Rule No X = 3 Y = 3 Enter the Rule No X = 4 Y = 2 Enter the Rule No X = 0 Y = 2