csci 127: introduction to computer sciencecsci 127 (hunter) lecture 9 14 april 202019/39 third part:...

86
CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 9 14 April 2020 1 / 39

Upload: others

Post on 30-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

CSci 127: Introduction to Computer Science

hunter.cuny.edu/csci

CSci 127 (Hunter) Lecture 9 14 April 2020 1 / 39

Page 2: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Announcements

We are back from Spring Break (and all otherbreaks)There should be no more interruptions in ourschedule

Please refer to the updated schedule

If you have consented to participate in theEducational Psychology study,fill in the 3-question survey after watching allthe videos (link also provided at end oflecture)

CSci 127 (Hunter) Lecture 9 14 April 2020 2 / 39

Page 3: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Announcements

We are back from Spring Break (and all otherbreaks)There should be no more interruptions in ourschedule

Please refer to the updated schedule

If you have consented to participate in theEducational Psychology study,fill in the 3-question survey after watching allthe videos (link also provided at end oflecture)

CSci 127 (Hunter) Lecture 9 14 April 2020 2 / 39

Page 4: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Announcements

We are back from Spring Break (and all otherbreaks)There should be no more interruptions in ourschedule

Please refer to the updated schedule

If you have consented to participate in theEducational Psychology study,fill in the 3-question survey after watching allthe videos (link also provided at end oflecture)

CSci 127 (Hunter) Lecture 9 14 April 2020 2 / 39

Page 5: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Today’s Topics

Recap: Functions & Top Down Design

Mapping GIS Data

Random Numbers

Indefinite Loops

CSci 127 (Hunter) Lecture 9 14 April 2020 3 / 39

Page 6: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Today’s Topics

Recap: Functions & Top Down Design

Mapping GIS Data

Random Numbers

Indefinite Loops

CSci 127 (Hunter) Lecture 9 14 April 2020 4 / 39

Page 7: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

What are the formal parameters for the functions?

What is the output of:r = prob4(4,"city")

print("Return: ", r)

What is the output of:r = prob4(2,"university")

print("Return: ", r)

CSci 127 (Hunter) Lecture 9 14 April 2020 5 / 39

Page 8: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

What are the formal parameters for the functions?

CSci 127 (Hunter) Lecture 9 14 April 2020 6 / 39

Page 9: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

What are the formal parameters for the functions?

CSci 127 (Hunter) Lecture 9 14 April 2020 7 / 39

Page 10: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

What is the output of:r = prob4(4,"city")

print("Return: ", r)

What is the output of:r = prob4(2,"university")

print("Return: ", r)

CSci 127 (Hunter) Lecture 9 14 April 2020 8 / 39

Page 11: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 9 14 April 2020 9 / 39

Page 12: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 13: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 14: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 15: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 16: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 17: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 18: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

http://koalastothemax.com

CSci 127 (Hunter) Lecture 9 14 April 2020 10 / 39

Page 19: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

Process:

Get template → Fill in missing → Test locally → Submit tofrom github → functions → idle3/python3 → Gradescope

CSci 127 (Hunter) Lecture 9 14 April 2020 11 / 39

Page 20: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

The main() is written for you.

Only fill in two functions: average() and setRegion().

CSci 127 (Hunter) Lecture 9 14 April 2020 12 / 39

Page 21: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

The main() is written for you.

Only fill in two functions: average() and setRegion().

CSci 127 (Hunter) Lecture 9 14 April 2020 12 / 39

Page 22: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

From Last Time: koalas

The main() is written for you.

Only fill in two functions: average() and setRegion().

CSci 127 (Hunter) Lecture 9 14 April 2020 12 / 39

Page 23: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Top-Down Design

The last example demonstratestop-down design: breaking intosubproblems, and implementing eachpart separately.

I Break the problem into tasks for a“To Do” list.

I Translate list into function names &inputs/returns.

I Implement the functions, one-by-one.

Excellent approach since you can thentest each part separately before addingit to a large program.

Very common when working with ateam: each has their own functions toimplement and maintain.

CSci 127 (Hunter) Lecture 9 14 April 2020 13 / 39

Page 24: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Top-Down Design

The last example demonstratestop-down design: breaking intosubproblems, and implementing eachpart separately.

I Break the problem into tasks for a“To Do” list.

I Translate list into function names &inputs/returns.

I Implement the functions, one-by-one.

Excellent approach since you can thentest each part separately before addingit to a large program.

Very common when working with ateam: each has their own functions toimplement and maintain.

CSci 127 (Hunter) Lecture 9 14 April 2020 13 / 39

Page 25: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Top-Down Design

The last example demonstratestop-down design: breaking intosubproblems, and implementing eachpart separately.

I Break the problem into tasks for a“To Do” list.

I Translate list into function names &inputs/returns.

I Implement the functions, one-by-one.

Excellent approach since you can thentest each part separately before addingit to a large program.

Very common when working with ateam: each has their own functions toimplement and maintain.

CSci 127 (Hunter) Lecture 9 14 April 2020 13 / 39

Page 26: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Top-Down Design

The last example demonstratestop-down design: breaking intosubproblems, and implementing eachpart separately.

I Break the problem into tasks for a“To Do” list.

I Translate list into function names &inputs/returns.

I Implement the functions, one-by-one.

Excellent approach since you can thentest each part separately before addingit to a large program.

Very common when working with ateam: each has their own functions toimplement and maintain.

CSci 127 (Hunter) Lecture 9 14 April 2020 13 / 39

Page 27: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Top-Down Design

The last example demonstratestop-down design: breaking intosubproblems, and implementing eachpart separately.

I Break the problem into tasks for a“To Do” list.

I Translate list into function names &inputs/returns.

I Implement the functions, one-by-one.

Excellent approach since you can thentest each part separately before addingit to a large program.

Very common when working with ateam: each has their own functions toimplement and maintain.

CSci 127 (Hunter) Lecture 9 14 April 2020 13 / 39

Page 28: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Top-Down Design

The last example demonstratestop-down design: breaking intosubproblems, and implementing eachpart separately.

I Break the problem into tasks for a“To Do” list.

I Translate list into function names &inputs/returns.

I Implement the functions, one-by-one.

Excellent approach since you can thentest each part separately before addingit to a large program.

Very common when working with ateam: each has their own functions toimplement and maintain.

CSci 127 (Hunter) Lecture 9 14 April 2020 13 / 39

Page 29: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write the missing functions for the program:

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.

CSci 127 (Hunter) Lecture 9 14 April 2020 14 / 39

Page 30: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write the missing functions for the program:

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.

CSci 127 (Hunter) Lecture 9 14 April 2020 15 / 39

Page 31: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Group Work: Fill in Missing Pieces

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.

CSci 127 (Hunter) Lecture 9 14 April 2020 16 / 39

Page 32: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Group Work: Fill in Missing Pieces

1 Write import statements.

import turtle

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.

CSci 127 (Hunter) Lecture 9 14 April 2020 17 / 39

Page 33: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Third Part: Fill in Missing Pieces

1 Write import statements.

2 Write down new function names and inputs.

import turtle

def setUp():

#FILL IN

def getInput():

#FILL IN

def markLocation(t,x,y):

#FILL IN

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.

CSci 127 (Hunter) Lecture 9 14 April 2020 18 / 39

Page 34: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Third Part: Fill in Missing Pieces1 Write import statements.

2 Write down new function names and inputs.

3 Fill in return values.

import turtle

def setUp():

#FILL IN

return(newTurtle)

def getInput():

#FILL IN

return(x,y)

def markLocation(t,x,y):

#FILL IN

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.

CSci 127 (Hunter) Lecture 9 14 April 2020 19 / 39

Page 35: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Third Part: Fill in Missing Pieces1 Write import statements.

2 Write down new function names and inputs.

3 Fill in return values.

4 Fill in body of functions.

import turtle

def setUp():

newTurtle = turtle.Turtle()newTurtle.penup()return(newTurtle)

def getInput():

x = int(input(’Enter x: ’))y = int(input(’Enter y: ’))return(x,y)

def markLocation(t,x,y):

t.goto(x,y)t.stamp()

def main():

tess = setUp() #Returns a purple turtle with pen up.

for i in range(5):

x,y = getInput() #Asks user for two numbers.

markLocation(tess,x,y) #Move tess to (x,y) and stamp.CSci 127 (Hunter) Lecture 9 14 April 2020 20 / 39

Page 36: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write a function that takes a number as an input and prints itscorresponding name.

For example,

I num2string(0) returns: zeroI num2string(1) returns: oneI num2string(2) returns: two

You may assume that only single digits, 0,1,...,9, are given as input.

CSci 127 (Hunter) Lecture 9 14 April 2020 21 / 39

Page 37: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write a function that takes a number as an input and prints itscorresponding name.

For example,

I num2string(0) returns: zeroI num2string(1) returns: oneI num2string(2) returns: two

You may assume that only single digits, 0,1,...,9, are given as input.

CSci 127 (Hunter) Lecture 9 14 April 2020 21 / 39

Page 38: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write a function that takes a number as an input and prints itscorresponding name.

For example,

I num2string(0) returns: zero

I num2string(1) returns: oneI num2string(2) returns: two

You may assume that only single digits, 0,1,...,9, are given as input.

CSci 127 (Hunter) Lecture 9 14 April 2020 21 / 39

Page 39: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write a function that takes a number as an input and prints itscorresponding name.

For example,

I num2string(0) returns: zeroI num2string(1) returns: one

I num2string(2) returns: two

You may assume that only single digits, 0,1,...,9, are given as input.

CSci 127 (Hunter) Lecture 9 14 April 2020 21 / 39

Page 40: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write a function that takes a number as an input and prints itscorresponding name.

For example,

I num2string(0) returns: zeroI num2string(1) returns: oneI num2string(2) returns: two

You may assume that only single digits, 0,1,...,9, are given as input.

CSci 127 (Hunter) Lecture 9 14 April 2020 21 / 39

Page 41: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Write a function that takes a number as an input and prints itscorresponding name.

For example,

I num2string(0) returns: zeroI num2string(1) returns: oneI num2string(2) returns: two

You may assume that only single digits, 0,1,...,9, are given as input.

CSci 127 (Hunter) Lecture 9 14 April 2020 21 / 39

Page 42: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python Tutor

(On github)

CSci 127 (Hunter) Lecture 9 14 April 2020 22 / 39

Page 43: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Today’s Topics

Recap: Functions & Top Down Design

Mapping GIS Data

Random Numbers

Indefinite Loops

CSci 127 (Hunter) Lecture 9 14 April 2020 23 / 39

Page 44: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

CSci 127 (Hunter) Lecture 9 14 April 2020 24 / 39

Page 45: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

A module for making HTML maps.

It’s a Python interface to the popularleaflet.js.

Outputs .html files which you can open in abrowser.

An extra step:

Write → Run → Open .htmlcode. program. in browser.

CSci 127 (Hunter) Lecture 9 14 April 2020 25 / 39

Page 46: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

A module for making HTML maps.

It’s a Python interface to the popularleaflet.js.

Outputs .html files which you can open in abrowser.

An extra step:

Write → Run → Open .htmlcode. program. in browser.

CSci 127 (Hunter) Lecture 9 14 April 2020 25 / 39

Page 47: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

A module for making HTML maps.

It’s a Python interface to the popularleaflet.js.

Outputs .html files which you can open in abrowser.

An extra step:

Write → Run → Open .htmlcode. program. in browser.

CSci 127 (Hunter) Lecture 9 14 April 2020 25 / 39

Page 48: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

A module for making HTML maps.

It’s a Python interface to the popularleaflet.js.

Outputs .html files which you can open in abrowser.

An extra step:

Write → Run → Open .htmlcode. program. in browser.

CSci 127 (Hunter) Lecture 9 14 April 2020 25 / 39

Page 49: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

A module for making HTML maps.

It’s a Python interface to the popularleaflet.js.

Outputs .html files which you can open in abrowser.

An extra step:

Write → Run → Open .htmlcode. program. in browser.

CSci 127 (Hunter) Lecture 9 14 April 2020 25 / 39

Page 50: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Demo

(Map created by Folium.)

CSci 127 (Hunter) Lecture 9 14 April 2020 26 / 39

Page 51: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

To use:

import folium

Create a map:

myMap = folium.Map()

Make markers:

newMark = folium.Marker([lat,lon],popup=name)

Add to the map:

newMark.add to(myMap)

Many options to customize background map (“tiles”)and markers.

CSci 127 (Hunter) Lecture 9 14 April 2020 27 / 39

Page 52: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

To use:

import folium

Create a map:

myMap = folium.Map()

Make markers:

newMark = folium.Marker([lat,lon],popup=name)

Add to the map:

newMark.add to(myMap)

Many options to customize background map (“tiles”)and markers.

CSci 127 (Hunter) Lecture 9 14 April 2020 27 / 39

Page 53: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

To use:

import folium

Create a map:

myMap = folium.Map()

Make markers:

newMark = folium.Marker([lat,lon],popup=name)

Add to the map:

newMark.add to(myMap)

Many options to customize background map (“tiles”)and markers.

CSci 127 (Hunter) Lecture 9 14 April 2020 27 / 39

Page 54: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

To use:

import folium

Create a map:

myMap = folium.Map()

Make markers:

newMark = folium.Marker([lat,lon],popup=name)

Add to the map:

newMark.add to(myMap)

Many options to customize background map (“tiles”)and markers.

CSci 127 (Hunter) Lecture 9 14 April 2020 27 / 39

Page 55: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Folium

To use:

import folium

Create a map:

myMap = folium.Map()

Make markers:

newMark = folium.Marker([lat,lon],popup=name)

Add to the map:

newMark.add to(myMap)

Many options to customize background map (“tiles”)and markers.

CSci 127 (Hunter) Lecture 9 14 April 2020 27 / 39

Page 56: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Demo

(Python program using Folium.)

CSci 127 (Hunter) Lecture 9 14 April 2020 28 / 39

Page 57: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs of Triples

Predict which each line of code does:

(example from Folium documentation)

CSci 127 (Hunter) Lecture 9 14 April 2020 29 / 39

Page 58: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Today’s Topics

Recap: Functions & Top Down Design

Mapping GIS Data

Random Numbers

Indefinite Loops

CSci 127 (Hunter) Lecture 9 14 April 2020 30 / 39

Page 59: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python’s random package

Python has a built-in package for generatingpseudo-random numbers.

To use:

import random

Useful command to generate whole numbers:

random.randrange(start,stop,step)

which gives a number chosen randomly fromthe specified range.

Useful command to generate real numbers:

random.random()

which gives a number chosen (uniformly) atrandom from [0.0,1.0).

Very useful for simulations, games, andtesting.

CSci 127 (Hunter) Lecture 9 14 April 2020 31 / 39

Page 60: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python’s random package

Python has a built-in package for generatingpseudo-random numbers.

To use:

import random

Useful command to generate whole numbers:

random.randrange(start,stop,step)

which gives a number chosen randomly fromthe specified range.

Useful command to generate real numbers:

random.random()

which gives a number chosen (uniformly) atrandom from [0.0,1.0).

Very useful for simulations, games, andtesting.

CSci 127 (Hunter) Lecture 9 14 April 2020 31 / 39

Page 61: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python’s random package

Python has a built-in package for generatingpseudo-random numbers.

To use:

import random

Useful command to generate whole numbers:

random.randrange(start,stop,step)

which gives a number chosen randomly fromthe specified range.

Useful command to generate real numbers:

random.random()

which gives a number chosen (uniformly) atrandom from [0.0,1.0).

Very useful for simulations, games, andtesting.

CSci 127 (Hunter) Lecture 9 14 April 2020 31 / 39

Page 62: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python’s random package

Python has a built-in package for generatingpseudo-random numbers.

To use:

import random

Useful command to generate whole numbers:

random.randrange(start,stop,step)

which gives a number chosen randomly fromthe specified range.

Useful command to generate real numbers:

random.random()

which gives a number chosen (uniformly) atrandom from [0.0,1.0).

Very useful for simulations, games, andtesting.

CSci 127 (Hunter) Lecture 9 14 April 2020 31 / 39

Page 63: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python’s random package

Python has a built-in package for generatingpseudo-random numbers.

To use:

import random

Useful command to generate whole numbers:

random.randrange(start,stop,step)

which gives a number chosen randomly fromthe specified range.

Useful command to generate real numbers:

random.random()

which gives a number chosen (uniformly) atrandom from [0.0,1.0).

Very useful for simulations, games, andtesting.

CSci 127 (Hunter) Lecture 9 14 April 2020 31 / 39

Page 64: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python’s random package

Python has a built-in package for generatingpseudo-random numbers.

To use:

import random

Useful command to generate whole numbers:

random.randrange(start,stop,step)

which gives a number chosen randomly fromthe specified range.

Useful command to generate real numbers:

random.random()

which gives a number chosen (uniformly) atrandom from [0.0,1.0).

Very useful for simulations, games, andtesting.

CSci 127 (Hunter) Lecture 9 14 April 2020 31 / 39

Page 65: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Trinket

(Demo turtlerandom walk)

CSci 127 (Hunter) Lecture 9 14 April 2020 32 / 39

Page 66: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Today’s Topics

Recap: Functions & Top Down Design

Mapping GIS Data

Random Numbers

Indefinite Loops

CS Survey

CSci 127 (Hunter) Lecture 9 14 April 2020 33 / 39

Page 67: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

In Pairs or Triples:

Predict what the code will do:

CSci 127 (Hunter) Lecture 9 14 April 2020 34 / 39

Page 68: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 9 14 April 2020 35 / 39

Page 69: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Indefinite Loops

Indefinite loops repeat as long as thecondition is true.

Could execute the body of the loopzero times, 10 times, infinite numberof times.

The condition determines how manytimes.

Very useful for checking input,simulations, and games.

More details next lecture...

CSci 127 (Hunter) Lecture 9 14 April 2020 36 / 39

Page 70: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Indefinite Loops

Indefinite loops repeat as long as thecondition is true.

Could execute the body of the loopzero times, 10 times, infinite numberof times.

The condition determines how manytimes.

Very useful for checking input,simulations, and games.

More details next lecture...

CSci 127 (Hunter) Lecture 9 14 April 2020 36 / 39

Page 71: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Indefinite Loops

Indefinite loops repeat as long as thecondition is true.

Could execute the body of the loopzero times, 10 times, infinite numberof times.

The condition determines how manytimes.

Very useful for checking input,simulations, and games.

More details next lecture...

CSci 127 (Hunter) Lecture 9 14 April 2020 36 / 39

Page 72: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Indefinite Loops

Indefinite loops repeat as long as thecondition is true.

Could execute the body of the loopzero times, 10 times, infinite numberof times.

The condition determines how manytimes.

Very useful for checking input,simulations, and games.

More details next lecture...

CSci 127 (Hunter) Lecture 9 14 April 2020 36 / 39

Page 73: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Indefinite Loops

Indefinite loops repeat as long as thecondition is true.

Could execute the body of the loopzero times, 10 times, infinite numberof times.

The condition determines how manytimes.

Very useful for checking input,simulations, and games.

More details next lecture...

CSci 127 (Hunter) Lecture 9 14 April 2020 36 / 39

Page 74: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Recap

Top-down design: breaking into subproblems, andimplementing each part separately.

Excellent approach: can then test each partseparately before adding it to a large program.

When possible, design so that your code is flexibleto be reused (“code reuse”).

Introduced a Python library, Folium for creatinginteractive HTML maps.

Introduced while loops for repeating commandsfor an indefinite number of times.

CSci 127 (Hunter) Lecture 9 14 April 2020 37 / 39

Page 75: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Recap

Top-down design: breaking into subproblems, andimplementing each part separately.

Excellent approach: can then test each partseparately before adding it to a large program.

When possible, design so that your code is flexibleto be reused (“code reuse”).

Introduced a Python library, Folium for creatinginteractive HTML maps.

Introduced while loops for repeating commandsfor an indefinite number of times.

CSci 127 (Hunter) Lecture 9 14 April 2020 37 / 39

Page 76: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Recap

Top-down design: breaking into subproblems, andimplementing each part separately.

Excellent approach: can then test each partseparately before adding it to a large program.

When possible, design so that your code is flexibleto be reused (“code reuse”).

Introduced a Python library, Folium for creatinginteractive HTML maps.

Introduced while loops for repeating commandsfor an indefinite number of times.

CSci 127 (Hunter) Lecture 9 14 April 2020 37 / 39

Page 77: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Recap

Top-down design: breaking into subproblems, andimplementing each part separately.

Excellent approach: can then test each partseparately before adding it to a large program.

When possible, design so that your code is flexibleto be reused (“code reuse”).

Introduced a Python library, Folium for creatinginteractive HTML maps.

Introduced while loops for repeating commandsfor an indefinite number of times.

CSci 127 (Hunter) Lecture 9 14 April 2020 37 / 39

Page 78: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Recap

Top-down design: breaking into subproblems, andimplementing each part separately.

Excellent approach: can then test each partseparately before adding it to a large program.

When possible, design so that your code is flexibleto be reused (“code reuse”).

Introduced a Python library, Folium for creatinginteractive HTML maps.

Introduced while loops for repeating commandsfor an indefinite number of times.

CSci 127 (Hunter) Lecture 9 14 April 2020 37 / 39

Page 79: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Recap

Top-down design: breaking into subproblems, andimplementing each part separately.

Excellent approach: can then test each partseparately before adding it to a large program.

When possible, design so that your code is flexibleto be reused (“code reuse”).

Introduced a Python library, Folium for creatinginteractive HTML maps.

Introduced while loops for repeating commandsfor an indefinite number of times.

CSci 127 (Hunter) Lecture 9 14 April 2020 37 / 39

Page 80: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Practice Quiz & Final Questions

Lightning rounds:

I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions & Top-Down Design (Summer 18, #7 & #5).

CSci 127 (Hunter) Lecture 9 14 April 2020 38 / 39

Page 81: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Practice Quiz & Final Questions

Lightning rounds:I write as much you can for 60 seconds;

I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions & Top-Down Design (Summer 18, #7 & #5).

CSci 127 (Hunter) Lecture 9 14 April 2020 38 / 39

Page 82: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Practice Quiz & Final Questions

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; and

I repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions & Top-Down Design (Summer 18, #7 & #5).

CSci 127 (Hunter) Lecture 9 14 April 2020 38 / 39

Page 83: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Practice Quiz & Final Questions

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions & Top-Down Design (Summer 18, #7 & #5).

CSci 127 (Hunter) Lecture 9 14 April 2020 38 / 39

Page 84: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Practice Quiz & Final Questions

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions & Top-Down Design (Summer 18, #7 & #5).

CSci 127 (Hunter) Lecture 9 14 April 2020 38 / 39

Page 85: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Practice Quiz & Final Questions

Lightning rounds:I write as much you can for 60 seconds;I followed by answer; andI repeat.

Past exams are on the webpage (under Final Exam Information).

Theme: Functions & Top-Down Design (Summer 18, #7 & #5).

CSci 127 (Hunter) Lecture 9 14 April 2020 38 / 39

Page 86: CSci 127: Introduction to Computer ScienceCSci 127 (Hunter) Lecture 9 14 April 202019/39 Third Part: Fill in Missing Pieces 1 Write import statements. 2 Write down new function names

Educational Psychology Study

If you have consented to participate in theEducational Psychology study,please fill in the 3-question survey

Thank you for your participation!!!

CSci 127 (Hunter) Lecture 9 14 April 2020 39 / 39