Search This Blog

Thursday, March 5, 2015

Data Types: Tuples, Sets, Dictionaries

Tuples:


Tuples are similar to lists but they are immutable. We can convert as list to tuple ans vice versa.

>>> mytuple=tuple(["Harry Potter",25,100,3])
>>> mytuple

('Harry Potter', 25, 100, 3)

Note that tuple has () instead of a list's []

>>> moviename,runningTime,upVotes,downVotes=mytuple
>>> moviename
'Harry Potter'
>>> runningTime
25
>>> upVotes
100
>>> downVotes

3


Sets:

Sets are unordered collection of unique objects.

List to a set: myset = set(moviename)
Set to list: mylist = list(myset)

>>> a=set([1,2,3,3,2])
>>> a

set([1, 2, 3])

Sets have only unique items

>>> b=set([3,4,5])
>>> b
set([3, 4, 5])
>>> a|b

set([1, 2, 3, 4, 5])

Intersection of sets

>>> a&b
set([3])

>>> 


>>> a-b

set([1, 2])
>>> b-a
set([4, 5])

>>> 

Sets are useful in finding different unique values


Dictionaries:


Dictionaries are unordered key-value pairs

a={}
Empty dictionary

>>> a={"Movie":"Harry Potter", "sitCom":"Big bang theory"}
>>> print a
{'Movie': 'Harry Potter', 'sitCom': 'Big bang theory'}










No comments:

Post a Comment