Programming Examples

Python Program that generate a set of prime numbers and another set of even numbers


Python Program that generate a set of prime numbers and another set of even numbers. Demonstrate the result of union, intersection, difference and symmetirc difference operations.

Solution

even=set([x*2 for x in range(1,11)])
primes=set()
for i in range(2,20):
	j=2
	f=0
	while j<i/2:
		if i%j==0:
			f=1
		j+=1
	if f==0:
		primes.add(i)
print("Even Numbers: ", even)
print("Prime Numbers: ", primes)
print("Union: ", even.union(primes))
print("Intersection: ", even.intersection(primes))
print("Difference: ", even.difference(primes))
print("Symmetric Difference: ", even.symmetric_difference(primes))
Output

Even Numbers: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Prime Numbers: {2, 3, 4, 5, 7, 11, 13, 17, 19}

Union: {2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20}

Intersection: {2, 4}

Difference: {6, 8, 10, 12, 14, 16, 18, 20}

Symmetric Difference: {3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20}

CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad