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

21
Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai 蔡蔡蔡蔡 () [email protected] Fall, 2015

Transcript of Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟)...

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

Xiaojuan Cai Computational Thinking 1

Lecture 2

Simple Programs

Xiaojuan Cai(蔡小娟)

[email protected]

Fall, 2015

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

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

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

Xiaojuan Cai Computational Thinking 3

Roadmap

• The software development

• Elements of program

• Names

• expressions

• Output statements

• Assignment statements

• Definite loop

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

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

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

Xiaojuan Cai Computational Thinking 5

Example

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

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

Xiaojuan Cai Computational Thinking 6

Roadmap

• The software development

• Elements of program

• Names

• Expressions

• Output statements

• Assignment statements

• Definite loop

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

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.

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

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_, …

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

Xiaojuan Cai Computational Thinking 9

Reserved words

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

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)

• …

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

Xiaojuan Cai Computational Thinking 11

Output statementprint

print <expr>

print <expr>, <expr>, …, <expr>

print <expr>, <expr>, …, <expr>,

• Syntax: <> is slots for other fragments, alternative

• … indicate any number of <expr>

• Semantics: evaluate <expr>, output the value

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

Xiaojuan Cai Computational Thinking 12

Assignment statement• Syntax: <var> = <expr>

• 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 + 110x

11

x 10

“x”

x = “x”

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

Xiaojuan Cai Computational Thinking 13

Assigning input• Syntax:

<variable> = input(<prompt>)

• <prompt>: prompt the user for input

• E.g., x = input("Enter a number ")

• Semantics:

evaluate <promt>, wait for an input and assign

the input to <variable>

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

Xiaojuan Cai Computational Thinking 14

Simultaneous assignment• Syntax:

<var1>,…,<varn> = <expr1>, …,<exprn>

• Semantics:

evaluate all the <expr>’s, assign the values

to corresponding <var>’s.

• E.g., x,y = 3,4; x,y = y,x

x,y = input(“…”)

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

Xiaojuan Cai Computational Thinking 15

Definite loop -- syntaxfor <var> in <sequence>:

<body>

• <var> is called loop index

• <body> is any sequence of statements,

indenting under the heading

• <sequence> consists of list of values

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

Xiaojuan Cai Computational Thinking 16

Loops

• A definite loop will execute definite

times.

• The most common definite loop is

counted loop.

• for <var> in range(<expr>): is

one of counted loops

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

Xiaojuan Cai Computational Thinking 17

Definite loop -- semantics

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

Xiaojuan Cai Computational Thinking 18

Example

• One more example – Future

values

• Susan在中国银行存了一笔钱,根据中国银行的年利率,她想知道 10年后她能取出多少钱。

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

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

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

Xiaojuan Cai Computational Thinking 20

Algorithm

Print an introduction

Input principal and apr

Repeat 10 times:

principal = principal * (1 + apr)

Output the principal

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

Xiaojuan Cai Computational Thinking 21

Conclusiondef 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