Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Yash Rughwani

0801IT211098
ASSIGNMENT 04
Ques1) Write a python program to add and remove item(s) from set.
Program:
num_set = set([0, 1, 3, 4, 5])
print("Original set:")
print(num_set)
print("\nAfter adding number 2 into set:")
num_set.add(2)
print(num_set)
print("\nAfter removing the first element from the said set:")
num_set.pop()
print(num_set)

Output:

Ques2) Write a python program to remove an item from a set if it is present in the set.
Program:
num_set = set([0, 1, 2, 3, 4, 5])
print("Original set elements:")
print(num_set)
print("\nRemove 0 from the said set:")
num_set.discard(4)
print(num_set)
print("\nRemove 5 from the said set:")
num_set.discard(5)
print(num_set)
print("\nRemove 2 from the said set:")
num_set.discard(5)
print(num_set)
print("\nRemove 7 from the said set:")
num_set.discard(15)
print(num_set)

Output:

Ques3) Write a python program to create an intersection, union difference and symmetric
difference of two sets.
Program:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# intersection
print("Intersection of set1 and set2:", set1.intersection(set2))

# union
print("Union of set1 and set2:", set1.union(set2))

# difference
print("Difference of set1 and set2:", set1.difference(set2))

# symmetric difference
print("Symmetric difference of set1 and set2:", set1.symmetric_difference(set2))

Output:
Ques4)Write a python program to create a shallow copy of sets.
Program:
# create a set
original_set = {1, 2, 3, 4, 5}
# make a shallow copy of the set
copied_set = original_set.copy()
# print out the original set and copied set
print("Original set:", original_set)
print("Copied set:", copied_set)

Output:

Ques5)Write a python program to create an intersection, union difference and symmetric


difference of two frozensets.
Program:
fset1 = frozenset([1, 2, 3, 4, 5])
fset2 = frozenset([4, 5, 6, 7, 8])

# intersection
print("Intersection of fset1 and fset2:", fset1.intersection(fset2))

# union
print("Union of fset1 and fset2:", fset1.union(fset2))

# difference
print("Difference of fset1 and fset2:", fset1.difference(fset2))
# symmetric difference
print("Symmetric difference of fset1 and fset2:", fset1.symmetric_difference(fset2))

Output:

Ques6)Write a program that will give suggested footwear based on the weather.

“Ask the user for the weather outside with three options (sunny, rainy, or snowy) and give the
correct footwear suggestion (sneaker, gumboot, or boots). Each option
should be written as its own function that prints a message based on the input.”

Program:
def suggest_sneaker():
print("It's sunny outside. Wear sneakers.")

def suggest_gumboot():
print("It's rainy outside. Wear gumboots.")

def suggest_boots():
print("It's snowy outside. Wear boots.")

weather = input("What is the weather outside? (sunny, rainy, or snowy): ")


if weather == "sunny":
suggest_sneaker()
elif weather == "rainy":
suggest_gumboot()
elif weather == "snowy":
suggest_boots()
else:
print("Invalid input.")

Output:
Ques7)Write a program that asks the user for two numbers. Then ask them if they would like to
add, subtract, divide, or multiply these numbers. Perform the chosen operation on the values,
showing the operation being performed. Write four functions, one for each mathematical
operation.
Example: add(), subtract(), Multiply(), and Divide()
Program:
def add(num1, num2):
return num1 + num2

def subtract(num1, num2):


return num1 - num2

def multiply(num1, num2):


return num1 * num2

def divide(num1, num2):


return num1 / num2

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")

if operation == '+':
result = add(num1, num2)
print(f"{num1} + {num2} = {result}")
elif operation == '-':
result = subtract(num1, num2)
print(f"{num1} - {num2} = {result}")
elif operation == '*':
result = multiply(num1, num2)
print(f"{num1} * {num2} = {result}")
elif operation == '/':
result = divide(num1, num2)
print(f"{num1} / {num2} = {result}")
else:
print("Invalid operation.")

Output:

Ques8)Write a function called favorite_book() that accepts two parameter, title and author. The
function should print a message, such as The History of Modern India by Bipan Chandra. Call
the function, making sure to include a book title and author as an argument def
favorite_book(title, author):
Program:
def favorite_book(title, author):
print(f"My favorite book is '{title}' by {author}.")

favorite_book("The History of Modern India", "Bipan Chandra")

Output:

Ques9)Write a function that takes two arguments and returns their sum.
Program:
def add(num1, num2):
return num1 + num2

result = add(5, 10)


print("Result:", result)

Output:
Ques10)Write a function called is_leap() that accepts one parameter i.e. year. The function
should check whether the year is a leap year or not.
Hint: In the Gregorian calendar, two conditions are used to identify leap years:
• The year that can be evenly divided by 4 but not by 100, is a leap year.
• The year is also a leap year if it is evenly divisible by 400.
Program:
def is_leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False

year = int(input("Enter a year: "))


if is_leap(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")

Output:

You might also like