Computers and Programs

Preview:

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

1

Computers and Programs

A Whirlwind tour

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

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

3

Getting The Lecture Code

• In Eclipse, right-click on lectures project

• Team• Update to HEAD

4

Variables

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

3'Paul'

xy

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

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

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

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

9

Check Your Understanding (CYU)

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

10

Assignment and output

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

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

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)

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

14

Operations on Types

• What did you learn?

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

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

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

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

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

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

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

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

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

24

Incrementing

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

25

Incrementing

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

10x

26

Incrementing

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

10x 11

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

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…

29

CYU

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

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

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

32

hw1.py

33

CYU

• What will the output be?

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

x

y

34

CYU

• What will the output be?

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

1x

y

35

CYU

• What will the output be?

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

1x

2y

36

CYU

• What will the output be?

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

1x 2

2y

37

CYU

• What will the output be?

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

1x 2

2y 2

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

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

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

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

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

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)

44

Repeated Steps

Output:Program:

for i in range(5): print i

i = 0 .. 4

print i

Z-233

45

Repeated Steps

Output:

01234

Program:

for i in range(5): print i

i = 0 .. 4

print i

Z-233

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

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]

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”.

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”.

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

51

CYU• What will the output be?

a) 021324

b) 234

c) 34

d) 23

e) 0203

52

CYU• What will the output be?

a) 021324

b) 234

c) 34

d) 23

e) 0203

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()

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()

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

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()

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

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()

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()

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

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()

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.

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

Recommended