Tamilnadu Samacheer Kalvi 12th Computer Science Notes Chapter 9 Lists, Tuples, Sets and Dictionary Notes

Four collections of data types in python programming language:
Python programming language has four collections of data types such as List, Tuples, Set and Dictionary.

List in Python:

  • A list in Python is known as a “sequence data type” like strings.
  • It is an ordered collection of values enclosed within square brackets [ ].
  • Each value of a list is called as element. It can be of any type such as numbers, characters, strings and even the nested lists as well.
  • Values in a list can be changed

Tuple:

  • Tuple consists of a number of values separated by comma and enclosed within parentheses even defined without parenthesis
  • Values in a tuple cannot be changed.

“Singleton” Tuple:
Creating a Tuple with one element is called “Singleton” tuple.

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.

Set operations:

  • The python supports the set operations such as
  • Union, Intersection, difference and Symmetric difference.

Union:

  • Union includes all elements from two or more sets.
  • In python, the operator | (pipe line) is used to union of two sets.
  • The function union() is also used to join two sets in python.

Example:
Program to Join (Union) two sets using union operator and union function
set_A={2,4,6,8} set_B={‘A’, ‘B’, ‘C’, ‘D’}
U_set=set_A | set_B
print(U _set)
set_ U=set_ A.union(set_ B)
Output:
{2, 4, 6, 8, ‘A’, ‘D’, ‘C’, ‘B’}
{‘D’, 2, 4, 6, 8, ‘B’,’C’,’A’}

Intersection:

  • Intersection includes the common elements in two sets.
  • The operator & is used to intersect two sets in python.
  • The function intersection() is also used to intersect two sets in python.

Example:
Program to insect two sets using intersection operator and intersection function
set_A={/A/, 2, 4, ‘D’}
set_B={‘A’, ‘B’, ‘C, ‘D’}
print(set_A&set_B)
print( set_A.intersection( set_B)
output:
{‘A’,’D’}
{‘A’, ‘D’}

Difference:

  • Difference includes all elements that are in first set (say set A) but not in the second set (say set B).
  • The minus (-) operator is used to difference set operation in python.
  • The function difference() is also used to difference operation.

Example:
Program to difference of two sets using minus operator and difference function set_A={/A’/ 2,4, ‘D’} setJB={‘A’, ‘B’, ‘C, ‘D’} print(set_A – set B) print(set_A.difference( set_B))
Output:
{2,4}
{2,4}

Symmetric difference:

  • Symmetric difference includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.
  • The caret (^) operator is used to symmetric difference set operation in python.
  • The function symmetric_difference( ) is also used to do the same operation.

Example:
Program to symmetric difference of two sets using caret operator and symmetric difference function
set_A={‘A’, 2,4, ‘D’} set_B={/A// ‘B’, ‘C, ‘D’}
print(set_A A set_B)
print( set_A.symmetric_difference(set_B))
Output:
{2,4,’B’,’C’}
{2,4,’B’,’C’}

Dictionary:

  • Dictionary is a mixed collection of elements.
  • Unlike other collection data types such as a list or tuple, the dictionary type stores a key along with its element.
  • The keys in a Python dictionary is separated by a colon (:) while the commas work as a separator for the elements.
  • The key value pairs are enclosed with curly braces {}.

Function :copy ()
Description : Returns a copy of the list Syntax : List.copy()

Function :count ()
Description : Returns the number of similar elements present in the last.
Syntax : List.count(value)

Function index ()
Description : Returns the index value of the first recurring element
Syntax : List, index (element)

Function : reverse ()
Description: Reverses the order of the element in the list.
Syntax : List.reverse()

Function: sort ()
Description : Sorts the element in list Syntax : List.sort(reverse=True | False, key=myFunc)

Samacheer Kalvi 12th Computer Science Notes

Leave a Reply

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