1. (a).
Write a python program to print the multiplication table for the given
number?
Code:
# Python program to find the multiplication table (from 1 to 10) of a number
input by the user
# take input from the user
num = int(input("Display multiplication table of? "))
# use for loop to iterate 10 times
for i in range(1,11):
print(num,'x',i,'=',num*i)
Output:
Display multiplication table of? 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
(b). Write a python program to check whether the given number is prime or not?
Code:
# Python program to check if the input number is prime or not
#num = 407
# take input from the user
num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
rem=1
for i in range(2,num):
rem=num%i
if rem == 0:
break
if rem == 0:
print(num,"is not a
prime number")else:
print(num,"is a prime number")
# if input number
is less than # or
equal to 1, it is not
primeelse:
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 factorialof a
number using
recursion"""
:
r
else:
return n*recur_factorial(n-1)
# Change this value for a
different result#num = 5
# uncomment to take input
from the usernum =
int(input("Enter a number: "))
# check is the number
is negativeif 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. (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 List2
n_list2 = ["Happy", [2,0,1,5]]
# Nested indexing #
Output: a
print(n_list2[0][1])#
Output: 5
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:
Length ['p', 'r', 'o', 'b', 'e']: 5o
Concatenation: [1, 3, 5] [7, 8, 9] : [1, 3, 5, 7, 8, 9]
Repetition: Hello HelloHelloHelloHello
(b) Write a python program to implement list operations (add, append, extend &delete)
Code:
myList = ["Earth", "revolves", "around",
"Sun"]print(myList)
print(len(myList)
)print(myList[0])
print(myList[3])
#Slice elements from a
listprint(myList[1:3])
#Use negative index
print(myList[-1])
#print(myList[4])
#add element to a list
myList.insert(0,"The")
print(myList)
print(len(myList))
myList.insert(4,"the")
print(myList)
#append an element to a list
myList.append("continuously")
print(myList)
print(len(myList))
#When use extend the argument should be another
list
#the elements of that list will be added
#to the current list as individual
elementsmyList.extend(["for", "sure"])
print(myList)
print(len(myList)
#you can append a sublist to the current list using
appendmyList.append(["The","earth","rotates"])
print(myList)
print(len(myList)
#delete a element in the list using
elementmyList.remove("The")
#delete a element in the list using
indexmyList.remove(myList[3])
print(myList)
Output
['Earth', 'revolves', 'around',
'Sun']
Eart
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']
['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously', 'for', 'sure', ['The',
'earth', 'rotates']]
10
['Earth', 'revolves', 'around', 'Sun', 'continuously', 'for', 'sure', ['The', 'earth', 'rotates']]
3. 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?")
print("Nice name and Nice meeting you",name)elif
question in ['what did you do yesterday?']:
print("I saw Bahubali 5 times")
elif question in ['quit']:
break
else:
print("I don't understand what you said")
Output:
Simple Question and Answering Program
==============================
You may ask any one of these
questions
Hi
How are you?
Are you
working?
What is your name?
what did you do yesterday?
Quit
Enter one question from above
list:hi
Hello
Enter one question from above list:how are
you? I am fine
Enter one question from above list:are you
working? yes. I'am working in KLU
Enter one question from above list:what is your
name?My name is Emilia
Enter your name?sachin
Nice name and Nice meeting you
sachinEnter one question from above
list:quit
4(a). Write a python program to Illustrate Different Set Operations?
Code:
# Program to perform different set operations like in
mathematics# define three sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
print("Union of E and N is",E |
N)# set intersection
print("Intersection of E and N is",E &
N)# set difference
print("Difference of E and N is",E -
N)# set symmetric difference
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}