Assignment - 4: Q.1. Write A Python Program To Add and Remove Item(s) From A Set ? A.1. Code
Assignment - 4: Q.1. Write A Python Program To Add and Remove Item(s) From A Set ? A.1. Code
0801IT223D09
Assignment - 4
Q.1. Write a python program to add and remove item(s) from a set ?
A.1.
Code :-
# A Python program to add and remove an item from a set.
Q.2. Write a python program to remove an item from a set if it is present in the set.
A.2.
Code :-
# create a set
my_set = {1, 2, 3, 4, 5}
Output :-
Q.3. Write a python program to create an intersection, union difference and symmetric
difference of two sets.
A.3.
Code :-
# create two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
Output :-
Output :-
Q.5. Write a python program to create an intersection, union difference and symmetric
difference of two frozensets.
A.5.
Code :-
# create two frozensets
frozenset1 = frozenset([1, 2, 3, 4, 5])
frozenset2 = frozenset([4, 5, 6, 7, 8])
Q.6. 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.”
A.6.
Code :-
def sunny():
print("It's sunny outside, so you should wear sneakers.")
def rainy():
print("It's rainy outside, so you should wear gumboots.")
def snowy():
print("It's snowy outside, so you should wear boots.")
def get_weather():
while True:
weather = input("What's the weather like outside? (sunny, rainy, or snowy): ")
if weather.lower() == "sunny":
sunny()
break
elif weather.lower() == "rainy":
rainy()
break
elif weather.lower() == "snowy":
snowy()
break
else:
print("Invalid input. Please enter sunny, rainy, or snowy.")
get_weather()
Output :-
Q.7. 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()
A.7.
Code :-
def add(num1, num2):
result = num1 + num2
print(f"{num1} + {num2} = {result}")
def get_numbers():
while True:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
return num1, num2
except ValueError:
print("Invalid input. Please enter a number.")
def get_operation():
while True:
operation = input("What operation would you like to perform? (add, subtract, multiply,
divide): ")
if operation.lower() in ("add", "subtract", "multiply", "divide"):
return operation.lower()
else:
print("Invalid input. Please enter add, subtract, multiply, or divide.")
if operation == "add":
add(num1, num2)
elif operation == "subtract":
subtract(num1, num2)
elif operation == "multiply":
multiply(num1, num2)
else:
divide(num1, num2)
Output :-
Q.8. 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 in the function call.
A.8.
Code :-
def favorite_book(title, author):
"""Prints a message about the favorite book"""
print("My favorite book is '" + title + "' by " + author + ".")
Output :-
Q.9. Write a function that takes two arguments and returns their sum.
A.9..
Code :-
def add_numbers(num1, num2):
return num1 + num2
Output :-
Q.10. 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.
A.10.
Code :-
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
Output :-