0
Data Structures in Python                                   Crash Course                                 Dr.Haitham A. El-...
Agenda	      •    Whe*ng	  Your	  Appe0te	      •    Using	  Python	  Interpreter	      •    Informal	  Introduc0on	  to	 ...
Python	      •  Easy	  to	  learn,	  powerful	  programming	         language.	      •  Has	  efficient	  high-­‐level	  dat...
Python	      •    Simpler	  to	  use	      •    Available	  on	  Windows,	  Mac	  OS	  X,	  and	  Unix	      •    Help	  y...
Python	      •  Python	  is	  extensible:	  if	  you	  know	  how	  to	         program	  in	  C	  it	  is	  easy	  to	  a...
Using	  the	  Python	  Interpreter	      •  Python	  interpreter	  is	  usually	  installed	  as	  /usr/       local/bin/p...
Using	  Python	  Interpreter	      •  Argument	  Passing	      •  Interac0ve	  Mode	      •  Error	  Handling	  Dr.Haitham...
Executable	  Python	  Scripts	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012 ...
Source	  Code	  Encoding	      •  It	  is	  possible	  to	  use	  encodings	  different	  than	         ASCII	  in	  Python...
Informal	  Introduc0on	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   10 / 61
Using	  Python	  as	  a	  Calculator	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9...
Numbers	  Dr.Haitham A. El-Ghareeb (CIS)    Data Structures and Algorithms - 2012   December 9, 2012   12 / 61
Strings	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   13 / 61
Strings	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   14 / 61
String	  Concatena0on	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   15 / 61
String	  Indexing	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   16 / 61
String	  Slicing	  Dr.Haitham A. El-Ghareeb (CIS)       Data Structures and Algorithms - 2012   December 9, 2012   17 / 61
Strings	  are	  Immutable!	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   1...
Indices	  May	  be	  Nega0ve	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012  ...
How	  Come?!	  Dr.Haitham A. El-Ghareeb (CIS)      Data Structures and Algorithms - 2012   December 9, 2012   20 / 61
Lists	      •  compound	  data	  type	      •  used	  to	  group	  together	  other	  values	  	      •  The	  most	  vers...
Shallow	  Copy	  Dr.Haitham A. El-Ghareeb (CIS)       Data Structures and Algorithms - 2012   December 9, 2012   22 / 61
First	  Steps	  toward	  Programming	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9...
More	  Control	  Flow	  Tools	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012 ...
If	  Statement	  Dr.Haitham A. El-Ghareeb (CIS)      Data Structures and Algorithms - 2012   December 9, 2012   25 / 61
For	  Statement	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   26 / 61
For	  Statement	  with	  Shallow	  Copy	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   Decembe...
Range	  Func0on	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   28 / 61
Break,	  Con0nue,	  Else	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   29 ...
Pass	      •  Does	  Nothing!	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012 ...
Func0ons	  Dr.Haitham A. El-Ghareeb (CIS)    Data Structures and Algorithms - 2012   December 9, 2012   31 / 61
Return	  Statement	  	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   32 / 61
Default	  Argument	  Value	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   3...
Documenta0on	  String	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   34 / 61
Coding	  Style	  -­‐	  PEP8	      •  Use	  4-­‐space	  indenta0on,	  and	  no	  tabs.	      •  4	  spaces	  are	  a	  good...
Coding	  Style	  -­‐	  PEP8	      •  When	  possible,	  put	  comments	  on	  a	  line	  of	  their	  own.	      •  Use	  ...
Data	  Structures	  -­‐	  Lists	      •  list.append(x)	  Add	  an	  item	  to	  the	  end	  of	  the	  list.	      •  lis...
Data	  Structures	  -­‐	  Lists	      •  list.pop([i])	  Remove	  the	  item	  at	  the	  given	  posi0on	  in	         th...
Lists	  Example	  Dr.Haitham A. El-Ghareeb (CIS)       Data Structures and Algorithms - 2012   December 9, 2012   39 / 61
Using	  Lists	  as	  Stack	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   4...
Using	  List	  as	  a	  Queue	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012 ...
Func0onal	  Programming	  Tools	      •  There	  are	  three	  built-­‐in	  func0ons	  that	  are	  very	         useful	 ...
Filter()	      •  filter(func0on,	  sequence)	  returns	  a	  sequence	         consis0ng	  of	  those	  items	  from	  the...
Filter()	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   44 / 61
Map()	      •  map(func0on,	  sequence)	  calls	  func0on(item)	         for	  each	  of	  the	  sequence’s	  items	  and	...
Map()	      •  More	  than	  one	  sequence	  may	  be	  passed;	  the	         func0on	  must	  then	  have	  as	  many	 ...
Reduce()	      •  reduce(func0on,	  sequence)	  returns	  a	  single	         value	  constructed	  by	  calling	  the	  b...
Tuples	  and	  Sequences	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   48 ...
Comma	  at	  the	  end!	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   49 /...
Sets	      •  A	  set	  is	  an	  unordered	  collec0on	  with	  no	         duplicate	  elements.	  	      •  Basic	  use...
Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   51 / 61
Dic0onaries	      •  Dic0onaries	  are	  some0mes	  found	  in	  other	         languages	  as	  “associa0ve	  memories”	 ...
Dic0onaries	      •  You	  can’t	  use	  lists	  as	  keys,	  since	  lists	  can	  be	  modified	  in	         place	  usi...
Dic0onaries	      •  The	  main	  opera0ons	  on	  a	  dic0onary	  are	  storing	  a	  value	         with	  some	  key	  ...
Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   55 / 61
Dict()	  Constructor	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   56 / 61
Looping	  Techniques	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   57 / 61
Looping	  over	  Two	  Sequences	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 20...
Loop	  in	  Reverse	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   December 9, 2012   59 / 61
More	  on	  Condi0ons	      •  Include	  and,	  or	  Dr.Haitham A. El-Ghareeb (CIS)     Data Structures and Algorithms - 2...
Comparing	  Objects	  of	  Different	  Types	  Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   Dec...
Upcoming SlideShare
Loading in...5
×

Lecture 9 - DSA - Python Data Structures

1,709

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,709
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
89
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Lecture 9 - DSA - Python Data Structures"

  1. 1. Data Structures in Python Crash Course Dr.Haitham A. El-Ghareeb Information Systems Department Faculty of Computers and Information Sciences Mansoura University helghareeb@gmail.com December 9, 2012Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 1 / 61
  2. 2. Agenda   •  Whe*ng  Your  Appe0te   •  Using  Python  Interpreter   •  Informal  Introduc0on  to  Python   •  More  Control  Flows   •  Data  Structures   •  Modules  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 2 / 61
  3. 3. Python   •  Easy  to  learn,  powerful  programming   language.   •  Has  efficient  high-­‐level  data  structures  and  a   simple  but  effec0ve  approach  to  object-­‐ oriented  programming.   –  elegant  syntax     –  dynamic  typing   –  interpreted  nature  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 3 / 61
  4. 4. Python   •  Simpler  to  use   •  Available  on  Windows,  Mac  OS  X,  and  Unix   •  Help  you  get  the  job  done  more  quickly   •  Split  your  program  into  modules  that  can  be  reused   •  Python  is  an  interpreted  language:   –  Save  you  considerable  0me  during  program   development  because  no  compila0on  and  linking   is  necessary.     –  Interpreter  can  be  used  interac0vely  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 4 / 61
  5. 5. Python   •  Python  is  extensible:  if  you  know  how  to   program  in  C  it  is  easy  to  add  a  new  built-­‐in   func0on  or  module  to  the  interpreter,   •  Named  aSer  the  BBC  show  “Monty  Python’s   Flying  Circus”  and  has  nothing  to  do  with   rep0les.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 5 / 61
  6. 6. Using  the  Python  Interpreter   •  Python  interpreter  is  usually  installed  as  /usr/ local/bin/python   •  Invoking  the  Interpreter   –  A  second  way  of  star0ng  the  interpreter  is  python   -­‐c  command  [arg]  ...,  which  executes  the   statement(s)  in  command   –  Some  Python  modules  are  also  useful  as  scripts.   These  can  be  invoked  using  python  -­‐m  module   [arg]  ...,  which  executes  the  source  file  for  module  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 6 / 61
  7. 7. Using  Python  Interpreter   •  Argument  Passing   •  Interac0ve  Mode   •  Error  Handling  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 7 / 61
  8. 8. Executable  Python  Scripts  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 8 / 61
  9. 9. Source  Code  Encoding   •  It  is  possible  to  use  encodings  different  than   ASCII  in  Python  source  files.     •  The  best  way  to  do  it  is  to  put  one  more   special  comment  line  right  aSer  the  #!  line  to   define  the  source  file  encoding:  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 9 / 61
  10. 10. Informal  Introduc0on  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 10 / 61
  11. 11. Using  Python  as  a  Calculator  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 11 / 61
  12. 12. Numbers  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 12 / 61
  13. 13. Strings  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 13 / 61
  14. 14. Strings  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 14 / 61
  15. 15. String  Concatena0on  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 15 / 61
  16. 16. String  Indexing  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 16 / 61
  17. 17. String  Slicing  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 17 / 61
  18. 18. Strings  are  Immutable!  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 18 / 61
  19. 19. Indices  May  be  Nega0ve  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 19 / 61
  20. 20. How  Come?!  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 20 / 61
  21. 21. Lists   •  compound  data  type   •  used  to  group  together  other  values     •  The  most  versa0le   •  can  be  wricen  as  a  list  of  comma-­‐separated   values  (items)  between  square  brackets.     •  List  items  need  not  all  have  the  same  type.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 21 / 61
  22. 22. Shallow  Copy  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 22 / 61
  23. 23. First  Steps  toward  Programming  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 23 / 61
  24. 24. More  Control  Flow  Tools  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 24 / 61
  25. 25. If  Statement  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 25 / 61
  26. 26. For  Statement  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 26 / 61
  27. 27. For  Statement  with  Shallow  Copy  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 27 / 61
  28. 28. Range  Func0on  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 28 / 61
  29. 29. Break,  Con0nue,  Else  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 29 / 61
  30. 30. Pass   •  Does  Nothing!  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 30 / 61
  31. 31. Func0ons  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 31 / 61
  32. 32. Return  Statement    Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 32 / 61
  33. 33. Default  Argument  Value  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 33 / 61
  34. 34. Documenta0on  String  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 34 / 61
  35. 35. Coding  Style  -­‐  PEP8   •  Use  4-­‐space  indenta0on,  and  no  tabs.   •  4  spaces  are  a  good  compromise  between  small   indenta0on  (allows  greater  nes0ng  depth)  and  large   indenta0on  (easier  to  read).  Tabs  introduce  confusion,   and  are  best  leS  out.   •  Wrap  lines  so  that  they  don’t  exceed  79  characters.   •  This  helps  users  with  small  displays  and  makes  it   possible  to  have  several  code  files  side-­‐by-­‐side  on   larger  displays.   •  Use  blank  lines  to  separate  func0ons  and  classes,  and   larger  blocks  of  code  inside  func0ons.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 35 / 61
  36. 36. Coding  Style  -­‐  PEP8   •  When  possible,  put  comments  on  a  line  of  their  own.   •  Use  docstrings.   •  Use  spaces  around  operators  and  aSer  commas,  but  not   directly  inside  bracke0ng  constructs:  a  =  f(1,  2)  +  g(3,  4).   •  Name  your  classes  and  func0ons  consistently;  the   conven0on  is  to  use  CamelCase  for  classes  and   lower_case_with_underscores  for  func0ons  and  methods.   Always  use  self  as  the  name  for  the  first  method  argument.   •  Don’t  use  fancy  encodings  if  your  code  is  meant  to  be  used   in  interna0onal  environments.  Plain  ASCII  works  best  in  any   case.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 36 / 61
  37. 37. Data  Structures  -­‐  Lists   •  list.append(x)  Add  an  item  to  the  end  of  the  list.   •  list.extend(L)  Extend  the  list  by  appending  all  the   items  in  the  given  list.   •  list.insert(i,  x)  Insert  an  item  at  a  given  posi0on.   The  first  argument  is  the  index  of  the  element   before  which  to  insert,  so  a.insert(0,  x)  inserts  at   the  front  of  the  list,  and  a.insert(len(a),  x)  is   equivalent  to  a.append(x).   •  list.remove(x)  Remove  the  first  item  from  the  list   whose  value  is  x.  It  is  an  error  if  there  is  no  such   item.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 37 / 61
  38. 38. Data  Structures  -­‐  Lists   •  list.pop([i])  Remove  the  item  at  the  given  posi0on  in   the  list,  and  return  it.  If  no  index  is  specified,  a.pop()   removes  and  returns  the  last  item  in  the  list.     •  list.index(x)  Return  the  index  in  the  list  of  the  first  item   whose  value  is  x.  It  is  an  error  if  there  is  no  such  item.   •  list.count(x)  Return  the  number  of  0mes  x  appears  in   the  list.   •  list.sort()  Sort  the  items  of  the  list,  in  place.   •  list.reverse()  Reverse  the  elements  of  the  list,  in  place.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 38 / 61
  39. 39. Lists  Example  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 39 / 61
  40. 40. Using  Lists  as  Stack  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 40 / 61
  41. 41. Using  List  as  a  Queue  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 41 / 61
  42. 42. Func0onal  Programming  Tools   •  There  are  three  built-­‐in  func0ons  that  are  very   useful  when  used  with  lists:  filter(),  map(),  and   reduce().  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 42 / 61
  43. 43. Filter()   •  filter(func0on,  sequence)  returns  a  sequence   consis0ng  of  those  items  from  the  sequence   for  which  func0on(item)  is  true.     •  If  sequence  is  a  string  or  tuple,  the  result  will   be  of  the  same  type;  otherwise,  it  is  always  a   list.     •  For  example,  to  compute  a  sequence  of   numbers  not  divisible  by  2  and  3:  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 43 / 61
  44. 44. Filter()  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 44 / 61
  45. 45. Map()   •  map(func0on,  sequence)  calls  func0on(item)   for  each  of  the  sequence’s  items  and  returns  a   list  of  the  return  values.     •  For  example,  to  compute  some  cubes:  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 45 / 61
  46. 46. Map()   •  More  than  one  sequence  may  be  passed;  the   func0on  must  then  have  as  many  arguments   as  there  are  sequences  and  is  called  with  the   corresponding  item  from  each  sequence  (or   None  if  some  sequence  is  shorter  than   another).  For  example:  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 46 / 61
  47. 47. Reduce()   •  reduce(func0on,  sequence)  returns  a  single   value  constructed  by  calling  the  binary   func0on  func2on  on  the  first  two  items  of  the   sequence,  then  on  the  result  and  the  next   item,  and  so  on.     •  For  example,  to  compute  the  sum  of  the   numbers  1  through  10:  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 47 / 61
  48. 48. Tuples  and  Sequences  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 48 / 61
  49. 49. Comma  at  the  end!  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 49 / 61
  50. 50. Sets   •  A  set  is  an  unordered  collec0on  with  no   duplicate  elements.     •  Basic  uses  include  membership  tes0ng  and   elimina0ng  duplicate  entries.     •  Set  objects  also  support  mathema0cal   opera0ons  like  union,  intersec0on,  difference,   and  symmetric  difference.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 50 / 61
  51. 51. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 51 / 61
  52. 52. Dic0onaries   •  Dic0onaries  are  some0mes  found  in  other   languages  as  “associa0ve  memories”  or   “associa0ve  arrays”.     •  Unlike  sequences,  which  are  indexed  by  a  range   of  numbers,  dic0onaries  are  indexed  by  keys,   which  can  be  any  immutable  type;  strings  and   numbers  can  always  be  keys.     •  Tuples  can  be  used  as  keys  if  they  contain  only   strings,  numbers,  or  tuples;  if  a  tuple  contains   any  mutable  object  either  directly  or  indirectly,  it   cannot  be  used  as  a  key.    Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 52 / 61
  53. 53. Dic0onaries   •  You  can’t  use  lists  as  keys,  since  lists  can  be  modified  in   place  using  index  assignments,  slice  assignments,  or   methods  like  append()  and  extend().   •  It  is  best  to  think  of  a  dic0onary  as  an  unordered  set  of   key:  value  pairs,  with  the  requirement  that  the  keys   are  unique  (within  one  dic0onary).     •  A  pair  of  braces  creates  an  empty  dic0onary:  {}.  Placing   a  comma-­‐separated  list  of  key:value  pairs  within  the   braces  adds  ini0al  key:value  pairs  to  the  dic0onary;   this  is  also  the  way  dic0onaries  are  wricen  on  output.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 53 / 61
  54. 54. Dic0onaries   •  The  main  opera0ons  on  a  dic0onary  are  storing  a  value   with  some  key  and  extrac0ng  the  value  given  the  key.   •  It  is  also  possible  to  delete  a  key:value  pair  with  del.  If   you  store  using  a  key  that  is  already  in  use,  the  old   value  associated  with  that  key  is  forgocen.  It  is  an   error  to  extract  a  value  using  a  non-­‐existent  key.   •  The  keys()  method  of  a  dic0onary  object  returns  a  list   of  all  the  keys  used  in  the  dic0onary,  in  arbitrary  order   (if  you  want  it  sorted,  just  apply  the  sorted()  func0on   to  it).     •  To  check  whether  a  single  key  is  in  the  dic0onary,  use   the  in  keyword.  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 54 / 61
  55. 55. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 55 / 61
  56. 56. Dict()  Constructor  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 56 / 61
  57. 57. Looping  Techniques  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 57 / 61
  58. 58. Looping  over  Two  Sequences  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 58 / 61
  59. 59. Loop  in  Reverse  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 59 / 61
  60. 60. More  on  Condi0ons   •  Include  and,  or  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 60 / 61
  61. 61. Comparing  Objects  of  Different  Types  Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 61 / 61
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×