0% found this document useful (0 votes)
59 views9 pages

Assignment - 4: Q.1. Write A Python Program To Add and Remove Item(s) From A Set ? A.1. Code

The document contains Python code solutions to 10 assignment questions about sets, functions, and conditional logic. For each question, the code provides the necessary functions and conditionals to add/remove items from sets, perform set operations, take user input, perform mathematical operations on numbers, print messages based on conditions, and check for leap years. The code samples are accompanied by expected output.

Uploaded by

yash
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)
59 views9 pages

Assignment - 4: Q.1. Write A Python Program To Add and Remove Item(s) From A Set ? A.1. Code

The document contains Python code solutions to 10 assignment questions about sets, functions, and conditional logic. For each question, the code provides the necessary functions and conditionals to add/remove items from sets, perform set operations, take user input, perform mathematical operations on numbers, print messages based on conditions, and check for leap years. The code samples are accompanied by expected output.

Uploaded by

yash
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/ 9

Yagnesh Mittal

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.

fruit = {"apple", "banana", "pineapple", "guava"}


print("Original Set :-")
print(fruit)
print("Banana Removed :-")
fruit.remove("banana")
print(fruit)
print("Mango added :-")
fruit.add("mango")
print(fruit)

# A Python program to add and remove multiple item(s) from a set.


# create a set
my_set = {1, 2, 3, 4, 5}
print("Original Set is: {}".format(my_set))

# add multiple items to the set


my_set.update({6, 7, 8})

# print the set after adding items


print("Set after adding items:", my_set)

# remove multiple items from the set


my_set.difference_update({2, 4, 6})

# print the set after removing items


print("Set after removing items:", my_set)
Output :-

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}

# print the original set


print("Original set:", my_set)

# remove an item from the set if it is present


if 3 in my_set:
my_set.remove(3)
print("Item 3 removed from set")

# print the set after removing the item


print("Set after removing item:", my_set)

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}

# print the two sets


print("Set 1:", set1)
print("Set 2:", set2)

# find the intersection of the two sets


intersection = set1.intersection(set2)
print("Intersection:", intersection)

# find the union of the two sets


union = set1.union(set2)
print("Union:", union)

# find the difference of the two sets


difference = set1.difference(set2)
print("Difference:", difference)

# find the symmetric difference of the two sets


symmetric_difference = set1.symmetric_difference(set2)
print("Symmetric difference:", symmetric_difference)

Output :-

Q.4. Write a python program to create a shallow copy of sets.


A.4.
Code :-
# 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 :-

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])

# perform set operations on the frozensets


intersection = frozenset1.intersection(frozenset2)
union = frozenset1.union(frozenset2)
difference = frozenset1.difference(frozenset2)
symmetric_difference = frozenset1.symmetric_difference(frozenset2)

# print the results


print("Intersection: ", intersection)
print("Union: ", union)
print("Difference: ", difference)
print("Symmetric difference: ", symmetric_difference)
Output :-

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 subtract(num1, num2):


result = num1 - num2
print(f"{num1} - {num2} = {result}")

def multiply(num1, num2):


result = num1 * num2
print(f"{num1} * {num2} = {result}")

def divide(num1, num2):


if num2 == 0:
print("Error: division by zero")
else:
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.")

num1, num2 = get_numbers()


operation = get_operation()

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 + ".")

# call the function with a book title and author


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

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 :-

You might also like