Feb 21, 2015

Textual description of firstImageUrl

Python data structure - Non sequence types

In the previous post we discussed about sequence types data structure. Here we will be discussing about non sequence types in built data structure of python. Set, frozenset and dict classes are  non -sequence types in python.

set class:

1. Sets are unordered collections of objects and without duplicates.
2. No inherent order for objects is maintained. i.e: Order of object is not important.
3. Since it is based on a data structure known as a hash table so it has a highly optimized method for
    checking whether a specific element is contained in the set. 
4. Only instances of immutable types can be added to a Python set.
5. Sets are mutable data structure. 
Question : Why "set of lists" and "set of sets" is not possible, only set of tuples is feasible ?
Answer: Since only instances of immutable types can be added to a Python set so set of lists and set of sets are not feasible as sets and lists are mutable. However, tuples are immutable so set of tuples can be created.
Please note, objects such as integers, floating-point numbers, and character strings are eligible to be elements of a set. Since they are also immutable.
6. Python uses curly braces { and } as delimiters for a set.
Note: { } does not represent an empty set; it represents an empty dictionary.
   {17}, {'red','green'} are examples of set.
7. If an iterable parameter is sent to the constructor, then the set of distinct elements is produced.
    For example, set( hello ) produces set(['h', 'e', 'l', 'o']).  # Duplicates are not allowed in set

dict class:

In python dict class is represented by dictionary. Dictionary is a collection of key/value pairs. 
1. Keys are unique for the dictionary and only immutable objects (like strings) for the keys is 
    allowed. Values can be immutable or mutable. 
2. Order is not important in dictionary. 
3. An empty dictionary can be  created like dic_one = { } # An empty dictionary
ad_book = {'nikhil' : 'nikhilranjan@gmail.com', 'ranjan': 'ranjan@hotmail.com' }
    # An dictionary of name as Key and emil as  value 

4. Add and remove Key/value pair in dictionary :
    # Adding a key-value pair
       ad_book ['Rahul'] = 'rahulk@hotmail.com'  #Rahul is added
>>> print ad_book
{'ranjan': 'ranjan@hotmail.com', 'nikhil': 'nikhilranjan@gmail.com', 'Rahul': 'rahulk@hotmail.com'}
   # Deleting a key-value pair
      del ad_book ['Nikhil']
>>> print ad_book  # nikhil is deleted 
{'ranjan': 'ranjan@hotmail.com', 'Rahul': 'rahulk@hotmail.com'}

5. List of dictionary functions/methods are as follows :

Previous : Python data structure - sequence type Next: Functions in python
Location: Hyderabad, Telangana, India