Download presentation
Presentation is loading. Please wait.
Published byNorah May Modified over 9 years ago
1
Expression Tree and Objects 1
2
Elements of Python Literals, Strings, Tuples, Lists, … The order of file reading The order of execution 2
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 3
4
IF statement 1. 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 4
5
Quiz Time (~ 1:45 pm) 5
6
Guess what 1I = 0 2for e in get_names(): 3I = 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? 6
7
Guess what What is the result of following expressions? 1. “five” + 3 2. 4 – “three” 3. 3 in 7 4. “one” += 1 7
8
Rules for operators ??? 8
9
Semantics I will buy a book. I will go to USA. I will go to a book. I will buy USA. 9
10
Semantics Close the lid operation ‘@’ @ 10
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. 11
12
Operators Algebraic Operator produces a numeric type. OP OP Logical Operator produces boolean type (True|False) OP True or False OP True of False Assignment Operator stores a value into a variable. 12
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 + number | string % number Can you guess the type of expression1,2,3, and 4? 13
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)? 14
15
Expression: more examples 1.a = + 3 2.if : print “hello” 3.while : i = i + Guess the type of the evaluation result of expression1,2,3,4 15
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()? 16
17
The evaluation of Expressions 17
18
Expression Tree Expression has a hierarchical structure. 3 + 4 * 9 n * factorial(n-1) 3 49 * + n factorial(n-1) * 18
19
Revisiting the summation Compute 1 + 2 + … + 10 = ((((((0+1)+2)+3)+4) … ) + 10) 19
20
Revisiting the summation Variable 20 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 21
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? 22
23
Revisiting the summation SUM0 = 0 SUM1 = SUM0 + 1 SUM2 = SUM1 + 2 SUM3 = SUM2 + 3 … SUM10 = SUM9 + 10 23 Let’s replace with a variable n SUM0 = 0 n = 1 SUM1 = SUM0 + n n = 2 SUM2 = SUM1 + n n = 3 SUM3 = SUM2 + n n = n + 1 SUM4 = SUM3 + n n = n + 1 SUM5 = SUM4 + n … n = n + 1 SUM10 = SUM9 + n
24
Eureka! We found a pattern! 24 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 25 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 26 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 … 27
28
Revisiting the summation sum = 0 + 1 sum = (0 + 1) + 2 sum = ((0 + 1) + 2) + 3 … sum = (((((0+1)+2)+3)+4) … ) + 10 28
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 = 0 sum = sum + 1 sum = sum + 2 … sum = sum + 10 29
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) 30
31
List and its operations, and Object 31
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 32
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) 33
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( ) It might be simpler for some people to use L.sum() instead of sum(L). 34
35
Dot Operator The dot operator changes the way of function calling.. ( ) (, ) sum(L) L.sum() 35
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) + 36
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 37
38
Collecting even numbers L = [] n = 0 while n < 11: n = n + 2 L.append(n) print L L = [] for e in range(1,11): if e%2 == 0: L.append(e) print L 38 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. 39
40
40
41
41 # find_minimum(number,number) ==> number def find_minimum(x,y): if x > y: return y else: return x # (list) ==> number def find_minimum_of_list(L): return 0 print find_minimum(find_minimum(L[0],L[1]),L[2])
42
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: 43
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.