PWP Exam Programs
PWP Exam Programs
'''
#4. WAP to display n terms of the Fibonacci series.
n = int(input("Enter no of term : "))
i=1
t1,t2 = -1,1
while i<=n:
t = t1+t2
print(t,end = "")
t1 = t2
t2 = t
i+=1
'''
'''
#25. WAP with function to calculate the factorial of a given number.
no=int(input("Enter No : "))
fact=1
if no<0:
print("Sorry factorial does not exist for negative numbers")
elif no == 0:
print("Factorial of 0 is 1")
else:
for i in range(1,no+1):
fact*=i
print("Factorial of",no,"is",fact)
'''
'''
#2. WAP to check if given number is palindrome or not.
no = int(input("Enter No : "))
temp = no
rev = 0
while(no>0):
rem = no%10
rev = rev*10+rem
no = no//10
if temp==rev:
print("Palndrome")
else:
print("Not a Palindrome")
'''
'''
#1. WAP to display the sum of digits and reverse of a number.
no = int(input("Enter No : "))
sum = 0
rev = 0
while no>0:
rem = no%10
rev = rev*10+rem
sum+=rem
no = no//10
print("Sum is :",sum,"Reverse is :",rev)
'''
'''
#5. WAP to create a list and demonstrate any four list methods.
l1 = [5,10,3,2,7]
print(l1.count(10))
l1.append(13)
print(l1)
l1.sort()
print(l1)
l1.reverse()
print(l1)
l1.pop(0)
print(l1)
'''
'''
#6. WAP to find the repeated elements of a list.
lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
uniqueList = []
duplicateList = []
for i in lis:
if i not in uniqueList:
uniqueList.append(i)
elif i not in duplicateList:
duplicateList.append(i)
print(duplicateList)
'''
'''
#7. WAP using list comprehension to create a list of cubes of all nos upto 10.
l = [1, 2, 3, 4]
res = []
for i in l:
res.append(i*i*i)
print(res)
'''
'''
#8. WAP using list comprehension to create a list of squares of all even nos from a given list.
data=[1,2,3,4,5,6,7,8]
result = [i*i for i in data if i%2==0]
print(result)
'''
'''
#9. WAP using list comprehension to include all strings in given list except those with letter ‘a’.
string_list = ['apple', 'banana', 'orange', 'kiwi', 'pear', 'grape']
filtered_list = [string for string in string_list if 'a' not in string]
print(filtered_list)
'''
'''
#10. WAP to create a tuple and demonstrate any four tuple methods/functions.
# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
'''
#11. WAP to create a tuple from two existing tuples using slicing.
# Two existing tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
'''
#12. WAP to create a set and demonstrate any four set methods.
# Create a set
my_set = {1, 2, 3, 4, 5}
'''
#13. WAP using set comprehension to create a set of multiples of 5 till 100.
# Set comprehension to create a set of multiples of 5 till 100
multiples_of_5 = {x for x in range(1, 101) if x % 5 == 0}
'''
#14. WAP to create a dictionary and demonstrate any four dictionary methods.
# Create a dictionary
my_dict = {'apple': 2, 'banana': 3, 'orange': 4, 'kiwi': 5}
'''
#15. WAP to combine values of two dictionaries having similar keys and create a new dictionary.
# Two dictionaries with similar keys
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'a': 4, 'b': 5, 'd': 6}
'''
#16. WAP to sort a dictionary by values.
# Dictionary to be sorted
my_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'kiwi': 3}
'''
#17. WAP to find all unique values in a dictionary.
# Dictionary
my_dict = {'a': 1, 'b': 2, 'c': 1, 'd': 3, 'e': 2}
'''
#18. WAP to demonstrate four built in string functions.
# 1. len() - Returns the length of the string
string = "Hello, World!"
print("Length of the string:", len(string))
'''
#19. WAP to count the number of vowels in the given string.
# Input string
input_string = "Hello, World!"
# Define vowels
vowels = "aeiouAEIOU"
# Initialize counter
count = 0
'''
#20. WAP to find the least frequency character in given string.
# Input string
input_string = "hello world"
'''
#21. WAP to demonstrate the L-E-G function scope in Python.
# Global variable
global_var = 10
def outer_function():
# Enclosing variable
enclosing_var = 20
def inner_function():
# Local variable
local_var = 30
print("Inside inner_function:")
print("Local variable:", local_var)
print("Enclosing variable:", enclosing_var)
print("Global variable:", global_var)
print("Inside outer_function:")
print("Enclosing variable:", enclosing_var)
print("Global variable:", global_var)
'''
#22. WAP to demonstrate user defined functions with default arguments.
# Function with default arguments
def greet(name="Guest", country="India"):
print("Hello,", name, "! Welcome to", country)
'''
#23. WAP to demonstrate user defined functions with keyword arguments.
# Function to calculate the area of a rectangle
def calculate_area(length, width=5):
area = length * width
return area
# Input number
num = int(input("Enter a number: "))
'''
#26. WAP with function to calculate the number of uppercase and lowercase characters in given
string.
# Function to calculate the number of uppercase and lowercase characters
def count_upper_lower(string):
upper_count = sum(1 for char in string if char.isupper())
lower_count = sum(1 for char in string if char.islower())
return upper_count, lower_count
# Input string
input_string = input("Enter a string: ")
# Input string
input_string = input("Enter a string: ")
#28. WAP to access and demonstrate any four functions of math module.
import math
'''
#29. WAP to access and demonstrate four functions of any built-in Python module.
import random
'''
#30. WAP to access methods of a user defined module.
# my_module.py
def greet(name):
return "Hello, " + name + "!"
# main.py
import my_module
# Accessing the greet function
message = my_module.greet("Alice")
print(message)
'''
#31. WAP illustrating the use of user defined package in Python.
# operations.py
# main.py
Sub_result = ops.subtract(8, 3)
print("Substraction:", Sub_result)
Mul_result = ops.multiply(4, 2)
print("Multiplication:", Mul_result)
'''
'''
#32. WAP to create a class and display class attributes.
class Student:
# Class attribute
college_name = "SSVPS"
'''
#33. WAP to access object methods.
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print("Car brand:", self.brand)
print("Car model:", self.model)
def start_engine(self):
print("Engine started for", self.brand, self.model)
'''
#34. WAP to demonstrate data hiding.
class Car:
def __init__(self, brand, model, price):
self.__brand = brand # Private attribute
self.__model = model # Private attribute
self.__price = price # Private attribute
def display_info(self):
print("Car brand:", self.__brand)
print("Car model:", self.__model)
print("Car price:", self.__price)
'''
#35. WAP to demonstrate method overriding.
class Animal:
def make_sound(self):
print("Generic animal sound")
class Dog(Animal):
def make_sound(self):
print("Bark")
class Cat(Animal):
def make_sound(self):
print("Meow")
'''
#36. WAP to demonstrate multiple inheritance.
class Vehicle:
def __init__(self, color):
self.color = color
def drive(self):
print("Driving the vehicle")
class Electric:
def __init__(self, battery_capacity):
self.battery_capacity = battery_capacity
def charge(self):
print("Charging the battery")
def display_info(self):
print("Electric car color:", self.color)
print("Battery capacity:", self.battery_capacity)
'''
#37. WAP to demonstrate multilevel inheritance.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
class GoldenRetriever(Dog):
def fetch(self):
print("Golden Retriever fetches")
'''
#38. WAP to open a file in append mode, write data to it and then read it.
# Open the file in append mode and write data to it
with open("my_file.txt", "a") as file:
file.write("This is line 1\n")
file.write("This is line 2\n")
file.write("This is line 3\n")
'''
#39. WAP to read contents of file1.txt and write the same to file2.txt.
# Read the contents of file1.txt
with open("file1.txt", "r") as file1:
content = file1.read()
'''
#40. WAP to raise the ZeroDivisionError exception.
def divide(x, y):
try:
result = x / y # Attempt division
except ZeroDivisionError: # Catch ZeroDivisionError exception
print("Error: Division by zero!") # Print error message
else: # If no exception occurs
print("Result:", result) # Print result if division is successful
finally: # Finally block always executes
print("Division operation completed.") # Print completion message
# Test cases
divide(10, 2) # Normal division
divide(5, 0) # Division by zero
'''