CSE 115 Introduction to Computer Science I Road map Review - - PowerPoint PPT Presentation

cse 115
SMART_READER_LITE
LIVE PREVIEW

CSE 115 Introduction to Computer Science I Road map Review - - PowerPoint PPT Presentation

CSE 115 Introduction to Computer Science I Road map Review range(stop) and range(start, stop, step) for in Python live coding / exercise Arrays An array is a collection of variables, with related names. var x = ['zero', 'one',


slide-1
SLIDE 1

CSE 115

Introduction to Computer Science I

slide-2
SLIDE 2

Road map

▶︎ Review ◀ range(stop) and range(start, stop, step) for in Python live coding / exercise

slide-3
SLIDE 3

Arrays

An array is a collection of variables, with related names. var x = ['zero', 'one', 'two']; The array consists of three variables, whose names are x[0], x[1], x[2]. The value of x[0] is 'zero', the value of x[1] is 'one', and the value of x[2] is 'two'. x.length reflects the length of the array, which is 3.

slide-4
SLIDE 4

name value

length

1 2

3 "zero" "one" "two"

name value x

slide-5
SLIDE 5

Control flow

for statement

for ( statementINIT ; expression ; statementUPDATE ) { statement0; statement1; … statementN; }

statementINIT statementN expression True False statementUPDATE statement1 statement0

...

A loop in the flow of control!

slide-6
SLIDE 6

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

4 Suppose we call sumTo(4)

slide-7
SLIDE 7

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum

4

slide-8
SLIDE 8

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 1

slide-9
SLIDE 9

sum = 0 i = 1 sum = sum + i return sum i <= max False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 1

True

because i <= max

slide-10
SLIDE 10

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 1 1

slide-11
SLIDE 11

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 1 2

slide-12
SLIDE 12

sum = 0 i = 1 sum = sum + i return sum i <= max False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 1 2

True

because i <= max

slide-13
SLIDE 13

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 3 2

slide-14
SLIDE 14

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 3 3

slide-15
SLIDE 15

sum = 0 i = 1 sum = sum + i return sum i <= max

True

False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 3 3 because i <= max

slide-16
SLIDE 16

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 6 3

slide-17
SLIDE 17

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 6 4

slide-18
SLIDE 18

sum = 0 i = 1 sum = sum + i return sum i <= max

True

False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 6 4 because i <= max

slide-19
SLIDE 19

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 10 4

slide-20
SLIDE 20

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 10 5

slide-21
SLIDE 21

sum = 0 i = 1 sum = sum + i return sum i <= max True False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 10 5

slide-22
SLIDE 22

sum = 0 i = 1 sum = sum + i return sum i <= max True

False

Control flow

visualizing repetition (for)

i = i + 1

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } name value

max

sum i

4 because i > max 10 5

slide-23
SLIDE 23

Road map

Review ▶︎ range(stop) and range(start, stop, step) ◀ for in Python live coding / exercise

slide-24
SLIDE 24

range

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops. class range(stop) class range(start, stop[, step]) The arguments to the range constructor must be integers […] If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop. For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.

Lightly edited from https://docs.python.org/3/library/stdtypes.html#range

slide-25
SLIDE 25

range examples

range(5) consists of the values 0, 1, 2, 3, and 4. range(3,7) consists of the values 3, 4, 5, and 6. range(3,7,2) consists of the values 3 and 5.

Keep in mind: a range does not include its "stop" value.

slide-26
SLIDE 26

Road map

Review range(stop) and range(start, stop, step) ▶︎ for in Python ◀ live coding / exercise

slide-27
SLIDE 27

for

https://docs.python.org/3/reference/compound_stmts.html#for

for var in sequence: statement0 statement1 ... statementN

slide-28
SLIDE 28

for

Python example

def sumTo(max): sum = 0 for i in range(1, max + 1): sum = sum + i return sum

A range does not include its "stop" value. range(1, 5) includes the values 1, 2, 3, and 4.

slide-29
SLIDE 29

for

JavaScript/Python comparison

function sumTo(max) { var sum = 0; for (var i=1; i<=max; i=i+1){ sum = sum + i; } return sum; } def sumTo(max): sum = 0 for i in range(1, max + 1): sum = sum + i return sum

slide-30
SLIDE 30

for

https://docs.python.org/3/reference/compound_stmts.html#for

for var in sequence: statement0 statement1 ... statementN

A range is one of many kinds of sequences in Python.

slide-31
SLIDE 31

sequences and for

Python example

def printSequence(seq): for x in seq: print(x) printSequence("this is a string") printSequence(range(5)) printSequence(['a', 'b', 'c'])

PYTHON The following are sequences: range, as in range(3,8) str, as in "this is a string" list, as in ['a','list','of','strings'] N.B. A list is not an array, but is similar in some ways.

slide-32
SLIDE 32

function printSequence(seq) { for (var i = 0; i < seq.length; i = i + 1){ console.log(seq[i]); } } printSequence("this is a string"); // JS doesn't have a native range printSequence(['a', 'b', 'c']); def printSequence(seq): for x in seq: print(x) printSequence("this is a string") printSequence(range(5)) printSequence(['a', 'b', 'c'])

sequences and for

JavaScript/Python comparison

slide-33
SLIDE 33

function printSequence(seq) { for (var i in seq) { console.log(seq[i]); } } printSequence("this is a string"); // JS doesn't have a native range printSequence(['a', 'b', 'c']); def printSequence(seq): for x in seq: print(x) printSequence("this is a string") printSequence(range(5)) printSequence(['a', 'b', 'c'])

JavaScript for (…in…)

slide-34
SLIDE 34

function printSequence_1(seq) { for (var i in seq) { console.log(seq[i]); } } printSequence_1(['a', 'b', 'c']); function printSequence_2(seq) { for (var i in seq) { console.log(i); } } printSequence_2(['a', 'b', 'c']);

i takes on index values, not contents, of sequence

a b c 1 2

slide-35
SLIDE 35

Road map

Review range(stop) and range(start, stop, step) for in Python ▶︎ live coding / exercise ◀

slide-36
SLIDE 36

exercise

Both Python and JavaScript allow the bracket notation to access elements of a string: "abc"[0] has value "a" "abc"[1] has value "b" Define a function which accepts a string as an argument and which returns a new string consisting

  • f every other character from the argument string,

starting with the character at position 0.