Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) Fall, 2015.

Similar presentations


Presentation on theme: "Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) Fall, 2015."— Presentation transcript:

1 Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

2 Xiaojuan Cai Computational Thinking 2 Objectives To be able to understand and write Python statements : to output information to the screen, assign values to variables, get numeric information entered, and perform a counted loop

3 Xiaojuan Cai Computational Thinking 3 Roadmap The software development Elements of program Names expressions Output statements Assignment statements Definite loop

4 Xiaojuan Cai Computational Thinking 4 Software development Analyze the problem Determine specifications Create a design (algorithm) Implement the design Test/Debug the program Maintain the program

5 Xiaojuan Cai Computational Thinking 5 Example Susan 刚刚来到中国,她什么都喜欢,唯独 不喜欢中国的天气预报,因为她习惯华氏度 ( Fahrenheit ),不习惯摄氏度( Celsius )。 她请求你的帮助,她告诉你,华氏和摄氏之 间的转换是线性的,而且 0 摄氏度 =32 华氏度, 100 摄氏度 =212 华氏度。

6 Xiaojuan Cai Computational Thinking 6 Roadmap The software development Elements of program Names Expressions Output statements Assignment statements Definite loop

7 Xiaojuan Cai Computational Thinking 7 Names File (module) has name, e.g. converter Function has name, e.g. main Variable has name, e.g. Celsius Names are called identifier.

8 Xiaojuan Cai Computational Thinking 8 Identifier begin with a letter or underscore _ followed by any sequence of letters, digits, or underscores. Case sensitive All these are legal identifiers: x, celsius, Spam, SPAM, x2, x_2, _x_2_, …

9 Xiaojuan Cai Computational Thinking 9 Reserved words

10 Xiaojuan Cai Computational Thinking 10 Expressions The fragments of code that produce or calculate new data values are called expressions, including literals, e.g. 3.9, 32 identifier, e.g. celsius, x combining simple ones with operators, e.g. 3.9*x*(1-x) function calls (later in this course) …

11 Xiaojuan Cai Computational Thinking 11 Output statement print print,, …, print,, …,, Syntax: <> is slots for other fragments, alternative … indicate any number of Semantics: evaluate, output the value

12 Xiaojuan Cai Computational Thinking 12 Assignment statement Syntax: = E.g., x = 3.9 * x * (1-x), x = 2 A variable can be assigned many times, with any values (of any types) Semantics: x = x + 1 10 x 11 x 10 “x” x = “x”

13 Xiaojuan Cai Computational Thinking 13 Assigning input Syntax: = input( ) : prompt the user for input E.g., x = input("Enter a number ") Semantics: evaluate, wait for an input and assign the input to

14 Xiaojuan Cai Computational Thinking 14 Simultaneous assignment Syntax:,…, =, …, Semantics: evaluate all the ’s, assign the values to corresponding ’s. E.g., x,y = 3,4; x,y = y,x x,y = input(“…”)

15 Xiaojuan Cai Computational Thinking 15 Definite loop -- syntax for in : is called loop index is any sequence of statements, indenting under the heading consists of list of values

16 Xiaojuan Cai Computational Thinking 16 Loops A definite loop will execute definite times. The most common definite loop is counted loop. for in range( ): is one of counted loops

17 Xiaojuan Cai Computational Thinking 17 Definite loop -- semantics

18 Xiaojuan Cai Computational Thinking 18 Example One more example – Future values Susan 在中国银行存了一笔钱,根据 中国银行的年利率,她想知道 10 年后 她能取出多少钱。

19 Xiaojuan Cai Computational Thinking 19 Formalize Program Future value Inputs: principal the amount of money initially apr the annual percentage rate Output: the value after 10 years

20 Xiaojuan Cai Computational Thinking 20 Algorithm Print an introduction Input principal and apr Repeat 10 times: principal = principal * (1 + apr) Output the principal

21 Xiaojuan Cai Computational Thinking 21 Conclusion def main(): print "This program illustrates a chaotic function" x = input("Enter a number between 0 and 1: ") for i in range (10): x = 3.9 * x * (1-x) print i,x main() Circle each identifier. Underline each expression. indicate the type of statement on each line


Download ppt "Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) Fall, 2015."

Similar presentations


Ads by Google