5ab6a7a
5ab6a7a
5ab6a7a
5(a)
Objective: Write Python functions using the concept of Keyword & default arguments and
write a program to use them.
Code:
def greet(name, greeting="Hello"):
"""
Greets a person with a specified greeting.
1
Program No. 5(b)
Objective: Write python functions to use the concept of variable length argument & global
variable.
Code:
# Global variable
total_sum = 0
def add_numbers(*args):
"""
Adds an arbitrary number of numbers and updates the global total_sum variable.
Output
2
Program No. 6(a)
Objective: Create a class Account with name, account no and balance as attribute and
no_of_accounts as class variable. Account no should be generated automatically (starting from
1) using the class variable no_of_account. Add the methods for displaying the account
information, depositing given amount, withdrawing given amount and initializer method to
initialize the object. Create objects of Account class and call different method to test the class.
Code:
class account:
no_of_account = 0
def __init__(self,account_name,balance = 0):
account.no_of_account += 1
self.account_no = account.no_of_account
self.account_name = account_name
self.balance = balance
def deposit(self,amount):
if amount > 0:
self.balance += amount
print(f"Deposited {amount}, New account balance is {self.balance}")
else:
print("Invalid amount")
def withdraw(self,amount):
if amount > self.balance:
print("Insufficient balance")
elif amount <=0:
print("Invalid Amount")
else:
self.balance -= amount
print(f"Withdrew {amount}, New account balance is {self.balance}")
def display_info(self):
print(f"Account Number: {self.account_no}")
print(f"Account Name: {self.account_name}")
print(f"Account Balance: {self.balance}")
3
if __name__ == "__main__":
account1 = account("Aakash Sharma",1000)
account2 = account("Avni Sharma", 1500)
account1.display_info()
account2.display_info()
account1.deposit(500)
account1.withdraw(200)
account2.withdraw(1000)
account1.display_info()
account2.display_info()
Input/Output
4
Program 7(a)
Objective: Create a class Polygon to represent a polygon having no of sides and a list having
magnitude of each side as attribute. Add the inputSides() to input sides and displaySides() to
display sides as methods. Derive a class Triangle from Polygon and add an additional method
displayArea() to display area. Create object of Triangle and call different methods to test the
class.
Code:
class polygon:
def __init__(self):
self.no_of_sides = 0
self.sides = []
def inputSides(self):
self.no_of_sides = int(input("enter the number of sides: "))
self.sides = []
for i in range(self.no_of_sides):
side_length = float(input(f"enter the length of side{i+1}: "))
self.sides.append(side_length)
def displaySides(self):
print("The no of Sides of the Polygon are: ", self.no_of_sides)
print("The length of the sides of the Polygon are: ", self.sides)
class Triangle(polygon):
def __init__(self):
super().__init__()
def displayArea(self):
if len(self.sides) != 3:
print("the polygon is not a Triangle")
return
else:
s = sum(self.sides) / 2
area = (s * (s - self.sides[0]) * (s - self.sides[1]) * (s - self.sides[2]))**0.5
5
print("Area of the triangle:", area)
if __name__ == "__main__":
triangle = Triangle()
triangle.inputSides()
triangle.displaySides()
triangle.displayArea()
Input/Output