60
Textual Analysis & Introduc4on to Python Feb 19 2015 CSCI 0931 Intro. to Comp. for the Humani4es and Social Sciences 1

Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  &  Introduc4on  to  Python    

Feb  19  2015  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   1  

Page 2: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Today’s  Class  

•  Brief  talk  about  senator  vs  senator  comparison  (and  matrix  mul4plica4on)  

•  Intro  to  text  analysis  problems  •  Intro  to  Python  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   2  

Page 3: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Text  Analysis  and  Python  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   3  

We’re  star4ng  a  new  unit  in  our  course!    

Page 4: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   4  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Computer  

Page 5: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   5  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Computer  

ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG  

Page 6: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   6  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Computer  

Build  a  Concordance  of  a  text  •  Loca/ons  of  words  •  Frequency  of  words  

ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG  

Page 7: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

Alphabe4cal  index  of  all  words  in  a  text  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   7  

Word   Page  Numbers  

Apple   4,7,10,27  

Banana   77,110,130  

Carrot   50,101  

Date   9  

…   …  

Page 8: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

•  Before  computers,  was  a  huge  pain.  •  What  texts  might  have  had  concordances?  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   8  

hZp://en.wikipedia.org/wiki/Concordance_(publishing)  

Page 9: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

•  Before  computers,  was  a  huge  pain.  •  What  texts  might  have  had  concordances?  – The  Bible  – The  Quran  – The  Vedas  – Shakespeare  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   9  

hZp://en.wikipedia.org/wiki/Concordance_(publishing)  

Page 10: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

•  Before  computers,  was  a  huge  pain.  •  What  texts  might  have  had  concordances?  – The  Bible  – The  Quran  – The  Vedas  – Shakespeare  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   10  

Not  a  “New”  Problem:  First  Bible  Concordance  completed  in  1230  

hZp://en.wikipedia.org/wiki/Concordance_(publishing)  

Page 11: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

•  How  long  would  the  King  James  Bible  take  us?  – 783,137  words  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   11  

hZp://agards-­‐bible-­‐4meline.com/q10_bible-­‐facts.html  

Page 12: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

•  How  long  would  the  King  James  Bible  take  us?  – 783,137  words    

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   12  

hZp://agards-­‐bible-­‐4meline.com/q10_bible-­‐facts.html  

800,000  *  (3  min.  to  look  up  word  and  put  page  #)  =  2,400,000  minutes  =  40,000  hours  =  1,667  days  =  4.5  years  

Page 13: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Concordances  

•  How  long  would  the  King  James  Bible  take  us?  – 783,137  words  

 Takes  70  hours  to  read  the  King  James  Bible  aloud    

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   13  

hZp://agards-­‐bible-­‐4meline.com/q10_bible-­‐facts.html  

800,000  *  (3  min.  to  look  up  word  and  put  page  #)  =  2,400,000  minutes  =  40,000  hours  =  1,667  days  =  4.5  years  

Page 14: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Strong’s  Concordance  

•  Concordance  of  the  King  James  Bible  •  Published  in  1890  by  James  Strong  

Wikipedia  

hZp://www.chris4anbook.com/reader/?item_no=563788  CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   14  

Page 15: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

From  Concordance  to  Word  Frequency  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   15  

Suppose  our  text  has  1000  words  total.  

Word   Page  Numbers  

#  of  Occurrences  

Word  Frequency  

Apple   4,7,10,27   4   4/1000  

Banana   77,110,130   3   3/1000  

Carrot   50,101   2   2/1000  

Date   9   1   1/1000  

…   …   …   …  

Page 16: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Google  Ngrams  

•  Google  (verb)  “Google  n-­‐grams”  •  ngram:  a  set  of  n  words  – “hello”  is  a  1-­‐gram  – “hello  there”  is  a  2-­‐gram  

•  Click  on  “About  Google  Books  Ngram  Viewer”  for  more  informa4on    

•  Ques4on:  what  is  the  data  source  here?    CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   16  

Page 17: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   17  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Computer  

Build  a  Concordance  of  a  text  •  Loca/ons  of  words  •  Frequency  of  words  •  Word  frequencies  across  4me  

ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG  

Page 18: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

The  Wizard  of  OZ  

•  About  40  Books,  wriZen  by  7  different  authors  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   18  

Lyman  Frank  Baum   Ruth  Plumly  Thompson  

hZp://www.ssc.wisc.edu/~zzeng/soc357/OZ.pdf  

#1   #14   #15   #16   #33  

…   …  

Page 19: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

The  Wizard  of  OZ  

•  About  40  Books,  wriZen  by  7  different  authors  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   19  

Lyman  Frank  Baum  (1856-­‐1919)  

Ruth  Plumly  Thompson  

hZp://www.ssc.wisc.edu/~zzeng/soc357/OZ.pdf  

#1   #14   #15   #16   #33  

…   …  

Published  in  1921  

Page 20: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

The  Wizard  of  OZ  

•  About  40  Books,  wriZen  by  7  different  authors  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   20  

Lyman  Frank  Baum  (1856-­‐1919)  

Ruth  Plumly  Thompson  

hZp://www.ssc.wisc.edu/~zzeng/soc357/OZ.pdf  

#1   #14   #15   #16   #33  

…   …  

Published  in  1921  

Page 21: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

The  Federalist  Papers  •  85  ar4cles  wriZen  in  1787  to  promote  the  ra4fica4on  of  the  US  Cons4tu4on  

•  In  1944,  Douglass  Adair  guessed  authorship  – Alexander  Hamilton  (51)  –  James  Madison  (26)  –  John  Jay  (5)  –  3  were  a  collabora4on  

•  Confirmed  in  1964  by  a  computer  analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   21  

Wikipedia  

hZp://pages.cs.wisc.edu/~gfung/federalist.pdf  

Page 22: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   22  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Computer  

Build  a  Concordance  of  a  text  •  Loca/ons  of  words  •  Frequency  of  words  •  Word  frequencies  across  4me  

•  Determine  authorship  ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG  

Page 23: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   23  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Computer  

Build  a  Concordance  of  a  text  •  Loca/ons  of  words  •  Frequency  of  words  •  Word  frequencies  across  4me  

•  Determine  authorship  •  Count  labels  to  determine  

liberal  media  bias  

ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG  

Page 24: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

How  are  we  going  to  analyze  texts?  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   24  

firehow.com  

Excel  

Numerical  Data  

Page 25: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

How  are  we  going  to  analyze  texts?  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   25  

firehow.com  

Excel  

Numerical  Data  

Textual  Data  

Page 26: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

How  are  we  going  to  analyze  texts?  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   26  

Makita  Cordless  Chain  Saw,  $270  

Textual  Data  

Page 27: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

How  are  we  going  to  analyze  texts?  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   27  

Textual  Data  

9poundhammer.blogspot.com  

Python:  A  Programming  Language  Free!  

Page 28: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Textual  Analysis  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   28  

Define  Problem   Find  Data  

Write  a  set  of  instruc4ons  

Solu4on  

Python  

Build  a  Concordance  of  a  text  •  Loca/ons  of  words  •  Frequency  of  words  •  Word  frequencies  across  4me  

•  Determine  authorship  •  Count  labels  to  determine  

liberal  media  bias  

ACTACGTCGACTACGATCACGATCGCGCGATCACGTATTTACGATCAGCTACGATCGATCTACGATCGTAGCTGTGATCG  

Page 29: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

“Python”  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   29  

Page 30: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

“Python”  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   30  

•  A  language  for  giving  the  computer  instruc4ons.  It  has  syntax  and  seman4cs.  

Page 31: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

“Python”  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   31  

•  A  language  for  giving  the  computer  instruc4ons.  It  has  syntax  and  seman4cs.  

•  Might  say  “write  a  Python  program”,  meaning  “write  instruc4ons  in  the  Python  language”  

Page 32: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

“Python”  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   32  

•  A  language  for  giving  the  computer  instruc4ons.  It  has  syntax  and  seman4cs.  

•  Might  say  “write  a  Python  program”,  meaning  “write  instruc4ons  in  the  Python  language”  

•  There  is  an  interpreter  (e.g.,  IDLE)  that  takes  Python  instruc4ons  and  executes  them  with  the  CPU,  etc.  

Page 33: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Install  •  Let’s  install  Python  2.7.9    •  www.python.org/downloads/  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   33  

Page 34: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Install  

•  Lets  open  IDLE  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   34  

Page 35: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  •  Expressions  are  inputs  that  Python  evaluates  –  Expressions  return  an  output  –  Like  using  a  calculator  

Type  the  expressions  below    auer  ‘>>>’  and  hit  Enter  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   35  

1.   Expressions  2.  Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

Page 36: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  •  Expressions  are  inputs  that  Python  evaluates  –  Expressions  return  an  output  –  Like  using  a  calculator  

Type  the  expressions  below    auer  ‘>>>’  and  hit  Enter  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   36  

1.   Expressions  2.  Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 4+2 6 >>> 4-2 2 >>> 4*2 8 >>> 4/2 2

Page 37: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Assignments  do  not  have  an  output,  they  are  stored  in  memory.  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   37  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

Page 38: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Assignments  do  not  have  an  output,  they  are  stored  in  memory.  – We’ve  done  this  kind  of  thing  in  Excel  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   38  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

We  have  assigned  the  number  1  to  cell  A1.  

Page 39: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Assignments  do  not  have  an  output,  they  are  stored  in  memory.  – We’ve  done  this  kind  of  thing  in  Spreadsheets  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   39  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

We  have  assigned  the  number  1  to  cell  A1.  

Let’s  rename  cell  A1  to  x.  

Page 40: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Assignments  do  not  have  an  output,  they  are  stored  in  memory.  – We’ve  done  this  kind  of  thing  in  Spreadsheets  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   40  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

Let’s  rename  cell  A1  to  x.  

>>> x = 1

Page 41: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Assignments  do  not  have  an  output,  they  are  stored  in  memory.  – We’ve  done  this  kind  of  thing  in  Spreadsheets  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   41  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> x = 1

variable   expression  =  

Memory  

Variable  Name   Value  

x   1  

Page 42: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Assignments  do  not  have  an  output,  they  are  stored  in  memory.  – We’ve  done  this  kind  of  thing  in  Spreadsheets  

– We  can  now  use  x  in  expressions!  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   42  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> x = 1

Memory  

Variable  Name   Value  

x   1  

>>> x+1 2 >>> (x+2)*3 9

Page 43: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  You  can  name  your  variables  anything  

•  Well,  almost  anything  – No  spaces,  operators,  punctua4on,  number  in  the  first  posi4on  

•  Variables  usually  start  with  a  lowercase  leZer  and,  if  useful,  describe  something  about  the  value.  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   43  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> numberOfEggs = 100 >>> myNumber = 12345 >>> noninteger = 4.75

Page 44: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Choices  

•  Why  are  those  the  rules  for  names?    •  Someone  thought  about  it  and  made  a  choice  •  Usually  based  on  years  of  experience  •  Many  choices  seem  crazy...  – Un4l  one  day  you  see  they’re  obviously  correct  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   44  

Page 45: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Try  this:  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   45  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 3/2

Page 46: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Try  this:  •  There  are  two  types  of  numbers  in  Python.    The  type()func4on  is  useful.  

     

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   46  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 3/2

>>> type(3/2) <type 'int'> >>> type(1.5) <type 'float'>

Page 47: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Try  this:  •  There  are  two  types  of  numbers  in  Python.    The  type()func4on  is  useful.  

 •  Floats  are  numbers  that                                      display  with  decimal                                      points.      

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   47  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 3/2

>>> type(3/2) <type 'int'> >>> type(1.5) <type 'float'>

>>> 3.0/2.0 1.5

Page 48: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Try  this:  •  There  are  two  types  of  numbers  in  Python.    The  type()func4on  is  useful.  

 •  Floats  are  decimals.      

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   48  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 3/2

>>> type(3/2) <type 'int'> >>> type(1.5) <type 'float'>

>>> 3.0/2.0 1.5

General  Rule:  Expressions  for  a  par4cular  type  will  output  that  same  type!  

Page 49: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Strings  are  sequences  of  characters,  surrounded  by  single  quotes.    

     •  The  +  operator  concatenates  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   49  

1.   Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 'hi' 'hi' >>> myString = 'hi there' >>> myString 'hi there'

Page 50: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Strings  are  sequences  of  characters,  surrounded  by  single  quotes.    

     •  The  +  operator  concatenates  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   50  

1.   Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 'hi' 'hi' >>> myString = 'hi there' >>> myString 'hi there'

General  Rule:  Expressions  for  a  par4cular  type  will  output  that  same  type!  

Page 51: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Strings  are  sequences  of  characters,  surrounded  by  single  quotes.    

     •  The  +  operator  concatenates  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   51  

1.   Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> 'hi' 'hi' >>> myString = 'hi there' >>> myString 'hi there'

>>> endString = ' class!' >>> myString + endString 'hi there class!' >>> newString = myString + endString >>> newString 'hi there class!'

Page 52: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Lists  are  an  ordered  collec4on  of  items  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   52  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> [5,10,15] [5, 10, 15] >>> myList = [5,10,15] >>> myList [5, 10, 15] >>> stringList = ['hi','there','class'] >>> stringList ['hi', 'there', 'class']

Page 53: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  •  Lists  are  an  ordered  collec4on  of  items  

   •  Individual  items  are  elements  •  The  +  operator  concatenates  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   53  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> [5,10,15] [5, 10, 15] >>> myList = [5,10,15] >>> myList [5, 10, 15] >>> stringList = ['hi','there','class'] >>> stringList ['hi', 'there', 'class']

>>> myList + stringList [5, 10, 15, 'hi', 'there', 'class']

Page 54: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  To  get  an  element  from  a  list,  use  the  expression                                                      where  i  is  the    index.  Ouen  spoken:  “myList  sub  i”  

•  List  indices  start  at  0!  

•  What  does                                                            do?  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   54  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> myList[i]

>>> myList[0] 5 >>> myList[1] 10 >>> myList[2] 15

>>> myList[1] = 4

Page 55: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  To  get  a  range  of  elements  from  a  list,  use  the  expression                                                      where  i  is  the  start  index  (inclusive)  and  j  is  the  end  index  (exclusive).  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   55  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> myList[i:j]

>>> myList [5, 4, 15] >>> myList[0:2] [5, 4] >>> myList[1:3] [4, 15] >>> newList = [2,5,29,1,9,59,3] >>> newList [2, 5, 29, 1, 9, 59, 3] >>> newList[2:6] [29, 1, 9, 59]

Page 56: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Indexing  and  ranges  also  work  on  Strings.  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   56  

1.  Expressions  2.  Assignments  

a)  Variables  3.   Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

>>> myString 'hi there' >>> myString[0] 'h' >>> myString[5] 'e' >>> myString[6] 'r' >>> myString[0:6] 'hi the'

Page 57: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Introduc4on  to  Python  

•  Remember  what  assignments  do  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   57  

1.  Expressions  2.   Assignments  

a)  Variables  3.  Types  

a)  Integers  b)  Floats  c)  Strings  d)  Lists  

Memory  

Variable  Name   Value  

x 1

amountOfEggs 100

myNumber 12345

noninteger 4.75

myString 'hi there'

endString ' class!'

myList [5,4,15]

stringList ['hi','there','class']

newList [2,5,29,1,9,59,3]

Page 58: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

Class  Review  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   58  

Python  So  Far  (to  be  updated/refined!)  1.  Expressions  •  Evaluate  input  and  returns  some  output  (calculator)  

2.  Variable  Assignments:  <variable>  =  <expression>  •  Store  the  value  of  the  expression  in  the  variable  

instead  of  outpu{ng  the  value.  •  There  is  always  an  equals  sign  in  an  assignment  •  Variables  can  be  named  many  things  •  List  assignments:  <listvar>[<index>]  =  <expression>  

3.  Types  •  Integers  vs.  Floats  (Decimals)  •  Strings  in  single  quotes  •  Lists  are  sets  of  other  types  •  We  can  index  into  Strings  &  Lists  

•  Indexed  starUng  at  0!  

General  Rule:  Expressions  for  a  par4cular  type  will  output  that  same  type!  

Page 59: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

A  brief  review  of  things  you  didn’t  know  you’d  learned  

•  In  a  spreadsheet,  there  are  many  types  of  data  

•  Numbers  (start  with  +/-­‐  or  a  digit)  •  Strings  (nondigit-­‐start,  or  start  with  ‘  )  •  Formulas  (start  with  =  )  •  Ranges  (B2,  B2:B4,  B2:D5)  •  Errors  (#N/A)  •  Blanks  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   59  

Page 60: Textual(Analysis(&(Introduc4on( to(Python((cs.brown.edu/courses/cs0931/2015-spring/2-text_analysis/LEC2-1.pdf · Textual(Analysis(CSCI0931(A(Intro.(to(Comp.(for(the(Humani4es(and(Social(Sciences(

What  shows  up  in  a  cell  •  If  a  formula  evaluates  to  a  number  or  string,  that  number  or  string  

•  If  it  evaluates  to  a  range,  the  value  in  the  first  cell  of  that  range  ...some4mes  –  If  you  write  =A1:A6,  you  get  A1  –  If  you  write  =OFFSET(A1:A6,  0,  0),  Gsheets  fills  in  adjacent  cells;  excel  just  fills  in  one  cell  

•  If  evalua4on  leads  to  an  error,  then  #N/A  •  Mostly,  we  never  no4ce  any  of  this  •  In  Python,  the  rules  have  greater  consistency,  and  because  results  aren’t  instantly  visible,  knowing  the  rules  maZers  more  

CSCI  0931  -­‐  Intro.  to  Comp.  for  the  Humani4es  and  Social  Sciences   60