Tuesday, 28 May 2019

frozenset datatype


Frozenset

See the below examples:

 1.
>>> s={10,20,30,"hello"} #created set elements, where s is the variable (object reference) to it


>>>s=frozenset(s) # type casting from set into frozenset
>>>print(s)
20,'hello',10,30 # frozen set is unordered list


2. >>> b={10,20,30,"hello"} # set b is created
>>> b
{10, 'hello', 20, 30} # set b is displayed
>>> b=frozenset(b) # type casting from set b to frozenset b
>>> b #frozenset b is displayed
frozenset({10, 'hello', 20, 30})

3. b=frozenset({10,20,30,"hello"}) # frozenset b is created

4. s={10,2.5,True,"Ram",2+3j} # same different types
5. s[0] # Error No slicing operation
6. We can not modify as frozen set is immutable so can not add and remove
>>> s={10,20,,30}
>>>s=frozenset(s)
>>>s.add(40)#error
s.remove(10)#error

5. Accessing Set elements:
>>> s={"app","bana","cherry"}
>>> s=frozenset(s)
>>> for i in s: print(i)
App bana cherry
>>> 
>>> s={"app","bana","cherry"}
>>> for x in s:
        print(x)
output:
cherry
bana
app



Now we will understand the definitoin: 


Definition:


The frozenset datatype is same as set data type. The main difference is that elements in the set datatype can be modified whereas, the elements of frozenset can not be modified.

Creating frozenset data type:

We can create frozenset data type in two ways
1. By typecasting
2. Bypassing elements to the frozenset function 

1. By typecasting

s={10,20,30,"hello"} #created set elements, where s is the variable (object reference) to it
s=frozenset(s) # type casting from set into frozenset

Displaying the frozenset elements:
>>> s
frozenset({'hello', 10, 20, 30})
>>> print(s)
frozenset({'hello', 10, 20, 30})
>>> 

2. By passing elements to the frozenset function 

Another way of creating frozenset is by passing elements to the frozenset function.
a=frozenset({10,20,True,2+3j})
>>> type(a)
<class 'frozenset'>
>>> 


Output: Random output as it is unordered
frozenset({10, 'hello', 20, 30})





Ex 1:
Unordered List
>>> s=frozenset({10,20,,30,"hello"})
>>> s
Frozenset({'hello', 10, 20, 30})

Ex2: Different type of data
s=frozenset({10,2.5,True,"Ram",2+3j})
>>> s
{True, 2.5, 10, (2+3j), 'Ram'}
>>> 

Ex3: Index concept is not available thus it is unordered set and slicing operations
>>> s={10,20,30}
>>>s[0] #Error as no index
>>>s[0:2] #Error no slicing operation
Ex 4: How can we access each element in a set?
>>> s=frozenset({2,5,7,8,9,10})
>>> for i in s:
        print(i)
2 5 7 8 9 10
Ex 5: How do we check one item in set
B=frozenset({10,20,30})
print(10 in b)
True
Ex4: we cant not modify
>>> s=frozenset({10,20,30})
>>>s.add(40)#error
>>>s.remove(10) #error
Ex5: Duplication is not allowed
b=frozenset({10,20,10,20})
>>> b
frozenset({10, 20})
>>> 

Exercise:
find out the output:
>>> a={1,2,3}
>>> b=frozenset([3,4,5])
>>> a-b
{1, 2}


Next topic: range data type
Previous topic: Set data type


No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...