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 • Invariant • Reasoning formally and informally about loops • Internet • Network of networks • Far more than that! 9/15/2020 Compsci 101, Fall 2020 2
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
Run Turtle, Run 9/15/2020 Compsci 101, Fall 2020 4
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
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
Example: simple.py 9/15/2020 Compsci 101, Fall 2020 7
Example: Simple.py parts • Im Impor ort a at t the t top op • 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 on n it it 9/15/2020 Compsci 101, Fall 2020 8
Example: Simple.py parts • Im Impor ort a at t the t top op • 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 on n it it 9/15/2020 Compsci 101, Fall 2020 9
Example: Simple.py parts • Im Impor ort a at t the t top op • 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 on n it it 9/15/2020 Compsci 101, Fall 2020 10
Example: Simple.py parts • Im Impor ort a at t the t top op • 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 on n it it 9/15/2020 Compsci 101, Fall 2020 11
Example: Simple.py parts • Im Impor ort a at t the t top op • 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 on n it it 9/15/2020 Compsci 101, Fall 2020 12
Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 13
Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 14
Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 15
Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 16
Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 17
Compsci 101 101 Turtle, B , Bag agel els, L , Loop T op Tracing, Files es Par art 2 2 of of 4 Susan Rodger September 15, 2020 9/15/2020 Compsci 101, Fall 2020 18
What are key concepts in Spiro.py? Import turtle 1 – slowest 10 – fastest 0 – No animation Create screen/window Create turtle Close on click 9/15/2020 Compsci 101, Fall 2020 19
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
ColorMyWorld.py 9/15/2020 Compsci 101, Fall 2020 21
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
Compsci 101 101 Turtle, B , Bag agel els, L , Loop T op Tracing, Files es Par art 3 3 of of 4 Susan Rodger September 15, 2020 9/15/2020 Compsci 101, Fall 2020 23
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 of 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
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 What should be the table’s columns? 9/15/2020 Compsci 101, Fall 2020 25
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 Other variable Useful expression to track Loop variable 9/15/2020 Compsci 101, Fall 2020 26
Fill in table 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. Execut 2. 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 mystery([2, 12, 4, 15, 15]) i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] 9/15/2020 Compsci 101, Fall 2020 27
Fill in table 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. Execut 2. 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 mystery([2, 12, 4, 15, 15]) #1 i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] 0 0 2 2 False 1 0 9/15/2020 Compsci 101, Fall 2020 28
Fill in table 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 mystery([2, 12, 4, 15, 15]) #2 i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] 0 0 2 2 False 1 0 1 2 12 True 9/15/2020 Compsci 101, Fall 2020 29
Compsci 101 101 Turtle, B , Bag agel els, L , Loop T op Tracing, Files es Par art 4 4 of of 4 Susan Rodger September 15, 2020 9/15/2020 Compsci 101, Fall 2020 30
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
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
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 What can the URL tell you? 9/15/2020 Compsci 101, Fall 2020 33
Sequences, Repetition • Par arameters? W Wha hat ar are t the hey t to this his quer uery? Search • https://books.google.com/ngrams/graph?conte words nt=terrorism%2Cpatriot&year_start=1800&year _end=2000&corpus=15&smoothing=3 Year start search Year end search 9/15/2020 Compsci 101, Fall 2020 34
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 occurs 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
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
altCount.py • a 9/15/2020 Compsci 101, Fall 2020 37
Recommend
More recommend