0% found this document useful (0 votes)
54 views4 pages

Python Assignment 4

This document contains examples of using sets in Python. It covers basic set operations like initialization, union, intersection, difference and symmetric difference. It also shows how to iterate through sets, add/remove elements, check if a string contains vowels, find maximum/minimum values in a set, and copy a list. The document is divided into three sections (SET A, SET B, SET C) with multiple questions in each section demonstrating various set methods and operations in Python.

Uploaded by

Atharv Karnekar
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)
54 views4 pages

Python Assignment 4

This document contains examples of using sets in Python. It covers basic set operations like initialization, union, intersection, difference and symmetric difference. It also shows how to iterate through sets, add/remove elements, check if a string contains vowels, find maximum/minimum values in a set, and copy a list. The document is divided into three sections (SET A, SET B, SET C) with multiple questions in each section demonstrating various set methods and operations in Python.

Uploaded by

Atharv Karnekar
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/ 4

PYTHON ASSIGNMENT NO.

SET A
Q1
sets = {1, 2, 3, 4, 4}
print(sets)
Q2
def Remove(sets):
sets.discard(20)
print (set)
sets = set([10, 20, 26, 41, 54, 20])
Remove(sets)
Q3
num_set = set([0, 1, 2, 3, 4, 5])
for n in num_set:
print(n, end=' ')
print("\n\nCreating a set using string:")
char_set = set("w3resource")
for val in char_set:
print(val, end=' ')
Q4
num_set = set([0, 1, 3, 4, 5])
print("Original set:")
print(num_set)
num_set.pop()
print("\nAfter removing the first element from the said set:")
print(num_set)

SET B
Q1
def check(string) :
string = string.lower()
vowels = set("aeiou")
s = set({})

LIL’JERRY
PYTHON ASSIGNMENT NO.4

for char in string :


if char in vowels :
s.add(char)
else:
pass
if len(s) == len(vowels) :
print("Accepted")
else :
print("Not Accepted")
if __name__ == "__main__" :
string = "SEEquoiaL"
check(string)
Q2
setc1 = set(["green", "blue"])
setc2 = set(["blue", "yellow"])
print("Original sets:")
print(setc1)
print(setc2)
setc = setc1.union(setc2)
print("\nUnion of above sets:")
print(setc)
setn1 = set([1, 1, 2, 3, 4, 5])
setn2 = set([1, 5, 6, 7, 8, 9])
print("\nOriginal sets:")
print(setn1)
print(setn2)
print("\nUnion of above sets:")
setn = setn1.union(setn2)
print(setn)
Q3
A = {2, 3, 5, 4}
B = {2, 5, 100}

LIL’JERRY
PYTHON ASSIGNMENT NO.4

C = {2, 3, 8, 9, 10}

print(B.intersection(A))
print(B.intersection(C))
print(A.intersection(C))

print(C.intersection(A, B))
Q4
setn = {5, 10, 3, 15, 2, 20}
print("Original set elements:")
print(setn)
print(type(setn))
print("\nMaximum value of the said set:")
print(max(setn))
print("\nMinimum value of the said set:")
print(min(setn))
Q5
setc1 = set(["green", "blue"])
setc2 = set(["blue", "yellow"])
print("Original sets:")
print(setc1)
print(setc2)
r1 = setc1.symmetric_difference(setc2)
print("\nSymmetric difference of setc1 - setc2:")
print(r1)
r2 = setc2.symmetric_difference(setc1)
print("\nSymmetric difference of setc2 - setc1:")
print(r2)
setn1 = set([1, 1, 2, 3, 4, 5])
setn2 = set([1, 5, 6, 7, 8, 9])
print("\nOriginal sets:")
print(setn1)

LIL’JERRY
PYTHON ASSIGNMENT NO.4

print(setn2)
r1 = setn1.symmetric_difference(setn2)
print("\nSymmetric difference of setn1 - setn2:")
print(r1)
r2 = setn2.symmetric_difference(setn1)
print("\nSymmetric difference of setn2 - setn1:")
print(r2)

Q6
set1 = set()
set1.add(8)
set1.add(9)
set1.add((6, 7))
print("The length of set is:", len(set1))
SET C
Q1
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
print("Union of E and N is",E | N)
print("Intersection of E and N is",E & N)
print("Difference of E and N is",E - N)
print("Symmetric difference of E and N is",E ^ N)
Q2
import copy
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
print("Old list:", old_list)
print("New list:", new_list)

LIL’JERRY

You might also like