[go: up one dir, main page]

0% found this document useful (0 votes)
45 views32 pages

Programming Languages: - Goal

This document discusses programming languages and how they work. It covers low-level languages that use simple instructions similar to a computer's control unit, as well as high-level languages that use more abstract terms. It notes that programming languages define the syntax and semantics needed to translate computational ideas into mechanical steps that a computer can execute. Python is highlighted as an interpreted, high-level language that will be used in examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views32 pages

Programming Languages: - Goal

This document discusses programming languages and how they work. It covers low-level languages that use simple instructions similar to a computer's control unit, as well as high-level languages that use more abstract terms. It notes that programming languages define the syntax and semantics needed to translate computational ideas into mechanical steps that a computer can execute. Python is highlighted as an interpreted, high-level language that will be used in examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Programming

 languages  
•  Goal:  
–  Need  a  way  to  describe  algorithmic  steps  such  
that  computer  can  use  them  to  execute  process  
–  Programming  language  defines  syntax  and  
seman;cs  needed  to  translate  our  computa;onal  
ideas  into  mechanical  steps  
Op;ons  for  programming  languages  

Source  code   Checker   Interpreter   Output  

Compiler   Object  code  


Op;ons  for  programming  languages  

Source  code   Checker   Interpreter   Output  


Low  level  

•  Low  level  language  uses  instruc;ons  similar  to  internal  


control  unit:  
–  Move  data  from  one  loca;on  to  another  
–  Execute  a  simple  ALU  opera;on  
–  Jump  to  new  point  in  sequence  based  on  test  
•  Checker  confirms  syntax,  sta;c  seman;cs  correct  
•  Interpreter  just  follows  sequence  of  simple  instruc;ons  
Op;ons  for  programming  languages  

Source  code   Checker   Interpreter   Output  


High  level  

Compiler   Object  code  

•  A  high  level  language  uses  more  abstract  terms  –  


invert  a  matrix,  compute  a  func;on  
•  In  a  compiled  language,  those  abstrac;ons  are  
converted  back  into  low  level  instruc;ons,  then  
executed  
Op;ons  for  programming  languages  

Source  code   Checker   Interpreter   Output  


High  level    

•  In  an  interpreted  language,  special  program  


converts  source  code  to  internal  data  
structure,  then  interpreter  sequen;ally  
converts  each  step  into  low  level  machine  
instruc;on  and  executes  
•  We  are  going  to  use  Python,  which  belongs  to  
this  class  of  programming  languages  
Python  programs  
•  Program  (or  script)  is  a  sequence  of  
defini&ons  and  commands  
–  Defini9ons  evaluated  and  commands  executed  by  
Python  interpreter  in  a  shell  
–  Can  be  typed  directly  into  a  shell,  or  stored  in  a  
file  that  is  read  into  the  shell  and  evaluated  
•  Command  (or  statement)  instructs  interpreter  
to  do  something  
Objects  
•  At  heart,  programs  will  manipulate  data  
objects  
•  Each  object  has  a  type  that  defines  the  kinds  
of  things  programs  can  do  to  it  
•  Objects  are:  
–  Scalar  (i.e.  cannot  be  subdivided),  or  
–  Non-­‐scalar  (i.e.  have  internal  structure  that  can  be  
accessed)  
Scalar  objects  
•  int –  used  to  represent  integers,  e.g.,  5 or  10082!
•  float –  used  to  represent  real  numbers,  e.g.,  3.14
or  27.0!
•  bool –  used  to  represent  Boolean  values  True and  
False!
•  The  built  in  Python  func9on  type returns  the  type  of  
an  object  
>>> type(3)!
<type ‘int’>!
>>> type(3.0)!
<type ‘float’>!
Expressions  
•  Objects  and  operators  can  be  combined  to  
form  expressions,  each  of  which  denotes  an  
object  of  some  type  
•  The  syntax  for  most  simple  expressions  is:  
–  <object>  <operator>  <object>  
Operators  on  ints  and  floats  
•  i + j –  sum  –  if  both  are  ints,  result  is  int,  
if  either  is  float,  result  is  float!
•  i - j –  difference  
•  i * j –  product  
•  i/j –  division  –  if  both  are  ints,  result  is  int,  
represen9ng  quo9ent  without  remainder  
•  i%j –  remainder  
•  i**j –  i  raised  to  the  power  of  j!
Some  simple  examples  
>>>  3  +  5  
8  
>>>  3.14  *  20  
62.8  
>>>  (2  +  3)*4  
20  
>>>  2  +  3*4  
14  
 
 
Performing  simple  opera9ons  
•  Parentheses  define  sub-­‐computa9ons  –  complete  
these  to  get  values  before  evalua9ng  larger  
expression  
–  (2+3)*4  
–  5*4  
–  20    
•  Operator  precedence:  
–  In  the  absence  of  parentheses  (within  which  
expressions  are  first  reduced),  operators  are  executed  
le[  to  right,  first  using  **,  then  *  and  /,  and  then  +
and  -!
Comparison  operators  on  ints  and  
floats    
•  i > j –  returns  True if  strictly  greater  
than  
•  i >= j –  returns  True if  greater  than  or  
equal  
•  i < j  
•  i <= j  
•  i == j –  returns  True if  equal  
•  i != j –  returns  True if  not  equal  
Operators  on  bools  
•  a and b is  True if  both  are  True  
•  a or b is  True if  at  least  one  is  True  
•  not a is  True if  a is  False;  it  is  False
if  a is  True  
Type  conversions  (type  cas9ng)  
•  We  can  o[en  convert  an  object  of  one  type  to  
another,  by  using  the  name  of  the  type  as  a  
func9on  
–  float(3) has  the  value  of  3.0!
–  int(3.9) truncates  to  3!
Simple  means  of  abstrac1on  
•  While  we  can  write  arbitrary  expressions,  it  is  
useful  to  give  names  to  values  of  expressions,  
and  to  be  able  to  reuse  those  names  in  place  
of  values  
•  pi = 3.14159!
•  radius = 11.2!
•  area = pi * (radius**2)!
Binding  variables  and  values  
•  The  statement  pi = 3.14159
assigns  the  name  pi to  the  
value  of  the  expression  to  the  
right  of  the  =!
•  Think  of  each  assignment  
statement  as  crea1ng  a  binding  
between  a  name  and  a  value  
stored  somewhere  in  the  
computer  
•  We  can  retrieve  the  value  
associated  with  a  name  or  
variable  by  simply  invoking  that  
name,  e.g.,  pi !
Changing  bindings  
•  Variable  names  can  be  
rebound,  by  invoking  new  
assignments  statements.  
•  For  example,  if  we  now  
execute:  
–  radius = 11.2!
•  we  get  the  diagram  
shown  here.  
•  Note  that  this  doesn’t  
change  the  value  
associated  with  area  
Non-­‐scalar  objects  
•  We  will  see  many  different  kinds  of  compound  
objects  
•  The  simplest  of  these  are  strings,  objects  of  type  
str!
•  Literals  of  type  string  can  be  wri?en  using  single  
or  double  quotes  
–  ‘abc’!
–  “abc”!
–  ‘123’ –  this  is  a  string  of  characters,  not  the  
number    
Operators  on  strings  
>>>  3  *  ‘a’  
‘aaa’  
>>>  ‘a’  +  ‘a’  
‘aa’  
>>>  ‘a’  +  str(3)  
‘a3’  
>>>  len(‘abc’)  
3  
ExtracMng  parts  of  strings  
•  Indexing:  
–  ‘abc’[0] returns  the  string  ‘a’!
–  ‘abc’[2] returns  the  string  ‘c’!
–  ‘abc’[3] is  an  error  (as  we  cannot  go  beyond  the  
boundaries  of  the  string)  
–  ‘abc’[-1] returns  the  string  ‘c’ (essenMally  
counMng  backwards  from  the  start  of  the  string)  
•  Slicing:  
–  If  s is  a  string,  the  expression  s[start:end]
denotes  the  substring  that  starts  at  start,  and  ends  
at  end-1!
•  ‘abc’[1:3] has  the  value  ‘bc’!
Programs  (or  scripts)  
•  While  we  can  type  expressions  directly  to  a  
Python  interpreter  (for  example  using  an  
interface  such  as  an  IDLE  shell),  in  general  we  will  
want  to  include  statements  in  a  program  file  
•  Execu@ng  an  expression  from  a  script  will  not  
produce  any  output;  for  that  we  need  statements  
(not  expressions),  such  as  
–  print(‘ab’)!
–  print(‘3’*3)!
Providing  input  
•  If  we  are  going  to  write  programs  or  scripts,  we  
will  need  a  way  to  incorporate  input  from  a  
user.  
•  We  use  the  Python  func@on  raw_input,  as  
in:  
>>> name = raw_input(‘Enter your name: ‘)!
Enter your name: Eric Grimson!
>>> print(‘Are you ‘ + name + ‘?’)!
Are you Eric Grimson?!
Some  simple  code  
One  can  use  variable  names  anywhere  you  
might  use  the  expression  whose  value  it  holds  
>>> myString = ‘Too much’!
>>> weather = ‘snow’!
>>> print(myString + ‘ ‘ +
weather)!
Too much snow!
A  straight  line  program  
Suppose  we  type  the  following  into  a  file,  and  load  it  into  a  
Python  IDLE  window  
 
x = 3!
x = x*x # square value of x!
print(x)!
y = float(raw_input('Enter a number: '))!
print(y*y)!
!
Then  we  observe  the  following  behavior  (where  I  type  a  4  
below)  
9!
Enter a number: 4!
16.0!
Some  observa@ons  
•  Comments  appear  aLer  a  #  
–  These  are  very  valuable,  as  they  help  a  user  
understand  decisions  the  programmer  has  made  in  
crea@ng  the  program  
–  Well  commented  code  should  be  very  readable  by  a  
user  
•  A  straight  line  program  simply  executes  each  
statement  in  order,  with  no  varia@on  in  order  
•  Most  programs  require  more  sophis@cated  flow  
control    
Branching  programs  
•  The  simplest  branching  
statement  is  a  condi&onal  
–  A  test  (expression  that  
evaluates  to  True or  
False)  
–  A  block  of  code  to  execute  
if  the  test  is  True!
–  An  op<onal  block  of  code  
to  execute  if  the  test  is  
False!
A  simple  example  
x = int(raw_input('Enter an integer: '))!
if x%2 == 0:!
print(‘’)!
print('Even')!
else:!
print(‘’)!
print('Odd')!
print(’Done with conditional')!
Some  observa<ons  
•  The  expression  x%2 == 0 evaluates  to  True
when  the  remainder  of  x divided  by  2 is  0!
•  Note  that  == is  used  for  comparison,  since  = is  
reserved  for  assignment  
•  The  indenta<on  is  important  –  each  indented  set  
of  expressions  denotes  a  block  of  instruc<ons  
–  For  example,  if  the  last  statement  were  indented,  it  
would  be  executed  as  part  of  the  else block  of  code  
•  Note  how  this  indenta<on  provides  a  visual  
structure  that  reflects  the  seman<c  structure  of  
the  program  
We  can  have  nested  condi<onals  
if x%2 == 0:!
if x%3 == 0:!
print('Divisible by 2 and 3’)!
else:!
print('Divisible by 2 and not by 3’)!
elif x%3 == 0:!
print('Divisible by 3 and not by 2’)!
And  we  can  use  compound  Booleans  
if x < y and x < z:!
print('x is least’)!
elif y < z:!
print('y is least’)!
else:!
print('z is least’) !
What  have  we  added?  
•  Branching  programs  allow  us  to  make  choices  
and  do  different  things  
•  But  s<ll  the  case  that  at  most,  each  statement  
gets  executed  once.  
•  So  maximum  <me  to  run  the  program  
depends  only  on  the  length  of  the  program  
•  These  programs  run  in  constant  &me  

You might also like