5ab6a7a

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Program No.

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.

:param name: Name of the person


:param greeting: Greeting message (default is "Hello")
"""
print(f"{greeting}, {name}!")
# Using the function with default argument
greet("Alice")
# Using the function with a custom greeting
greet("Bob", greeting="Hi")
Input/Output

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.

:param args: A variable number of numeric arguments


"""
global total_sum # Declare total_sum as global to modify it
for number in args:
total_sum += number
print(f"Current total sum: {total_sum}")
# Using the function with variable-length arguments
add_numbers(1, 2, 3)
add_numbers(4, 5)
add_numbers(10)
# Final value of total_sum
print(f"Final total sum: {total_sum}")

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy