Compsci 101 101 Turtle, B , Bag agel els, L , Loop T op - - PowerPoint PPT Presentation

compsci 101 101 turtle b bag agel els l loop t op tracing
SMART_READER_LITE
LIVE PREVIEW

Compsci 101 101 Turtle, B , Bag agel els, L , Loop T op - - PowerPoint PPT Presentation

Compsci 101 101 Turtle, B , Bag agel els, L , Loop T op Tracing, Files es Par art 1 1 of of 4 Susan Rodger September 15, 2020 9/15/2020 Compsci 101, Fall 2020 1 I is for Identity Who are you? Computer Science Student


slide-1
SLIDE 1

Compsci 101 101 Turtle, B , Bag agel els, L , Loop T

  • p Tracing, Files

es Par art 1 1 of

  • f 4

9/15/2020 Compsci 101, Fall 2020 1

Susan Rodger September 15, 2020

slide-2
SLIDE 2

I is for …

  • Identity
  • Who are you? Computer Science Student
  • Invariant
  • Reasoning formally and informally about loops
  • Internet
  • Network of networks
  • Far more than that!

9/15/2020 Compsci 101, Fall 2020 2

slide-3
SLIDE 3

PFTD

  • Turtle

le

  • Bag

agels ls A APT

  • Trace t

ace throug ugh h loops

  • File

iles

9/15/2020 Compsci 101, Fall 2020 3

slide-4
SLIDE 4

Run Turtle, Run

9/15/2020 Compsci 101, Fall 2020 4

slide-5
SLIDE 5

Turtle Programming

  • Must

st:

  • Import turtle module
  • Create window/Screen
  • Last thing - exit on click
  • Create turtles to use, name/type/value
  • Revi

view T Turtle c commands a and concepts

  • http://bit.ly/turtle_tutorial for more, and book
  • Se

See Snowpeo wpeopl ple.py, , ColorMyWorl rld.p d.py, a and Sp Spiro.py for so some i ideas

  • Color, Position, Leaving Turtle where started
  • Many more commands than this

9/15/2020 Compsci 101, Fall 2020 5

slide-6
SLIDE 6

Put yourself in the turtle t…

t.fo forward rd(50) 50) # # turtle le moves forward # # drawing a line a line t.left(90) # # tu turtl tle tu turns s to to its s left t.penc encolo lor(“blue”) # c # cha hange pen en colo lor t.fo forward rd(10 100) 0) # # turtle le moves es forward # # drawing lin line, ne new c colo lor

9/15/2020 Compsci 101, Fall 2020 6

slide-7
SLIDE 7

Example: simple.py

9/15/2020 Compsci 101, Fall 2020 7

slide-8
SLIDE 8

Example: Simple.py parts

  • Im

Impor

  • rt a

at t the t top

  • p
  • Create c

canv nvas

  • Create t

turtle le

  • Call f

ll funct ctio ion t n to draw

  • Clo

lose c can anvas w when hen cli lick o

  • n

n it it

9/15/2020 Compsci 101, Fall 2020 8

slide-9
SLIDE 9

Example: Simple.py parts

  • Im

Impor

  • rt a

at t the t top

  • p
  • Create w

window

  • Create t

turtle le

  • Call f

ll funct ctio ion t n to draw

  • Clo

lose c can anvas w when hen cli lick o

  • n

n it it

9/15/2020 Compsci 101, Fall 2020 9

slide-10
SLIDE 10

Example: Simple.py parts

  • Im

Impor

  • rt a

at t the t top

  • p
  • Create c

canv nvas

  • Create t

turtle le

  • Call f

ll funct ctio ion t n to draw

  • Clo

lose c can anvas w when hen cli lick o

  • n

n it it

9/15/2020 Compsci 101, Fall 2020 10

slide-11
SLIDE 11

Example: Simple.py parts

  • Im

Impor

  • rt a

at t the t top

  • p
  • Create c

canv nvas

  • Create t

turtle le

  • Call f

ll funct ctio ion t n to draw

  • Clo

lose c can anvas w when hen cli lick o

  • n

n it it

9/15/2020 Compsci 101, Fall 2020 11

slide-12
SLIDE 12

Example: Simple.py parts

  • Im

Impor

  • rt a

at t the t top

  • p
  • Create c

canv nvas

  • Create t

turtle le

  • Call f

ll funct ctio ion t n to draw

  • Clo

lose c can anvas w when hen cli lick o

  • n

n it it

9/15/2020 Compsci 101, Fall 2020 12

slide-13
SLIDE 13

Example: Simple.py DrawPicture

9/15/2020 Compsci 101, Fall 2020 13

slide-14
SLIDE 14

Example: Simple.py DrawPicture

9/15/2020 Compsci 101, Fall 2020 14

slide-15
SLIDE 15

Example: Simple.py DrawPicture

9/15/2020 Compsci 101, Fall 2020 15

slide-16
SLIDE 16

Example: Simple.py DrawPicture

9/15/2020 Compsci 101, Fall 2020 16

slide-17
SLIDE 17

Example: Simple.py DrawPicture

9/15/2020 Compsci 101, Fall 2020 17

slide-18
SLIDE 18

Compsci 101 101 Turtle, B , Bag agel els, L , Loop T

  • p Tracing, Files

es Par art 2 2 of

  • f 4

9/15/2020 Compsci 101, Fall 2020 18

Susan Rodger September 15, 2020

slide-19
SLIDE 19

What are key concepts in Spiro.py?

9/15/2020 Compsci 101, Fall 2020 19

Create screen/window Close on click Create turtle 1 – slowest 10 – fastest 0 – No animation Import turtle

slide-20
SLIDE 20

Useful turtle functions

  • forward(n)/backward(n) – move t

turtle le n n pixels els

  • left(n)/right(n) – turn t

n turtle n le n degree ees

  • pendown()/pendup() – whethe

her a act ctuall ually d drawing ing

  • setposition(x, y) – puts tu

ts turtl tle i in this ( s (x,y ,y) ) coordina inate ( (a.k .k.a .a. . goto, , setpos)

  • sethead(n) – poin

ints t tur urtle in in this his dir irection ( (n= n=0 is 0 is east) t)

  • Many

ny more i in document entatio ion! n!

  • https://docs.python.org/3/library/turtle.html

9/15/2020 Compsci 101, Fall 2020 20

slide-21
SLIDE 21

ColorMyWorld.py

9/15/2020 Compsci 101, Fall 2020 21

slide-22
SLIDE 22

Turtle Concepts

  • Create

e a screen s een so you c u can an ..

  • Exit On Click
  • Some other Screen Functions
  • Create

e a turtle s e so you c u can … n …

  • Move and draw using the turtle
  • Drawing C

ng Concepts

  • Pen [up and down]
  • Fill
  • Color
  • Position

9/15/2020 Compsci 101, Fall 2020 22

slide-23
SLIDE 23

Compsci 101 101 Turtle, B , Bag agel els, L , Loop T

  • p Tracing, Files

es Par art 3 3 of

  • f 4

9/15/2020 Compsci 101, Fall 2020 23

Susan Rodger September 15, 2020

slide-24
SLIDE 24

Code-Tracing a Loop

1.

  • 1. Find the c

chang anging ing v variab iable les/expres essio ions ns 2.

  • 2. Create table

le, c colum lumns ns a are v varia iable les/e /expressio ions ns

  • 1. First column is loop variable
  • 2. Add columns to help track everything else

3.

  • 3. Eac

ach row is is an it an iter eration o

  • f the lo

he loop

  • 1. Before execute code block, copy down each

variable’s value

  • 2. Execute code block, update a value in the row

as it changes

9/15/2020 Compsci 101, Fall 2020 24

slide-25
SLIDE 25

Code-Tracing a Loop

1.

  • 1. Find the c

chang anging ing v variab iable les/expres essio ions ns 2.

  • 2. Create table

le, c colum lumns ns a are v varia iable les/e /expressio ions ns

  • 1. First column is loop variable
  • 2. Add columns to help track everything else

9/15/2020 Compsci 101, Fall 2020 25

What should be the table’s columns?

slide-26
SLIDE 26

Code-Tracing a Loop

1.

  • 1. Find the c

chang anging ing v variab iable les 2.

  • 2. Create table

le, c colum lumns ns a are t the va varia iables les

  • 1. First column is loop variable
  • 2. Add columns to help track everything else

9/15/2020 Compsci 101, Fall 2020 26

Loop variable Other variable Useful expression to track

slide-27
SLIDE 27

Fill in table

9/15/2020 Compsci 101, Fall 2020 27

1.

  • 1. Bef

efore e e execut cute c e code e bl block, c copy do down wn eac each v var ariable le’s v val alue 2.

  • 2. Execut

ecute c e code b e block ck, , up update a a val alue in in the he row as as it c chang anges

i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i]

mystery([2, 12, 4, 15, 15])

slide-28
SLIDE 28

Fill in table

9/15/2020 Compsci 101, Fall 2020 28

1.

  • 1. Bef

efore e e execut cute c e code e bl block, c copy do down wn eac each v var ariable le’s v val alue 2.

  • 2. Execut

ecute c e code b e block ck, , up update a a val alue in in the he row as as it c chang anges

i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] 2 2 False 1

mystery([2, 12, 4, 15, 15]) #1

slide-29
SLIDE 29

Fill in table

9/15/2020 Compsci 101, Fall 2020 29

1.

  • 1. Bef

efore e e execut cute c e code e bl block, c copy do down wn eac each v var ariable le’s v val alue 2.

  • 2. Execut

ecute c e code b e block ck, , up update a a val alue in in the he row as as it c chang anges

i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] 2 2 False 1 0 1 2 12 True

mystery([2, 12, 4, 15, 15]) #2

slide-30
SLIDE 30

Compsci 101 101 Turtle, B , Bag agel els, L , Loop T

  • p Tracing, Files

es Par art 4 4 of

  • f 4

9/15/2020 Compsci 101, Fall 2020 30

Susan Rodger September 15, 2020

slide-31
SLIDE 31

Examples of Processing Data

  • Lec

ecture 1: 1: coun unt le letters in in Bib ible le

  • Anothe

her e example le: G Google le Ngra gram viewe wer

  • https://books.google.com/ngrams

9/15/2020 Compsci 101, Fall 2020 31

slide-32
SLIDE 32

Studying Language Evolution

  • Ngra

gram info forms rms h how word rds e evolve

  • From dove t

to dived

  • https://www.youtube.com/watch?v=tFW7orQsBuo

9/15/2020 Compsci 101, Fall 2020 32

slide-33
SLIDE 33

Sequences, Repetition

  • Par

arameters? W Wha hat ar are t the hey t to this his quer uery?

  • https://books.google.com/ngrams/graph?conte

nt=terrorism%2Cpatriot&year_start=1800&year _end=2000&corpus=15&smoothing=3

9/15/2020 Compsci 101, Fall 2020 33

What can the URL tell you?

slide-34
SLIDE 34

Sequences, Repetition

  • Par

arameters? W Wha hat ar are t the hey t to this his quer uery?

  • https://books.google.com/ngrams/graph?conte

nt=terrorism%2Cpatriot&year_start=1800&year _end=2000&corpus=15&smoothing=3

9/15/2020 Compsci 101, Fall 2020 34

Search words Year start search Year end search

slide-35
SLIDE 35

Processing Data

  • How do we f

e find ind the he lo long ngest word in .. in .. Any ny t tex ext?

  • How do we

e find ind t the he w word t tha hat o

  • ccurs t

the he m most?

  • How is t

this r rela lated ed t to how Google S le Search w h works?

  • Tex

ext f file iles c can an b be e vie iewed as seq equences

  • Sequences of lines
  • Each line is a string
  • Some clean-up because of ‘\n’

9/15/2020 Compsci 101, Fall 2020 35

slide-36
SLIDE 36

File Pattern: One line at a time

  • Simples

lest a and reasona nably ef efficie icient nt Python n pat attern

  • Open, loop, close, return/process
  • LineCounter.py in code zip file
  • File a

e as s seque uenc nce

  • One line at-a-time
  • As

Asymmetry i in Open v n vs Close s e step eps

9/15/2020 Compsci 101, Fall 2020 36

slide-37
SLIDE 37

altCount.py

  • a

9/15/2020 Compsci 101, Fall 2020 37

slide-38
SLIDE 38

File Objects

  • A file

ile is is an an objec ect, lik like a a string

  • Functions applied to object: len(“word”)
  • To get file object use open(“data.txt”)
  • What is returned? Integer value, file object
  • Often

n met etho hods ( (aka f funct ctio ion) n) a applied ied t to object ct

  • f.readlines(), f.read(), f.close()
  • Just like: st.lower(), st.count(“e”)

9/15/2020 Compsci 101, Fall 2020 38

slide-39
SLIDE 39

Text File Processing Pattern

  • See m

modul ule e FileStuff.py in in c code zip ip f file ile

  • If newline ’\n’ is read, call .strip()
  • If want to break line into “words”, call .split()
  • Proces

cess the l e list r ret etur urne ned b by .split()

  • May need to convert strings to int or float or …
  • The

he for line in f: pat atter ern i is ef effici cien ent

  • Contrast list returned by f.readlines()

9/15/2020 Compsci 101, Fall 2020 39

slide-40
SLIDE 40

FileStuff.py: avgWord

  • a

9/15/2020 Compsci 101, Fall 2020 40