 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

 Computer Science 1MD3 Introduction to Programming Winter 2014.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Adapted from John Zelle’s Book Slides
 Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Lists Introduction to Computing Science and Programming I.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Chapter 2 Writing Simple Programs
Python Control of Flow.
 1 String and Data Processing. Sequential Processing Processing each element in a sequence for e in [1,2,3,4]: print e for c in “hello”: print c for.
 1 Loop and Set operations. Creating a loop Loops are used to repeat a computation. Let’s see the example of factorial computation. Let’s compute 10!
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
An Introduction to Textual Programming
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
CIS Computer Programming Logic
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
ADVANCED EV3 PROGRAMMING LESSON
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Variables and Expressions CMSC 201 Chang (rev )
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
More Functions with Return Values CS303E: Elements of Computers and Programming.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Language Find the latest version of this document at
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Control flow Ruth Anderson UW CSE 160 Spring
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Control flow Ruth Anderson UW CSE 160 Winter
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Chapter 2 Writing Simple Programs
G. Pullaiah College of Engineering and Technology
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Making Choices with if Statements
Python: Control Structures
Organization of Programming Languages
While Loops in Python.
Ruth Anderson UW CSE 160 Winter 2017
Logical Operators, Boolean Data Types
Conditions and Ifs BIS1523 – Lecture 8.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Ruth Anderson UW CSE 140 Winter 2014
Loops CIS 40 – Introduction to Programming in Python
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Introduction to Programming Using Python PART 2
Python Basics with Jupyter Notebook
Types, Truth, and Expressions
While Loops in Python.
REPETITION Why Repetition?
Lecture 6 - Recursion.
Presentation transcript:

 Expression Tree and Objects 1

Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2

WHILE Loop 1. I = 0 2. WHILE I < 2: 3. I = I + 1 The order of execution is

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:

Quiz Time (~ 1:45 pm) 5

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

Guess what What is the result of following expressions? 1. “five” – “three” 3. 3 in 7 4. “one” += 1 7

Rules for operators ??? 8

Semantics I will buy a book. I will go to USA. I will go to a book. I will buy USA. 9

Semantics  Close the lid 10

Semantics  Even if a code is syntactically correct, the semantics could be wrong.  The type system determines the correctness of semantics. 1. “five” – “three”  The above examples are semantically wrong. 11

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

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

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

Expression: more examples 1.a = if : print “hello” 3.while : i = i + Guess the type of the evaluation result of expression1,2,3,4 15

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

The evaluation of Expressions 17

Expression Tree  Expression has a hierarchical structure * 9 n * factorial(n-1) 3 49 * + n factorial(n-1) * 18

Revisiting the summation  Compute … + 10 = ((((((0+1)+2)+3)+4) … ) + 10) 19

Revisiting the summation  Variable 20 SUM1 SUM2

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

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

Revisiting the summation SUM0 = 0 SUM1 = SUM0 + 1 SUM2 = SUM1 + 2 SUM3 = SUM2 + 3 … SUM10 = SUM 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

Eureka!  We found a pattern! 24 It was … n = n + 1

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?

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 It is all the same! How many repetitions? I believe you can do with FOR or WHILE.

Revisiting the summation Compute … + 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

Revisiting the summation sum = sum = (0 + 1) + 2 sum = ((0 + 1) + 2) + 3 … sum = (((((0+1)+2)+3)+4) … )

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

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

List and its operations, and Object 31

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

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

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

Dot Operator The dot operator changes the way of function calling.. ( ) (, ) sum(L)  L.sum() 35

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

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 Discrimination of any number: test if n % 2 == 0 37

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

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

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 List Indexing Ordering elements in a list Advanced list handling Finding the minimum and maximum

The inner product The operation between two vectors: 43