2
Jython secondary memory Jython processor Jython (main) memory 3 Jython secondary memory Jython processor foo: if Jython a<b :! (main) print a! memory else :! print b : pi : 3.141 dayofweek : tuesday 4
31 Integers -12 12 Mark Strings Barbara Ericson 85 5th Street NW 34,654.01 Floats 1.01 0.01 12.998 Pictures Inside the computer, these are all just bits 5 = name = expression foo: if a<b :! print a! else :! print b : foo: if a<b :! print a! else :! print b x = 43 : x: 43 dayofweek: tuesday pi : 3.141 pi : 3.141 dayofweek: tuesday 6
Names can be (nearly) whatever we want Must start with a letter or the _ character then, any combination of alphanumerical characters, plus the _ character Case matters Print is not the same as print mypicture is not the same as mypicture Be careful not to use Jython names as your own names Jython reserved words and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, lambda, not, or, pass, print, raise, return, try, while, yield 7 JES: Jython Environment for Students Installing JES and starting it up Go to http://code.google.com/p/mediacomp-jes/ and get the version of JES for your computer. If you know that you have a Java compiler (e.g., a JDK or an IDE ) Windows users: Just copy the folder Double-click JES application If trouble, try jes2.bat or jes-customjava.bat Mac users: Just copy the folder Double-click JES application 8
A simple editor (for entering in our programs or recipes): We ll call that the program area A command area for entering in commands for Python to execute. Program Area you are invited to use the Help menu lots of excellent help Command Area 9 JES with help displayed use the Window Layout menu to get the view you want 10
Python program + Python interpreter input data Computer Result(s) Java program input data Java compiler Computer ML program input data ML program Computer Result(s) 11 each time you write a command/expression in the command area and hit the <return> key, the Python interpreter executes/evaluates it think of it as a calculator always ready to run starting JES the >>> prompt examples of expression evaluation examples of variables creation and use 12
evaluating simple expressions try them with the JES interpreter >>> 34 + 56" >>> 34.1/46.5" >>> 22 * 33" >>> 14-15" Adding integers Dividing floats Multiplying integers Subtracting integers >>> "Hello" + "Mark"" Adding (concatenating) two strings 13 expressions with variables try again >>> value = 12 >>> value * 3 >>> value Defining a variable with integer value Using a variable in an expression The variable value does not change!!! >>> name = "Mark" >>> name * 3 Defining a variable with string value Using a variable in an expression >>> value = 20 >>> value * 3 >>> value = John It is only the = command that does change a variable value We can even change the type of value associated with a name >>> x = 10" >>> x = x - 3" What does it mean? 14
15 Names References Objects >>> a = 3" >>> b = a" >>> a = a+2" >>> a = ʻspamʼ" a" 3" b" 5" ʻspamʼ" 16
another Python command :! print expression >>> x = 20! >>> x + 3! >>> print x + 3! >>> name = ʻMaryʼ! >>> 2*name! >>> print 2*name! >>> expression makes Python evaluating it and displaying the Python internal representation of its value >>> print expression makes Python evaluating it and displaying the Python printable (external) representation of its value often the two look identical, but sometimes not" e.g., for strings " 17 math may be surprising sometimes try! >>> print 1.0/2.0! >>> print 1/2! if you only use integers (numbers without decimal point), Jython thinks you only want integers" caution : it works a bit differently with Python 3.x" trying new expressions >>> x = ʻAnneʼ! >>> y = ʻBobʼ! >>> print x/2! what do you expect to happen? >>> print x*y! 18
data type : a fundamental concept in computer science! building blocks data: integers ( 3, -47, 105, ) reals (0.482, -21.004, 5.0, ) strings ('bbbb', 'Mark', 'A door', ) operators: arithmetic (+, -, *, %, ) string (+, *, count, ) connected to each other by the data type concept 19! numbers strings (and more )" " -3 0.314 'spam' '$'!! data 45 23.2 'F' Guido s!!!!!!!!!! + - * /! + * count! % **!! replace! operators set of values + set of primitive operators = data type Python is a strongly typed language operators of a type can be applied ONLY to data belonging to that type Python raises an error in case of mismatching data and operators 20
functional definition of operators general form: op : dom x dom x dom some of these operators are expressed in infix notation, other in functional notation numbers (int and float) + : num x num num - : num x num num * : num x num num / : num x num num ** : num x num num % : num x num num abs : num num 21 functional definition of operators general form: op : dom x dom x dom strings some of these operators are expressed in infix notation, other in functional notation + : string x string string * : string x int string or * : int x string string count : string x string int find : string x string int 22