CS Knowledge Opener - Logo
CS Knowledge
Opener
Facebook Youtube Telegram
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
CS Knowledge Opener - Logo
CS Knowledge
Opener
Facebook Youtube Telegram
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
12th CS English Medium

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

Baskaran J
March 1, 2023 3 Mins Read
2.9K Views
0 Comments

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}

 

Tags:

Practical

Share Article

Follow Me Written By

Baskaran J

Other Articles

Previous

Puducherry Police Constable and Driver Grade III PST-PET date announced 2023

Next

Practical Program Exercise 5 Display a string elements using class for 12th Computer Science

Next
March 1, 2023

Practical Program Exercise 5 Display a string elements using class for 12th Computer Science

Previous
March 1, 2023

Puducherry Police Constable and Driver Grade III PST-PET date announced 2023

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

1 × 1 =

Related Posts

12th CS Board Exam Answer Key Discussion 2024-2025

Baskaran J
March 14, 2025

12th Computer Science English medium Program Question 2024-2025

Baskaran J
February 23, 2025

12th Computer Science Practice One marks question 2024-2025

Baskaran J
February 3, 2025

12th Computer Science English medium Public Exam Important Important Question 2024-2025

Baskaran J
January 16, 2025

Subscribe to our newsletter and stay updated.

CS Knowledge Opener

CS Knowledge Opener focuses on 11th &12th Computer Science & Computer Applications English Medium & Tamil Medium. Programming Skills and also we are Publishing Study Materials Based on New syllabus introduced by TNSCERT. (TNBOARD).

Quick Links

  • Contact Us
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms and Conditions
Categories
11th CA English Medium 15
11th CA Tamil Medium 9
11th CS English Medium 21
11th CS Tamil Medium 12
12th CA English Medium 32
12th CA Tamil Medium 8
12th CS English Medium 36
12th CS Tamil Medium 12
Government Exams 36
Notifications 8
Other Subjects 13

Follow Us

Youtube
Facebook
Telegram
CS Knowledge Opener © 2020 - 2025. All Rights Reserved.