Learning Python: Different between string, list, tuples, dictionary and set


String

Immutable. No - (),[] or {}. String can be confusing with tuple. As more of a set of string being added to variable definition, it turns into a tuple.

Code
string_list = 'one'
#string_list = 'one','two'  #this is a tuple now.
print string_list

type(string_list)
dir(string_list)

Output
one
<type 'str'>

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']




List

List is mutable.



Code

list=[2,3,5,7,11,13]
print list[:2]
print type(list)
print dir(list)


Output

<type 'list'>

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

(2, 3)





Some of the more obvious methods inside the list are "append", "pop", "remove" and etc.


Tuple

Tuple is immutable. 


Code

tuple=(2,3,5,7,11,13)
print tuple[:2]
print type(tuple)
print dir(tuple)



Output

<type 'tuple'>

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']



Possible usage is, sometimes, developer would like to protect the data structure and this can be easily done with changing [..] to (..)

Are list and tuple the same?


Code

print "The same", list is tuple
print "The same", list == tuple



Output

The same False

The same False




Attempting to change the data within List and Tuple




Code


list_n = [5, 3, 1, -1, -3, 5]
list_n[0] = 0
print list_n
  

Output

[0, 3, 1, -1, -3, 5]


Code


tuple_n = (5, 3, 1, -1, -3, 5)
tuple_n(0) = 0

print tuple_n
  

Output


    tuple_n(0) = 0
SyntaxError: can't assign to function call

Set 

No duplicate records.
set_list = {we,we,re,te}

Dictionary

Pair between key and values.

Code

print "#### Output of Dictionary #### "
Dictionary = dict()
Dictionary['one'] = 1
Dictionary['two'] = 2
Dictionary['three'] = 3
print Dictionary
print "print out the value", Dictionary['two']
print "print keys " , Dictionary.keys()
print "print values " , Dictionary.values()
print "print items ", Dictionary.items()
print "print sorting ", sorted(Dictionary.items()) #this is a weird way to sort comparing to list sort
print "print lenght " , len(Dictionary.items()) #another strange way to call function instead of method to find length

#print "print out the index of 2". Dictionary[2]
print type(Dictionary)
print dir(Dictionary)

print " #### End of Dictionary #### "




Output
#### Output of Dictionary #### 
{'three': 3, 'two': 2, 'one': 1}
print out the value 2
print keys  ['three', 'two', 'one']
print values  [3, 2, 1]
print items  [('three', 3), ('two', 2), ('one', 1)]
print sorting  [('one', 1), ('three', 3), ('two', 2)]
print lenght  3
<type 'dict'>
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
 #### End of Dictionary ####