lecture 9 - dsa - python data structures

61
Data Structures in Python Crash Course Dr.Haitham A. El-Ghareeb Information Systems Department Faculty of Computers and Information Sciences Mansoura University [email protected] December 9, 2012 Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 1 / 61

Upload: haitham-a-el-ghareeb

Post on 05-Dec-2014

1.942 views

Category:

Education


8 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Lecture 9 - DSA - Python Data Structures

Data Structures in PythonCrash Course

Dr.Haitham A. El-Ghareeb

Information Systems DepartmentFaculty of Computers and Information Sciences

Mansoura University

[email protected]

December 9, 2012

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 1 / 61

Page 2: Lecture 9 - DSA - Python Data Structures

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

Page 3: Lecture 9 - DSA - Python Data Structures

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

Page 4: Lecture 9 - DSA - Python Data Structures

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

Page 5: Lecture 9 - DSA - Python Data Structures

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

Page 6: Lecture 9 - DSA - Python Data Structures

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

Page 7: Lecture 9 - DSA - Python Data Structures

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

Page 8: Lecture 9 - DSA - Python Data Structures

Executable  Python  Scripts  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 8 / 61

Page 9: Lecture 9 - DSA - Python Data Structures

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

Page 10: Lecture 9 - DSA - Python Data Structures

Informal  Introduc0on  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 10 / 61

Page 11: Lecture 9 - DSA - Python Data Structures

Using  Python  as  a  Calculator  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 11 / 61

Page 12: Lecture 9 - DSA - Python Data Structures

Numbers  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 12 / 61

Page 13: Lecture 9 - DSA - Python Data Structures

Strings  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 13 / 61

Page 14: Lecture 9 - DSA - Python Data Structures

Strings  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 14 / 61

Page 15: Lecture 9 - DSA - Python Data Structures

String  Concatena0on  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 15 / 61

Page 16: Lecture 9 - DSA - Python Data Structures

String  Indexing  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 16 / 61

Page 17: Lecture 9 - DSA - Python Data Structures

String  Slicing  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 17 / 61

Page 18: Lecture 9 - DSA - Python Data Structures

Strings  are  Immutable!  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 18 / 61

Page 19: Lecture 9 - DSA - Python Data Structures

Indices  May  be  Nega0ve  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 19 / 61

Page 20: Lecture 9 - DSA - Python Data Structures

How  Come?!  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 20 / 61

Page 21: Lecture 9 - DSA - Python Data Structures

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

Page 22: Lecture 9 - DSA - Python Data Structures

Shallow  Copy  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 22 / 61

Page 23: Lecture 9 - DSA - Python Data Structures

First  Steps  toward  Programming  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 23 / 61

Page 24: Lecture 9 - DSA - Python Data Structures

More  Control  Flow  Tools  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 24 / 61

Page 25: Lecture 9 - DSA - Python Data Structures

If  Statement  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 25 / 61

Page 26: Lecture 9 - DSA - Python Data Structures

For  Statement  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 26 / 61

Page 27: Lecture 9 - DSA - Python Data Structures

For  Statement  with  Shallow  Copy  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 27 / 61

Page 28: Lecture 9 - DSA - Python Data Structures

Range  Func0on  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 28 / 61

Page 29: Lecture 9 - DSA - Python Data Structures

Break,  Con0nue,  Else  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 29 / 61

Page 30: Lecture 9 - DSA - Python Data Structures

Pass  

•  Does  Nothing!  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 30 / 61

Page 31: Lecture 9 - DSA - Python Data Structures

Func0ons  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 31 / 61

Page 32: Lecture 9 - DSA - Python Data Structures

Return  Statement    

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 32 / 61

Page 33: Lecture 9 - DSA - Python Data Structures

Default  Argument  Value  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 33 / 61

Page 34: Lecture 9 - DSA - Python Data Structures

Documenta0on  String  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 34 / 61

Page 35: Lecture 9 - DSA - Python Data Structures

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

Page 36: Lecture 9 - DSA - Python Data Structures

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

Page 37: Lecture 9 - DSA - Python Data Structures

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

Page 38: Lecture 9 - DSA - Python Data Structures

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

Page 39: Lecture 9 - DSA - Python Data Structures

Lists  Example  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 39 / 61

Page 40: Lecture 9 - DSA - Python Data Structures

Using  Lists  as  Stack  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 40 / 61

Page 41: Lecture 9 - DSA - Python Data Structures

Using  List  as  a  Queue  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 41 / 61

Page 42: Lecture 9 - DSA - Python Data Structures

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

Page 43: Lecture 9 - DSA - Python Data Structures

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

Page 44: Lecture 9 - DSA - Python Data Structures

Filter()  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 44 / 61

Page 45: Lecture 9 - DSA - Python Data Structures

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

Page 46: Lecture 9 - DSA - Python Data Structures

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

Page 47: Lecture 9 - DSA - Python Data Structures

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

Page 48: Lecture 9 - DSA - Python Data Structures

Tuples  and  Sequences  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 48 / 61

Page 49: Lecture 9 - DSA - Python Data Structures

Comma  at  the  end!  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 49 / 61

Page 50: Lecture 9 - DSA - Python Data Structures

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

Page 51: Lecture 9 - DSA - Python Data Structures

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 51 / 61

Page 52: Lecture 9 - DSA - Python Data Structures

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

Page 53: Lecture 9 - DSA - Python Data Structures

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

Page 54: Lecture 9 - DSA - Python Data Structures

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

Page 55: Lecture 9 - DSA - Python Data Structures

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 55 / 61

Page 56: Lecture 9 - DSA - Python Data Structures

Dict()  Constructor  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 56 / 61

Page 57: Lecture 9 - DSA - Python Data Structures

Looping  Techniques  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 57 / 61

Page 58: Lecture 9 - DSA - Python Data Structures

Looping  over  Two  Sequences  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 58 / 61

Page 59: Lecture 9 - DSA - Python Data Structures

Loop  in  Reverse  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 59 / 61

Page 60: Lecture 9 - DSA - Python Data Structures

More  on  Condi0ons  

•  Include  and,  or  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 60 / 61

Page 61: Lecture 9 - DSA - Python Data Structures

Comparing  Objects  of  Different  Types  

Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 December 9, 2012 61 / 61