Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Introduc On To Programming With Python

This document provides an introduction to programming with Python as part of a course on computational and corpus linguistics. It discusses why programming is a useful tool for linguists and explains that Python will be used in the course. It then covers getting started with Python, including interactive mode and writing scripts. It demonstrates basic Python statements and covers strings, escape characters, and printing output. An exercise is provided to demonstrate printing a table with names and phone numbers. Finally, it briefly introduces basic programming elements like assignment and loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Introduc On To Programming With Python

This document provides an introduction to programming with Python as part of a course on computational and corpus linguistics. It discusses why programming is a useful tool for linguists and explains that Python will be used in the course. It then covers getting started with Python, including interactive mode and writing scripts. It demonstrates basic Python statements and covers strings, escape characters, and printing output. An exercise is provided to demonstrate printing a table with names and phone numbers. Finally, it briefly introduces basic programming elements like assignment and loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

 

   
 Introduc*on  to  Programming  with  Python  

             as  part  of  the  course  


“Introduc)on  to  Computa)onal  and  Corpus  Linguis)cs”  

Prof.  J.  Lavid  


Universidad  Complutense  

1  
•  Few,  if  any,  linguis*cs  programs  do  require  an  introduc*on  to  
programming.  That  is  a  shame!!!  
–  Programming  is  a  useful  tool  for  linguists,  just  as  word  processing  or  
sta*s*cs.    
–  It  can  help  you  do  things  faster  and  at  larger  scale.    
–  It’s  fun  

•  Programming  is  not  a  black  art.  It’s  a  skill  that  can  be  learned.  
In  this  course  we  will  use  the  Python  programming  language.  

2  
GeLng  started  with  Python  
•  Wri*ng  and  running  Python  programs:  
–  Interac*ve  mode  
–  In  text  files  called  ‘scripts’  

3  
Interac*ve  mode  
>>>  2  +  2    
4          
>>>  3  *  5  +  12    
27  
>>>  12  *  5000    
60000    
>>>  (12  *  5000)  /  52    
1153    
>>>  print  "Hi  there"    
Hi  there    

4  
Text  files  (‘scripts’)  
-­‐  Open  a  new  file/window  from  the  file  menu  
-­‐  Type  the  following  statements:  
 print  "Hello  there!"    
 print  "Tes*ng,  one,  two,  three”  
-­‐  Save  the  the  document  from  the  file  menu.  Give  it  the  name:  
Myfirstprogram    
-­‐  When  PythonWin  saves  the  file,  it  adds  a  .py  to  the  end  of  the  
filename  to  indicate  that  it’s  a  Python  file.  
-­‐  Run  the  program  by  clicking  on  the  run  icon.  You  should  see  the  
results  in  the  interac*ve  window:  
>>>  Hello  there!  
Tes*ng,  one,  two,  three  

5  
Python  statements  
>>>  print  'Hello  there!’  

print   ‘Hello  there!  


KEYWORD   VALUE  

6  
Strings  
•  Sequence  of  characters  enclosed  in  quotes  (single  ‘..’  or  double  
“…”),  ac*ng  as  delimiters.  The  opening  and  closing  quotes  must  
match:  
   'Hello  there!'  "Hello  there!”    are  fine    
     "Hello  there!'      'Hello  there!”    are  not    

What  happens  when  you  have  double  quotes  as  string  delimiters  and  
you  have  a  double  quote  in  the  string?    
>>>  print  ”Pam  wrote  ”This  is  nice  programming""    

Python  finds  a  quote  just  before  the  word  Learn  and  considers  it  as  
marking  the  end  of  the  string.  Thus,  it  considers  only  the  bold  part  
to  be  the  string:"Pam  wrote  ”  This  is  nice  programming""  

7  
Strings  
•  To  prevent  this  we  use  \"  (that  is,  two  characters—a  
backslash,  ‘\’,  and  a  quote,  ‘"’)  to  represent  quotes  within  a  
string.    
>>>  print  "Pam  wrote  \”This  is  nice  programming\""  
Pat  wrote  ”This  is  nice  programming”  

The  ‘\’  means  ‘interpret  the  next  character  as  a  real  genuine  
character.’  

8  
Strings  
•  Rule  of  thumb:    
 -­‐  if  I  want  to  have  double  quotes  in  the  string  I  use  the  single  
quotes  as  delimiters:  
>>>  print  'Pam  wrote  ”This  is  nice  programming"'    
Pam  wrote  ”This  is  nice  programming"  
 -­‐  if  I  want  to  have  single  quotes  in  the  string  I  use  double  
quotes  as  delimiters:  
>>>  print  "I  don't  understand"  
 I  don't  understand  

9  
•  Create  a  script  pat.py  
print  "Pat"    
print  "Hillary”  

•  Run  it  :    
Pam    
Hilton  
Suppose  I  wanted  the  words  Pat  and  Hillary  to  appear  on  the  same  line.    
1)  print  "Pam  Hilton”  
2)  print  ”Pam",    
 print  "Hilton"(with  a  comma  aper  the  first  print  statement)  
 Pam  Hilton  
if  I  wanted  to  use  one  print  statement  and  have  the  names  appear  on  different  
lines  I  can  use  the  special  character  sequence  \n  which  means  go  to  a  new  line  
>>>  print  "Pam\n  Hilton”  
               Pam    
   Hilton    

10  
Exercise  
Create  a  Python  script  that  will  print  the  following    output:  

Person      Phone  number  


-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐    -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐    
Pam  541-­‐1360    
Hilton  646-­‐6520    
Kate  646-­‐3307  

11  
Answer  
print  "Person  ",    
print  "Phone  number"    
print  "-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐",    
print  "-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐"    
print  "Pam",    
print  "541-­‐1360"    
print  "Hilton  ",    
print  "646-­‐6520"    
print  ”Kate”,  
print  "646-­‐3307"  

12  
4  basic  elements  in  programming  
•  Assignment:  linking  a  name  to  a  value.  These  names  are  also  called  variables.    

•  Loops:  Doing  the  same  thing  repeatedly,  either  a  fixed  number  of  *mes,  or  un*l  
something  happens.    

•  I/O,  Input/Output:  everything  that  has  to  do  with  geLng  informa*on  into  and  out  
of  our  programs,  e.g.  files  (opening,  closing,  reading  from  or  wri*ng  to  them)  or  
output  on  the  screen.  

•  Control  structures:  some*mes,  we  need  to  make  decisions.  I.e.,  if  a  variable  has  a  
certain  value,  do  X,  otherwise,  do  Y.  Control  structures  are  simple  if...then...else  
constructs  that  evaluate  the  alterna*ves  and  make  this  decision.  

All  problems  that  can  be  solved  by  programming  


 can  be  decomposed  into  these  four  elements!  

13  
Assignment  
sentence1  =  "Monty  Python  is  full  of  memorable  quotes.”  

                     
Variable  Name  =      Value  

Variable  is  like  a  container:    


 -­‐  it  has  a  label  on  the  outside  (the  name)  
 -­‐  and  contains  some  informa*on  in  it  (the  value).    

We  can  change  the  contents  without  changing  the  label,  or  slap  on  a  new  
label  without  changing  the  content.  

14  
Names  
•  A  name  in  Python  must  begin  with  a  lever  or  an  underscore  ‘_’.    
•  The  remaining  characters  can  be  levers,  digits,  or  underscores.    
•  By  levers  we  mean  the  levers  a-­‐z  and  their  uppercase  
equivalents.  E.g:  
   My_phonenumber  
   test1  
   test2    
   NounPhrase  

15  
Ques*on  
•  Which  of  the  following  are  allowable  names?  
   2ndtest    
   John’s_Phonenumber    
   Çie  
   _myname  
   part_of_speech  

16  
Valid  Variable  Names  
•  Variables  have  to  start  with  a  lever  (you  can  use  all  levers  from  a-­‐z  and  A-­‐
Z),  but  can  not  start  with  a  number.  

•  You  can  use  numbers  later  on  in  the  variable  name  ( just  not  as  first  lever).  

•  Variable  names  cannot  contain  spaces  or  dashes  (those  are  for  
subtrac*on),  but  you  can  use  underscores  (_)  to  separate  words.  

•  It  makes  a  difference  whether  you  use  a  capital  lever  or  not.  In  general,  
people  in  Python  use  lowercase  for  variables,  and  separate  words  by  
underscores.  

17  
Ques*on  
QUESTION:  Which  of  these  are  valid  names:    
1)  quan*fier    
2)  parasi*c_gap    
3)  2cool4school  
4)  parasi*c_Gap1  

18  
Values  
•  Names  can  be  given  to  all  sorts  of  values  through  assignment  ‘=’.    
 >>>  word  =  ’gold’  
 >>>  part_of_speech  =  'noun'    
 >>>  gender  =  'neuter'    
 >>>  translaNon  =  ’cute  thing'    
 >>>  frequency  =  354  

No*ce  that  when  we  typed  a  print  statement  we  immediately  received  a  
response  from  Python:  
>>>  print  "Pat”          our  input    
       Pat            Python’s  response  

19  
Values  
When  we  assign  a  name  to  a  value,  Python  doesn’t  generate  a  response:  
 >>>  part_of_speech  =  'noun’  

Internally,  Python  has  associated  the  name  with  the  value  (in  this  case  the  
name  ‘part_of_speech’  with  the  value‘noun’),  but  externally,  we  just  don’t  
see  it.  
To  get  the  value  associated  with  a  name  we  just  use  the  name  itself.    
 >>>  word    
 ‘gold’  
 >>>  part_of_speech  
 'noun'  

20  
Exercise  
•  Create  a  ‘script’  and  call  it  new_script.py  
•  Type:  
   word  =  ”gold"    
   part_of_speech  =  "noun"    
   gender  =  "neuter"    
   transla*on  =  "stupid  thing”  
   print  word    
   print  part_of_speech  

•  Save  the  document  and  run  the  script  

21  
Exercise  
Can  you  predict  what  output  you’ll  see  if  you  run  the  script:  
   word  =  "gold"    
   part_of_speech  =  "noun"    
   gender  =  "neuter"    
   transla*on  =  "stupid  thing”  
   print  word,    
   print  part_of_speech  

22  
String  addi*on  
•  Python’s  addi*on  operator  ‘+’  joins  together  two  strings.  So    

The  added  string   Is  the  same  as  


“walk”  +  “ing”    “walking”  
“Accusa*ve”  +  “Case”   “Accusa*ve  Case”  

•  In  this  way  we  can  change  the  block  of  print  statements:  
     print  word,    
     print  part_of_speech  
to            print  word  +  "  "  +  part_of_speech  
         
   If  we  forget  to  use  the  plus  sign  we  get  an  error  
23  
Exercise  
Can  you  predict  what  output  you’ll  see  if  you  run  the
 script:  
 Det  =  “this”  
 Verb  =  “is”  
 Adj  =  “nice”  
 Verb2  =  “program”+”ming”  
 Print  Det  +  “  “  +  Verb  +  “  “  +  Adj  +  “  “+  Verb2  

24  
Common  numerical  operators  
•  Python  knows  about  different  types  of  numbers.  
•  Two  main  types:  integers  (for  example,  2,  214,  -­‐5,291)  and  real  numbers  
(0.0,  -­‐777.1,  6.25).  

•  What  Python  returns  (whether  an  integer  or  a  real  number)  depends  on  
what  it  received  as  arguments:  
–  If  it  receives  two  integers  as  arguments,  these  opera*ons  will  return  an  integer.  
–  If  they  receive  2  real  numbers,  they'll  return  a  real.    
–  If  these  opera*ons  receive  one  integer  and  another  real,  they  will  return  a  real.    
–  This  is  par*cularly  important  for  division.  If  we  divide  one  integer  by  another  we  will  get  
an  integer  result:                  >>>  5  /  2    
                             2  
25  
An  example  
•  There  are  2  teaching  assistants  for  a  linguis*cs  class.    
•  We  create  a  name  “ta”  with  the  value  2:  
   ta  =  2  
•  There  are  27  quizzes  to  grade  
   quizzes  =  27  
•  We  want  the  TA’s  to  grade  an  equal  number  of  quizzes:  
   to_grade  =  quizzes  /  ta  
•  and  now  let’s  figure  out  the  remainder:  
   remainder  =  quizzes  %  ta  
•  the  TAs  (let’s  call  them  ta1  and  ta2)  should  grade  
     ta1  =  to_grade  +  remainder    
     ta2  =  to_grade  

26  
Exercise  
•  Write  a  program  that  gives  the  following  output:  
 TA  #1  should  grade:  14  quizzes  
 TA  #2  should  grade:  13  quizzes  

27  
Solu*on  
ta  =  2  
quizzes  =  27    
to_grade  =  quizzes  /  ta    
remainder  =  quizzes  %  ta    
ta1  =  to_grade  +  remainder    
ta2  =  to_grade    
print  "TA  #1  should  grade:",    
print  ta1,    
print  "quizzes”  
print  "TA  #2  should  grade:",    
print  ta2,    
print  "quizzes"  

28  
Explana*on  
You  may  be  wondering  why  I  didn’t  use  Python’s  append  operator  (‘+’)  and  do  
something  like  
 print  "TA  #1  should  grade:  "  +  ta1  +  "  quizzes”  

The  ‘+’  operator  does  different  things  depending  on  its  arguments.  If  its  
arguments  are  strings,  it  appends  them  together:  
>>>"walk"  +  "ing"    
 walking  
>>>"1"  +  "2"  +  "3"    
 123  
If  the  arguments  to  ‘+’  are  numbers,  it  performs  addi*on:  
>>>1  +  2  +  3    
 6  

29  
Explana*on  
•  When  both  are  numbers  it  performs  addi*on.  With  that  in  mind  let’s  look  
at  the  line:  

 print  "TA  #1  should  grade:  "  +  ta1  

The  value  of  ta1  is  a  number,  14,  so  the  line  is  equivalent  in  some  way  to  :  

 print  "TA  #1  should  grade:  "  +  14  

One  argument  is  a  string  and  the  other  a  number.  This  is  a  case  that  the  ‘+’  
operator  cannot  deal  with  and  this  statement  generates  an  error.  

30  

You might also like