# Practical Program Exercise 4 Generate prime numbers and set operations for 12th Computer Science

# Generate prime numbers and set operations

## Sets

- Set is a mutable and an unordered collection of elements without duplicates.
- That means the elements within a set cannot be repeated.
- This feature used to include membership testing and eliminating duplicate elements.

## Creating a Set

- A set is created by placing all the elements separated by comma within a pair of curly brackets.
- The set( ) function can also used to create sets in Python.

## Creating Set using List or Tuple

- A list or Tuple can be converted as set by using set( ) function.
- This is very simple procedure.
- First you have to create a list or Tuple then, substitute its variable within set( ) function as argument.

## Set Operations

- The python is also supports the set operations such as Union, Intersection, difference and Symmetric difference.

### Union:

- It includes all elements from two or more sets

### Intersection:

- It includes the common elements in two sets
- Th e operator & is used to intersect two sets in python.
- The function intersection( ) is also used to intersect two sets in python.

### Difference

- It includes all elements that are in first set (say set A) but not in the second set (say set B)

### Symmetric difference

- It includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.

### Note:

- When you print the elements from a set, python shows the values in different order.

## Prime Numbers:

- Prime number can be divisible by single factor
- 2,3,5,7 example for prime numbers

## for loop

- for loop is the most comfortable loop. It is also an entry check loop.
- The condition is checked in the beginning and the body of the loop(statements-block 1) is executed if it is only True otherwise the loop is not executed.
- The counter_variable mentioned in the syntax is similar to the control variable that weused in the for loop of C++ and the sequence refers to the initial, final and increment value.
- Usually in Python, for loop uses the range() function in the sequence to specify the initial, final

and increment values. - range() generates a list of values starting from start till stop-1.

### Syntax:

for counter_variable in sequence:

statements-block 1

[else: # optional block

statements-block 2]

### The syntax of range() is as follows:

range (start,stop,[step])

Where,

start – refers to the initial value

stop – refers to the final value

step – refers to increment value, this is optional part.

## PY 4 – GENERATE PRIME NUMBERS AND SET OPERATIONS

### QUESTION:

- Write a Program that generates a set of prime numbers and another set of odd numbers.
- Display the result of union, intersection, difference and symmetric difference operations.

### AIM:

- To generate a set of prime numbers and another set of odd numbers, and to display the result of union, intersection, difference and symmetric difference operations.

### CODING:

odd=set([x*2+1 for x in range (0,5)])

primes=set()

for i in range(2,10):

j=2

f=0

while j<=i/2:

if i%j==0:

f=1

j+=1

if f==0:

primes.add(i)

print(“odd number:”, odd)

print(“prime number:”, primes)

print(“union:”, odd.union(primes))

print(“intersection:”, odd.intersection(primes))

print(“difference:”, odd.difference(primes))

print(“symmetric difference:”, odd.symmetric_difference(primes))

### OUTPUT:

odd number: {1, 3, 5, 7, 9}

prime number: {2, 3, 5, 7}

union: {1, 2, 3, 5, 7, 9}

intersection: {3, 5, 7}

difference: {1, 9}

symmetric difference: {1, 2, 9}