63
1 Computers and Programs A Whirlwind tour slides draw freely on those of previous Professors Flinn, Severance and Prakash

Computers and Programs

  • Upload
    nizana

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

Computers and Programs. A Whirlwind tour. The Basics. Programs perform computation on data Variables provide temporary locations to store data while the program is running Statements are the instructions for performing computations on data. Getting The Lecture Code. - PowerPoint PPT Presentation

Citation preview

Page 1: Computers and Programs

1

Computers and Programs

A Whirlwind tour

slides draw freely on those of previous Professors Flinn, Severance and Prakash

Page 2: Computers and Programs

2

The Basics

• Programs perform computation on data

• Variables provide temporary locations to store data while the program is running

• Statements are the instructions for performing computations on data

Page 3: Computers and Programs

3

Getting The Lecture Code

• In Eclipse, right-click on lectures project

• Team• Update to HEAD

Page 4: Computers and Programs

4

Variables

• Programs perform computations on variables• A variable provides a location to store a value• Examples:– x = 3 – y = 'Paul'

3'Paul'

xy

Page 5: Computers and Programs

5

Variables

• Programs perform computations on variables• A variable provides a location to store a value• Examples:– x = 3 – y = 'Paul'

• Subsequent statements can change a variable’s value by storing a new value– x = 4

3'Paul'

xy

4

Page 6: Computers and Programs

6

Variables (cont)

• Each variable has:– A name (uniquely defines each variable)– A value (which can be changed)– A type, which defines:• A range of possible values• The set of operations that can be done to that variable

Page 7: Computers and Programs

7

Variable Names / Identifiers

• Must start with a letter or _• Must consist of letters, numbers, and _’s– Good: spam eggs spam23– Bad: 23spam #sign var.12

• Case SenSitiVe– Different: spam Spam SPAM

Z-2.3.1

Page 8: Computers and Programs

8

Reserved Words

• You can not use reserved words as variable names / identifiers

and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print

Z-2.3.1

Page 9: Computers and Programs

9

Check Your Understanding (CYU)

• Which of the following are valid variable names?a. Paulb. paulc. jason42d. 42jasone. forf. forever

Page 10: Computers and Programs

10

Assignment and output

• A statement can assign a value to a variable:– Syntax: <variable name> = <value>– Example: class_name = '182'

Page 11: Computers and Programs

11

Types

• Some examples of data types:– 3, 5 Integer (whole numbers)– 4.0, -1.345 Floating point (rational numbers)– 'EECS 182' String– These are all atomic types

• Composite types also possible– [1, 2, 3]: a list of integers

• In time, you’ll define your own complex types

Page 12: Computers and Programs

12

Aside: Static vs. Dynamic Typing

• Python is dynamically-typed– An assignment may change the type of a variable• Var = 3 (type is integer)• Var = "Hello, there!" (type is string)

– I recommend you don’t do this!• Other languages (e.g., C) are statically-typed– Must declare variable’s type before using it• int Var = 3 (declares type as integer)• Var = "Hello, there!" (illegal statement)

Page 13: Computers and Programs

13

Expressions

• A statement can assign the value of an expression to a variable:– x = 3 + 4

• An expression can contain variables:– E.g., (x + 2) * 2

• The assigned variable can be in the expression– y = y + 1

Page 14: Computers and Programs

14

Operations on Types

• What did you learn?

Page 15: Computers and Programs

15

y = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6x

Evaluation of Expressionsy

Page 16: Computers and Programs

16

y = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6 0.6

0.6x

Evaluation of Expressionsy

Page 17: Computers and Programs

17

y = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6 0.6

0.4

0.6x

0.936

Evaluation of Expressionsy

Page 18: Computers and Programs

18

y = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6 0.6

0.4

0.6x

0.936

Evaluation of Expressions0.936y

Page 19: Computers and Programs

19

Assignment Happens in Slow Motion

• We can use the same variable on the left and right side of an assignment statement

• Remember that the right side is evaluated *before* the variable is updated

Z-34

Page 20: Computers and Programs

20

x = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6x

Evaluation of Expressions

Page 21: Computers and Programs

21

x = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6 0.6

0.6x

Evaluation of Expressions

Page 22: Computers and Programs

22

x = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6 0.6

0.4

0.6x

0.936

Evaluation of Expressions

Page 23: Computers and Programs

23

x = 3.9 * x * ( 1 - x )

Right side is an expression. The assignment statement :1. Plugs values of variables into expression2. Evaluates the expression.3. Sets value of variable on left to the result

0.6 0.6

0.4

0.6x

0.936

0.936

Evaluation of Expressions

Page 24: Computers and Programs

24

Incrementing

• x = 10• x = x + 1• print x

Page 25: Computers and Programs

25

Incrementing

• x = 10• x = x + 1• print x

10x

Page 26: Computers and Programs

26

Incrementing

• x = 10• x = x + 1• print x

10x 11

Page 27: Computers and Programs

27

Summary of Expressions

• Programming languages have lots of expressions

• Expression is anything that evaluates to a value

• Can be a string, number or virtually anything• Can be a single value or computed from

several values using operators

Z-2.3.2

Page 28: Computers and Programs

28

Examples of expressions

• 2.0 * pi * r• 'My name is ' + first + ' ' + last• 17• 'The temperature is ' + str ((fahrenheit – 32.0)

* 5.0 / 9.0) + ' degrees Celsius'• And so on…

Page 29: Computers and Programs

29

CYU

• What are the values of these expression? (3+7)*2 'Hello' + 'world' 'Hello' + 3

Page 30: Computers and Programs

30

Stored Programs

• Like a recipe or installation instructions, a program is a sequence of steps (called statements) to be done in order

• Some steps are conditional - they may be skipped• Sometimes a step or group of steps are to be

repeated• Sometimes we store a set of steps to be used

over and over as needed several places throughout the program

Z-14

Page 31: Computers and Programs

31

Output Statements• The print statement takes one or more expressions

separated by commas and prints the expressions to the screen separated by spaces.

x = 6print 2print 2 + xprint “Hello”, 4+5

28Hello 9

Z-2.4

Page 32: Computers and Programs

32

hw1.py

Page 33: Computers and Programs

33

CYU

• What will the output be?

x = 1y = 2x = yy = xprint x, y

x

y

Page 34: Computers and Programs

34

CYU

• What will the output be?

x = 1y = 2x = yy = xprint x, y

1x

y

Page 35: Computers and Programs

35

CYU

• What will the output be?

x = 1y = 2x = yy = xprint x, y

1x

2y

Page 36: Computers and Programs

36

CYU

• What will the output be?

x = 1y = 2x = yy = xprint x, y

1x 2

2y

Page 37: Computers and Programs

37

CYU

• What will the output be?

x = 1y = 2x = yy = xprint x, y

1x 2

2y 2

Page 38: Computers and Programs

38

Conditional Steps

Output:Program:

x = 5if x < 10: print "Smaller"if x > 20: print "Bigger"

x = 5

X < 10 ?

print “Smaller”

X > 20 ?

print “Bigger”

Z-199

Page 39: Computers and Programs

39

Conditional Steps

Output:Program:

x = 5if x < 10: print "Smaller"if x > 20: print "Bigger"

x = 5

X < 10 ?

print “Smaller”

X > 20 ?

print “Bigger”

Z-199

Page 40: Computers and Programs

40

Conditional Steps

Output:

Smaller

Program:

x = 5if x < 10: print "Smaller"if x > 20: print "Bigger"

x = 5

X < 10 ?

print “Smaller”

X > 20 ?

print “Bigger”

Z-199

Page 41: Computers and Programs

41

Conditional Steps

Output:

Smaller

Program:

x = 5if x < 10: print "Smaller"if x > 20: print "Bigger"

x = 5

X < 10 ?

print “Smaller”

X > 20 ?

print “Bigger”

Z-199

Page 42: Computers and Programs

42

Conditional Steps

Output:

Smaller

Program:

x = 5if x < 10: print "Smaller"if x > 20: print "Bigger"

x = 5

X < 10 ?

print “Smaller”

X > 20 ?

print “Bigger”

Z-199

Page 43: Computers and Programs

43

Conditional statement

• if <expression>:<one or more statements>

Read this as: if and only if the logical expression evaluates to true, execute the statement(s)

Page 44: Computers and Programs

44

Repeated Steps

Output:Program:

for i in range(5): print i

i = 0 .. 4

print i

Z-233

Page 45: Computers and Programs

45

Repeated Steps

Output:

01234

Program:

for i in range(5): print i

i = 0 .. 4

print i

Z-233

Page 46: Computers and Programs

46

Definite Loops

• Loops that run a fixed (i.e. definite) number of times

• Loops that “iterate” through an ordered set

• Loops that run “for” a number of times

for abc in range(5):print abc

Output:01234

Z-39

Page 47: Computers and Programs

47

Looking at In...

• range(n) produces a sequence of numbers from 0 to n-1

• The block of code is executed once for each value in the sequence

• The iteration variable moves through all of the values in the sequence

for val in range(5) : ... block of code ...

Iteration variable

Five-element sequence[ 0, 1, 2, 3, 4]

Page 48: Computers and Programs

48

Stored (and reused) Steps

Output:Program:

def hello(): print "Hello" print "Fun" hello()hello()

def

print "Hello"print “Fun”

hello()

hello()

We call these little stored chunks of code “subprograms” or “functions”or “procedures” or “methods”.

Page 49: Computers and Programs

49

Functions: Stored (and reused) Steps

Output:

HelloFunHelloFun

Program:

def hello(): print "Hello“ print "Fun" hello()hello()

def

print “Hello”print “Fun”

hello()

hello()

We call these little stored chunks of code “subprograms” or “functions”or “procedures” or “methods”.

Page 50: Computers and Programs

50

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 x

main()

Syntax: Code Blocks

• Code blocks are started by colon• Each line of block is indented• Ends on a statement that is less indented

For blockFn block

Page 51: Computers and Programs

51

CYU• What will the output be?

a) 021324

b) 234

c) 34

d) 23

e) 0203

Page 52: Computers and Programs

52

CYU• What will the output be?

a) 021324

b) 234

c) 34

d) 23

e) 0203

Page 53: Computers and Programs

53

A Python Program

Z-14

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 x

main()

Page 54: Computers and Programs

54

Stored steps

Calling the stored steps

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 x

main()

Page 55: Computers and Programs

55

Main functions

• Some languages require you to specify a “main” function– This is the function that will be executed when the

program runs– Python doesn’t require this, but I recommend you

use one anyway• Makes your code easier to understand• Makes it easier to transition to other languages

Page 56: Computers and Programs

56

Output

Input

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 x

main()

Page 57: Computers and Programs

57

Input Statements

• input(“Prompt”)– displays the prompt

and waits for us to input an expression - this works for numbers

• raw_input(“Prompt”)– Takes input as string– Does not evaluate it as

an expressionZ-34

Page 58: Computers and Programs

58

Repeated Code

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 x

main()

Page 59: Computers and Programs

59

def main():print "This program illustrates a chaotic function" x = input("Enter a number between 0 and 1: ")x = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print xx = 3.9 * x * (1 - x)print x

main()

Page 60: Computers and Programs

60

Comments in Python

• Anything after a # is ignored by Python• Why comment?– Describe what is going to happen in a sequence of

code– Document who wrote the code or other ancillary

information– Turn off a line of code - perhaps temporarily

Z-14

Page 61: Computers and Programs

61

ProcessInput Output

Z-28

# convert.py# A program to convert Celsius temps to Fahrenheit# by: Susan Computewell

def main(): celsius = input("What is the Celsius temperature? ") fahrenheit = (9.0 / 5.0) * celsius + 32 print "The temperature is ", fahrenheit, " degrees Fahrenheit."

main()

Page 62: Computers and Programs

62

Running the Program...

ProcessInput Output

>python2.5 convert.pyWhat is the Celsius temperature? 0The temperature is 32.0 degrees Fahrenheit.

>python2.5 convert.pyWhat is the Celsius temperature? 100The temperature is 212.0 degrees Fahrenheit.

>python2.5 convert.pyWhat is the Celsius temperature? 10The temperature is 50.0 degrees Fahrenheit.

Page 63: Computers and Programs

63

Summary

• That was a quick overview– We will revisit these concepts throughout the

course• Focus on the big picture– You're not supposed to be able (yet) to write the

kind of code that we looked at today