5. Python Data Types List, Tuple, Set, Frozen Set



Now we are going in-depth on fundamental data types.

Ex        a =10              # here 'a' is the int type

            b = 10.5          # 'b' is the float type

            c= 10+20j      # 'c' is the complex type.

            d = True         # 'd' is the bool type

            e = ‘srinu’      # 'e' is the string or str type.

These all are having only one value, 'a' is represent 10 and 'b' represents 10.5 like, so in fundamental data types, every variable can only hold one object of data or value.  But if we try to create the program we required multiple values or groups of values.

Ex: all ration cardholder names in distributor or age groups or all area names under the distributor like. But our basic fundamental data types holding only one value. So that we need collection-related data types. If we want to represent a group of data as a single value Ex: (10,20,30,40). These are called collection-related data types. Now we have to check various types of related data types.

Ex:

1.       list

2.      tuple

3.      set

4.      forzenset

5.      dict (dictionary)

6.      range

7.      bytes

8.     bytearray.

 

List:

Important rules here are, it’s should on order type, which means (1,2,3,4) like and duplicates values are allowed. List' concept is very simple, we want to go shopping then we take one paper and write like one is so and so and  second is so and so like that, we wrote in the white paper.

List type syntax look like [x,x,x,x,x,x,x,x]

Example:

list= [10,20,30]

list1= [10,20,30,'srinu',True,10+20j]

list2= [10,20,30,40,10,30,'srinu',True,10+20j]

print (list)

print (list1)

print (list2)

print(type(list))

[10, 20, 30]  #result 

[10, 20, 30, 'srinu', True, (10+20j)]

[10, 20, 30, 40, 10, 30, 'srinu', True, (10+20j)]

<class 'list'>

Now we can find it’s duplicate values are allowed, all fundamental dates are allowed and what we give the list of data output also same as given values.

Some important points of list data type:

1.       Order is preserved in a memory location.

2.      Duplicate objects are allowed.

3.      Syntax looks like [x,x,x,x,x]

4.      Heterogeneous objects are allowed. Heterogeneous means all types of fundamental data types. 

5.      Order concept is there so we can apply it to the index and the slicing also accepted.

Ex:      list= [10,20,30,40,10,30,'srinu',True,10+20j]

             print (list[3])

print (list[2:5])

 40    #Result

[30, 40, 10]    #Result

6.      List is grow able in nature

Ex:  list= []  # take a empty list.

print (type(list))  # Result <class 'list'>

 list.append(10)  # add 10 value into  list

list.append(20)  # add 20 value into  list

list.append(30)  # add 30 value into  list

list.append(40)  # add 40 value into  list

print (list)           # Result [10, 20, 30, 40]

list.remove(20)  # remove 20 value into  list

list.remove(30)  # remove 20 value into  list

print (list)     # Result [10, 40]

List is mutable.

Ex:

list= [10,20,30,40]  #take a list

print(list)

list[2]=56      #trying to change second index

print(list)

[10, 20, 30, 40]  #result

[10, 20, 56, 40]  #result

Tuple:

The tuple is all most all same as a list, except that it is immutable (unchangeable). Once create the tuple object we can’t perform any changes.

This is the read-only version of the list type.

List type syntax look like (x,x,x,x,x,x,x,x)

Example:

item= (10,20,30)

item 1= (10,20,30,'srinu',True,10+20j)

item 2= (10,20,30,40,10,30,'srinu',True,10+20j)

print (item)

print (item 1)

print (item 2)

print(type(item))

(10, 20, 30) #result 

(10, 20, 30, 'srinu', True, (10+20j))

(10, 20, 30, 40, 10, 30, 'srinu', True, (10+20j))

<class 'tuple'>

Now we can find its duplicate values are allowed, all fundamental dates are allowed and what we give the list of data output also same as given values.

Some important point of list data type:

1.       Order is preserved in a memory location.

2.      Duplicate objects are allowed.

3.      Syntax looks like (x,x,x,x,x)

4.      Heterogeneous objects are allowed. Heterogeneous means all fundamental data types.           

5.     Order concept is there so we can apply the index and the slicing.

Ex:      item= (10,20,30,40,10,30,'srinu',True,10+20j)

             print (item [3])

print (item [2:5])

  40  #Result 

(30, 40, 10)   

6.      'Tuple' is not growable in nature, once we created we the tuple object we can’t perform any changes.

Ex:  list= ()  # take a empty tuple.

print (type(list))  # Result <class 'tuple'>

7.      Tuple is immutable.

Ex:

list= (10,20,30,40)  #take a list

print(list)

(10, 20, 30, 40)  #result

list[2]=56      #trying to change second index

print(list)

TypeError: 'tuple' object does not support item assignment   #result

8.     Tuple is a read-only version of the list.

 Difference between List and Tuple

LIST

TUPLE

1. Mutable

1. Immutable

2.  Syntax Is [X,X,X]

2. Syntax Is (X,X,X)

3. More Memory

3. Less Memory

4. Performance is noticeable

4. Performance is fast

Ex: Text filled box in the form

Ex: married or unmarried box in the form.



Set:

Before we discuss the list and tuple concept, the list and the tuple are order-wised, and duplicates are allowed, but the small difference on both, the list is mutable or changeable and tuple is immutable or unchangeable. But sometimes our requirements are, we want to represent a group of values as a single value, while duplicates are not allowed, order-wised not required.

Example:

Massage box: it sends all my contents without any order-wised, id any duplicate contact is there then message not sent.

This type of requirements on our application, then we use to set data type.

In the 9,10 academic study classes we have already learned like below

section = {1,2,3}, section_2 = {2,3,1}, section_3 = {3,1,2} all are equal. In 'set', order wise not applicable. Same way in python, if we don’t want order type, we don’t want duplicates then go for the set data type.

Set datatype syntax looks like {x,x,x,x,x,x}

Ex:

section_numbers = {1,2,3,4} and we can check, on python what type is it.

section_numbers = {1,2,3,4}

print (type (section_numbers))

<class 'set'>     #Result 

One more example

setOfNumbers = {10,20,'srinu',30,40,20}

print(setOfNumbers)

{40, 10, 'srinu', 20, 30}  #Result 

The output shows that it’s not in order wise and duplicates are removed. So this example we can’t guarantee the output always in order wise or not.so if order-wise not in the set then it’s not applicable to indexing and slicing.

Some important notes in set data type:

1.      Duplicated are not allowed

2.     Order-wise output not came.

3.     Set syntax is {x,x,x,x,x}

4.   If the output object not in order wise then indexing and the slicing concept not applicable.

5. Heterogeneous objects are allowed. Heterogeneous means all types of fundamental data types. 

6.     Set is growable in nature.

7.      Set is mutable or changeable.

8.  In 'set' data type (.append) not applicable, for that we use (.add) function. The reason is (.append) function only adds the value if a group of values in a single data type is in memory location order-wise type, then the (.append) function works. If a group of values in a single data type is in memory location unordered-wise type then (.add) function works. And (.remove) function we can use both scenarios.

9.     If we create a new empty set than

x = set() #valid empty set

x = {}  #invalid set, its  empty dict.

 

Difference between List and Set

LIST

Set

1. orderwise data output

1. unorderwise data output

2.  Syntax Is [X,X,X]

2. Syntax Is {X,X,X}

3. duplicates are allowed

3. duplicates are  not allowed

4. Text filled box in the form

4. Massage box: it sends all my contents without any order-wised, id any duplicate contact is there then message not sent.



Frozen set:

Before we cover the Set is un-orderwise type, duplicates are not allowed. Frozen set is exactly same but it’s immutable.

forzenset data type syntax looks like

Ex:

section_numbers = {1,2,3,4}  

section_numbers = (frozenset (section_numbers)) # now set convert to frozenset

and we can check, on python what type is it.

setOfNumbers = {1,2,3,4} # we taking set

setOfNumbers= (frozenset(setOfNumbers)) # now set convert to frozenset

print(type (setOfNumbers))

<class 'frozenset'> #Result 

One more example

setOfNumbers = {10,20,'srinu',30,40,20}

setOfNumbers= (frozenset(setOfNumbers))

print(setOfNumbers)

frozenset({20, 'srinu', 40, 10, 30})#Result 

Output shows that, it’s not in order wise and duplicates are removed. So this example we can’t guarantee the output always in order wise or not.so if order wise not in set than it’s not applicable to indexing and slicing.

Some important notes in set data type:

1.      Duplicated are not allowed

2.     Order-wise output not came.

3.     Frozen Set syntax takes a set syntax and we need to apply  to frozen set syntax Ex: frozenset({20, 'srinu', 40, 10, 30})

4.   If the output object not in order wise then indexing and the slicing concept not applicable.

5. Heterogeneous objects are allowed. Heterogeneous means all types of fundamental data types. 

 Difference between Set and frozen set

set

frozen set

1. Syntax Is {X,X,X}

1. Syntax Is f_s({X,X,X})

2.  Set is mutable or changeable.

2. Set is immutable or unchangeable.

 

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post