1)Write a Python script using class to reverse a string word by word
class py_solution:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))
print(py_solution().reverse_words('hello .py'))
2)Write a Python class named Student with two attributes student_name, marks.
Modify the attribute values of the said class and print the original and modified
values of the said attributes.
class Student:
student_name = 'Terrance Morales'
marks = 93
print(f"Student Name: {getattr(Student, 'student_name')}")
print(f"Marks: {getattr(Student, 'marks')}")
setattr(Student, 'student_name', 'Angel Brooks')
setattr(Student, 'marks', 95)
print(f"Student Name: {getattr(Student, 'student_name')}")
print(f"Marks: {getattr(Student, 'marks')}")
3)Write a python script to define the class person having members name, address.
Create a subclass called Employee with members staffed salary. Create 'n' objects of the
Employee class and display all the details of the employee.
class Employee:
def__init__(self)
Class Person:
def__init__(self,name,address):
Self.name=name
self.address=address
def show data(self):
Print("name of employee",self.name)
Print("address of employee",self.address)
Class emoticon(person)
def__init__(self,name,address,sal):
Super().__init__(name,address)
Self.sal=sal
def show data(self):
super().Show data()
Print("salary",sal)
Name=are(input("enter name of employee"))
Address=str(input("enter address of employee"))
Sal=int(input("enter salary of employee"))
e1=emp(name,address,sal)
e1.showdata()
4)Write a Python script to generate and print a dictionary which contains a number
(between 1 and n) in the form(x,x*x).
Sample Dictionary (n=5) Expected Output: {1:1, 2:4, 3:9, 4:16, 5:25}
n=int(input("Input a number "))
d = dict()
for x in range(1,n+1):
d[x]=x*x
print(d)
5)Write a Python Program to Check if given number is prime or not. Also find
factorial of the given no using user defined function.
def fact(x):
f=1
i=1
While I<=x:
f=f*i
i=i+1
Print("factorial is ",f)
b=int(input("enter number"))
fact(b)
def prime(x)
i=2
b=x
flag=0
While(b<x)
If b%i==0:
flag=1
Break
Else:
i=i+1
Continue
if flag==0:
print("no prime")
else
print("not prime")
c=int(input("enter number"))
Prime(c)
6)Write an anonymous function to find area of square and rectangle.
a=int(input("enter side of square"))
b=int(input("enter length of rectangle'))
c=int(input("enter breadth of rectangle"))
Squares=lambda a:a*a
recarea=lambda b,c:b*c
Print("area of square",squarea)
Print("area of rectangle,recarea)
7)Write a Python program to accept two lists and merge the two lists into list of tuple.
l1=["amaze","kia","creta"]
l2=["honda","Suzuki","tata","Honda"]
Print("original is l1",l1)
Print("original is l2",l2)
l1.extend(l2)
Print("merged list",l1)
8).Write a Python program to input a positive integer. Display correct message for
correct and incorrect input. (Use Exception Handling)
n = int(input("Please enter a number: "))
while True:
try:
n = input("Please enter an integer: ")
n = int(n)
break
except ValueError:
print("No valid integer! Please try again ...")
print("Great, you successfully entered an integer!")
9)Write a Python function that accepts a string and calculate the number of upper case
letters and lower case letters.
def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])
string_test('The quick Brown Fox')
10)Write a python script to generate Fibonacci terms using generator function
def fibonacci():
a=0
b=1
for i in range(6):
print(b)
a,b= b,a+b
obj = fibonacci()