Practical 2: Common Python Data Structures and Functions

Data Types

Strings

   1 >>> s_1 = 'another string'   #create string
   2 >>> s_2 = s_1[:7]            #create new string of characters 0 to 7 in s_1
   3 >>> s_2
   4 'another'
   5 >>> if s_2 in s_1:          #check for membership of s_2 in s_1
   6         print 'true'
   7 
   8 true
   9 >>> type(s_1)
  10 <type 'str'>

Numerical types

   1 >>> n_int = 135
   2 >>> n_int
   3 135
   4 >>> n_float = 10e-10
   5 >>> n_float
   6 1e-09

   1 >>> type(n_int)
   2 <type 'int'>
   3 >>>type(n_float)
   4 <type 'float'>

   1 >>> n_int2float=float(n_int)
   2 >>> n_int2float
   3 135.0
   4 >>> type(n_int2float)
   5 <type 'float'>   #n_int is still an integer

Boolean

   1 >>> val=True
   2 >>> if val:
   3         print 'val is true'
   4 
   5 'val is true'
   6 
   7 >>> val=False
   8 >>> if val:
   9         print 'val is true'
  10 
  11 >>>

Lists (and tuples)

   1 >>> empty_list=[]

   1 >>> empty_list.append('string 1')
   2 >>> empty_list
   3 ['string 1']

   1 >>> empty_list=['string 1']
   2 >>> empty_list
   3 ['string 1']

   1 >>> empty_list = ['string 1','string 2','string 3']
   2 >>> empty_list[0]
   3 'string 1'
   4 >>> empty_list[1]
   5 'string 2'
   6 >>> empty_list[2]
   7 'string 3'
   8 >>> empty_list[-1]
   9 'string 3'

   1 >>> empty_list.remove('string 1')
   2 >>> empty_list
   3 ['string 2', 'string 3']

   1 >>> empty_list[1:3]
   2 ['string 3']

   1 >>> empty_list.index('string 2')
   2 0

   1 >>> empty_list = ['string 1','string 2','string 3']
   2 >>> nested = [1,2,3]
   3 >>> empty_list.append(nested)
   4 >>> empty_list
   5 ['string 1', 'string 2', 'string 3', [1, 2, 3]]
   6 >>> empty_list[3]
   7 [1, 2, 3]
   8 >>> empty_list[3][0]
   9 1

Dictionaries

   1 >>> dict_1 = {'alfa':1,'beta':2}  #create a dictionary
   2 >>> keys = ['alfa','beta']
   3 >>> vals = [1,2]
   4 >>> dict_1 = dict(zip(keys,vals)) #create a dictionary from two lists
   5 >>> dict_1
   6 {'alfa':1,'beta':2}
   7 >>> dict_1['alfa']                #access value '1' by key 'alfa'
   8 1
   9 >>> dict_1.has_key('beta')        #check that dict_1 has key 'beta'
  10 True
  11 >>> dict_1.keys()                 #print keys of dict_1
  12 ['alfa','beta']
  13 >>> dict_1.values()               #print values
  14 [1,2]
  15 >>> dict_1['gamma'] = 1          #add new key:value pair
  16 >>> dict_1['alfa'] = 'a'         #overwrite key:value pair

   1 >>> dict_1 = {'alfa':1,'beta':2} #define first dictionary
   2 >>> dict_2 = {'gamma':1}         #define second dictionary
   3 >>> dict_1['nested'] = dict_2    #add dict_2 as value to key 'nested' in dict_1
   4 >>> dict_1
   5 {'beta': 2, 'alfa': 1, 'nested': {'gamma': 1}}
   6 >>> dict_1['nested']['gamma']    #access key 'gamma' in nested dictionary
   7 1

Built-in functions, loops, conditionals, assignment, and evaluation

   1 >>> a_list = ['a','b','c']
   2 >>> len(a_list)
   3 3

   1 >>> dir(dict_1)  # list attributes of dict_1
   2 ['__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']

   1 >>> range(0,10)
   2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
   3 >>> range(10)
   4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

   1 >>> range(0,10,2)
   2 [0, 2, 4, 6, 8]

   1 >>> from numpy import arange
   2 >>> arange(10)
   3 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
   4 >>> arange(0,5,0.5).tolist()
   5 [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

   1 >>> a_list = ['a','b','c']
   2 >>> for item in a_list:       #iterating over the items
   3         print item

   1 >>> a_list = ['a','b','c']
   2 >>> i = 0                  #assign value 0 to variable i
   3 >>> while i<len(a_list):   #as long as i is less than 3
   4         print a_list[i]    #print item at index i in a_list
   5         i += 1             #increment i by 1
   6 
   7 a
   8 b
   9 c
  10 
  11 
  12 
  13 >>> for i in range(len(a_list)):   #iterating over indices
  14         print a_list[i]
  15 
  16 a
  17 b
  18 c

   1 >>> a_list = ['a',1,'c',2]
   2 >>> for item in a_list:
   3         if type(item)==type(str()):  #check if item is of string type
   4             print item+' is string'
   5         else:                        #if not...
   6             print str(item)+' is not string'
   7 
   8 'a is string'
   9 '1 is not string'
  10 'c is string'
  11 '2 is not string'

   1 >>> for item in a_list:
   2 
   3         if type(item)==type(str()):
   4             print item+' is string'
   5         elif type(item)==type(int()):
   6             print str(item)+' is int'
   7         else:
   8             print str(item)+' is unkown'
   9 
  10 'a is string'
  11 '1 is int'
  12 'c is string'
  13 '2 is int'

   1 >>> a = 'string' #assignment
   2 >>> a=='string'  #evaluation, is a equal to 'string'?
   3 True
   4 >>> a!='string'  #evaluation, is a not equal to 'string'?
   5 False

None: Meetings/C1netWork4/Prac2 (last edited 2018-01-16 08:33:31 by mark)