expression tree and objects 1. elements of python literals, strings, tuples, lists, … the order...

43
Expression Tree and Objects 1

Upload: norah-may

Post on 02-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

1

Expression Tree and Objects

2

Elements of Python

Literals, Strings, Tuples, Lists, …

The order of file reading

The order of execution

3

WHILE Loop

1. I = 0

2. WHILE I < 2:

3. I = I + 1

The order of execution is 1 2 3 2 3 2

4

IF statement1. I = 3

2. IF I < 3:

3. PRINT “A”

4. ELIF I == 3:

5. PRINT “B”

6. ELSE:

7. PRINT “C”

The order of execution is: 1 2 4 5 6

5

Quiz Time(~ 1:45 pm)

6

Guess what

1 I = 0

2 for e in get_names():

3 I = I + 1

1) What is the intention of the code writer?

2) What is the return type of get_names()?

3) What is the order of execution?

7

Guess what

What is the result of following expressions?

1. “five” + 3

2. 4 – “three”

3. 3 in 7

4. “one” += 1

8

Rules for operators

? ? ?

9

Semantics

I will buy a book.

I will go to USA.

I will go to a book.

I will buy USA.

10

Semantics

Close the lid operation ‘@’

@

11

Semantics

Even if a code is syntactically correct, the semantics

could be wrong.

The type system determines the correctness of

semantics.

1. “five” + 3

2. 4 – “three”

The above examples are semantically wrong.

12

Operators

Algebraic Operator produces a numeric type.

<integer> OP <integer> <integer>

<float> OP <float> <float>

Logical Operator produces boolean type (True|False)

<integer|float> OP <integer|float> True or False

<boolean>

<string> OP <string> True of False

Assignment Operator stores a value into a variable.

13

Expression

With the help of the type system, an expression is reduced

to another value.

Expression can appear as an input to any operator as long

as the type of its evaluation is compatible.

For example

<expression1> + <expression2> number | string

<expression3> % <expression3> number

Can you guess the type of expression1,2,3, and 4?

14

Expression: more examples

I = 0

for n in range(0,3):

I = I + 1

What is the type of the evaluation of range(0,3)?

15

Expression: more examples

1. a = <expression1> + 3

2. if <expression2>:

print “hello”

3. while <expression3>:

i = i + <expression4>

Guess the type of the evaluation result of

expression1,2,3,4

16

Expression: more examples

1. if my_function(x):

print x

2. while some_func(x,y,z) == 0:

print x+y+z

3. if 3 in gen_func(a,b,c):

print a,b,c

What is the return type of my_function(),

some_func(), and gen_func()?

17

The evaluation of Expressions

18

Expression Tree

Expression has a hierarchical structure.

3 + 4 * 9 n * factorial(n-1)

3

4 9

*

+

n factorial(n-1)

*

19

Revisiting the summation

Compute 1 + 2 + … + 10 = ((((((0+1)+2)+3)+4) … ) + 10)

20

Revisiting the summation

Variable

SUM1

SUM2

21

Revisiting the summation

(((((0+1)+2)+3)+4) … ) + 10

(((sum1+2)+3+4)…) + 10

(((sum2+3)+4)…+10

((sum3+4)+…+10

sum9+10

sum10

22

Revisiting the summation

For each summation, we need 1, 2, 3, …, 10 in this

order.

Suppose that the variable I will have that value

inside a for loop.

Do you have an idea to represent this with FOR or

WHILE?

23

Revisiting the summation

SUM0 = 0

SUM1 = SUM0 + 1

SUM2 = SUM1 + 2

SUM3 = SUM2 + 3

SUM10 = SUM9 + 10

Let’s replace with a variable n

SUM0 = 0n = 1SUM1 = SUM0 + nn = 2SUM2 = SUM1 + nn = 3SUM3 = SUM2 + nn = n + 1SUM4 = SUM3 + nn = n + 1SUM5 = SUM4 + n…n = n + 1SUM10 = SUM9 + n

24

Eureka!

We found a pattern!

It was …

n = n + 1

25

Now

The summation is …

SUM0 = 0

n = 1

SUM1 = SUM0 + n

n = n + 1

SUM2 = SUM1 + n

n = n + 1

SUM10 = SUM9 + n

Wait!Do we use SUMx later?

Nope!

What happen if I overwrite the variable?

26

More patterns!

The summation is …

SUM = 0

n = 1

SUM = SUM + n

n = n + 1

SUM = SUM + n

n = n + 1

SUM = SUM + n

n = n + 1

It is all the same!

How many repetitions?

I believe you can do with FOR or WHILE.

27

Revisiting the summation

Compute 1 + 2 + … + 10 = ((((((0+1)+2)+3)+4) … )

+ 10)

The summation by looping is same as …

sum = 0

sum = sum + 1

sum = sum + 2

sum = sum + 3

28

Revisiting the summation

sum = 0 + 1

sum = (0 + 1) + 2

sum = ((0 + 1) + 2) + 3

sum = (((((0+1)+2)+3)+4) … ) + 10

29

Revisiting the summation

sum = 0

for x in [1,2,3,4,5,6,7,8,9,10]:

sum = sum + x

After the loop unrolling …

sum = 0sum = sum + 1sum = sum + 2…sum = sum + 10

30

Finding the minimum

[3,1,2,4] What is the minimum?

Assume that a function min(x,y) returns a minimum

between x and y. Then, 1) how do you implement

min(x,y); 2) how to compute the minimum?

The answer is min(min(min(3,1),2),4)

31

List and its operations, and Object

32

Mapping of a list

Suppose a math function f(x). Write a function to create a

list of sample values of f(x) where x is between 1 and 10.

The input: [ 1, 2, …, 10 ] range(1, 11)

The Output: [ f(1), f(2), …, f(10) ]

L = []

for e in range(1,11):

L.append(f(e))print L

33

Object

An object in Python is a combination of data and

functions.

An object can be stored in a variable, which

means we can use an assignment operator.

L = [ 1,2,3 ]

Using ‘.’ operator, we can call the function of a

given object; For example, L.append(4)

34

Object and Function

A typical function works only with a given data.

Many functions works with a collection of specific

data.

Suppose that we have a function sum(L) that takes

a list and return a number; sum(<list>)

<number>

It might be simpler for some people to use L.sum()

instead of sum(L).

35

Dot Operator

The dot operator changes the way of function calling.

<object> . <function name>(<parameters>)

<function name>(<object>, <parameters>)

sum(L) L.sum()

36

Append() function

L = []

for e in range(0,3):

L.append(e)

produces L = [0,1,2]

def my_append(L, e):

return L + [e]

L = my_append(L, e)

<list> + <list> <list>

37

Collecting data with a list

Find all even numbers between 1 and 10.

Two ways are possible:

1. Generation of even numbers:

n = 0

n = n + 2

2. Discrimination of any number:

test if n % 2 == 0

38

Collecting even numbersL = []n = 0while n < 11:

n = n + 2L.append(n)

print L

L = []

for e in range(1,11):

if e%2 == 0:

L.append(e)

print L

Generation of 2’s multiples

Testing if it is 2’s multiples

39

Generation vs. Discrimination

In general, generative methods are faster and

clear.

However, those generative methods are not always

available.

For example, finding prime numbers! There is no

function f(n) where f(n) is the prime number close

to a number n.

Discriminative methods are well suited to a

computer.

40

41

# find_minimum(number,number) ==> numberdef find_minimum(x,y): if x > y: return y else: return x

# (list) ==> numberdef find_minimum_of_list(L): return 0

print find_minimum(find_minimum(L[0],L[1]),L[2])

42

List Indexing

Ordering elements in a list

Advanced list handling

Finding the minimum and maximum

43

The inner product

The operation between two vectors: