0% found this document useful (0 votes)
1 views

pythonAssignment1

Pyhon assistant 1

Uploaded by

kagwadeashok5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

pythonAssignment1

Pyhon assistant 1

Uploaded by

kagwadeashok5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment No:4

Aim: Write a python program to demonstrate tuple and dictionary in


python
PROGRAM:
# Program to demonstrate tuple and dictionary in Python
# Tuple demonstration
print("Tuple Demonstration:")
# Creating a tuple
my_tuple = ("Apple", "Banana", "Cherry", "Date")
print("Original Tuple:", my_tuple)
# Accessing elements in a tuple
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])
# Tuples are immutable, but we can concatenate or slice them
new_tuple = my_tuple + ("Elderberry",)
print("Concatenated Tuple:", new_tuple)
# Slicing a tuple
print("Sliced Tuple (first 2 elements):", my_tuple[:2])
# Dictionary demonstration
print("\nDictionary Demonstration:")
# Creating a dictionary
my_dict = {
"name": "John",
"age": 25,
"city": "New York"
}
print("Original Dictionary:", my_dict)
# Accessing dictionary values
print("Name:", my_dict["name"])
print("Age:", my_dict["age"])
# Adding a new key-value pair
my_dict["email"] = "john@example.com"
print("Dictionary after adding email:", my_dict)
# Updating an existing key
my_dict["age"] = 26
print("Dictionary after updating age:", my_dict)
# Removing a key-value pair
my_dict.pop("city")
print("Dictionary after removing city:",my_dict)

OUTPUT:
Assignment No:5
Aim: Write a python program to demonstrating the tuple.
PROGRAM:
# Demonstrating Tuple
print("Tuple Example:")
# Creating a tuple with multiple data types
student_info = ("John", 21, "Computer Science")

# Accessing elements in a tuple


print("Name:", student_info[0])
print("Age:", student_info[1])
print("Department:", student_info[2])

# Tuples are immutable, so we cannot change them directly


# student_info[1] = 22 # This will raise an error

print("\n")

# Demonstrating Dictionary
print("Dictionary Example:")
# Creating a dictionary with key-value pairs
student_grades = {
"John": "A",
"Alice": "B",
"Bob": "C"
}
# Accessing values using keys
print("John's grade:", student_grades["John"])
print("Alice's grade:", student_grades["Alice"])
# Adding a new key-value pair to the dictionary
student_grades["Eve"] = "A"
print("Updated student grades:", student_grades)
# Modifying an existing value
student_grades["Bob"] = "B"
print("Modified student grades:", student_grades)
# Iterating over the dictionary
print("\nIterating through dictionary:")
for student, grade in student_grades.items():
print(f"{student}: {grade}")
OUTPUT:
Assignment No.6
Aim : Write a python program to display name,age and student id.
PROGRAM:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def introduce(self):
super().introduce()
print(f"My student ID is {self.student_id}.")
person1 = Person("Alice", 30)
person1.introduce()
student1 = Student("Bob", 20, "S12345")
student1.introduce()
OUTPUT:
Assignment No.7
Aim: Write a python program to perform following operations
1.Polymorphism 2.Encapsulation 3.Magic Method.
1.Polymorphism:
PROGRAM:
#Polymorphism Program
class Cat:
def sound(self):
return "Meow"

class Dog:
def sound(self):
return "Bark"

def make_sound(animal):
print(animal.sound())

cat = Cat()
dog = Dog()

make_sound(cat)
make_sound(dog)
OUTPUT:
2.Encapsulation:
PROGRAM:
#Encapsulation Program
class BankAccount:
def __init__(self, balance):
self.__balance = balance

def deposit(self, amount):


self.__balance += amount

def withdraw(self, amount):


if self.__balance >= amount:
self.__balance -= amount
return amount
else:
return "Insufficient balance"

def get_balance(self):
return self.__balance

# Encapsulation in action
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())
print(account.withdraw(300))
print(account.get_balance())
OUTPUT:
3.Magic Methods
PROGRAM:
#Magic Method Program
class Book:
def __init__(self, title, author):
self.title = title
self.author = author

def __str__(self):
return f"'{self.title}' by {self.author}"

def __add__(self, other):


return f"{self.title} and {other.title} are now a book series!"

book1 = Book("1984", "George Orwell")


book2 = Book("Brave New World", "Aldous Huxley")

print(book1)
print(book1 + book2)

OUTPUT:

You might also like