Compsci 101 I is for … Turtle, Bagels, Loop Tracing, Files Part 1 of 4 • Identity • Who are you? Computer Science Student Susan Rodger • Invariant September 15, 2020 • Reasoning formally and informally about loops • Internet • Network of networks • Far more than that! 9/15/2020 Compsci 101, Fall 2020 1 9/15/2020 Compsci 101, Fall 2020 2 PFTD Run Turtle, Run • Turtle • Bagels APT • Trace through loops • Files 9/15/2020 Compsci 101, Fall 2020 3 9/15/2020 Compsci 101, Fall 2020 4
Turtle Programming Put yourself in the turtle t… • Must: t.forward(50) # turtle moves forward • Import turtle module # drawing a line • Create window/Screen • Last thing - exit on click t.left(90) # turtle turns to its left • Create turtles to use, name/type/value t.pencolor(“blue”) # change pen color • Review Turtle commands and concepts t.forward(100) # turtle moves forward • http://bit.ly/turtle_tutorial for more, and book # drawing line, new color • See Snowpeople.py, ColorMyWorld.py, and Spiro.py for some ideas • Color, Position, Leaving Turtle where started • Many more commands than this 9/15/2020 Compsci 101, Fall 2020 5 9/15/2020 Compsci 101, Fall 2020 6 Example: Example: Simple.py parts simple.py • Import at the top • Create canvas • Create turtle • Call function to draw • Close canvas when click on it 9/15/2020 Compsci 101, Fall 2020 7 9/15/2020 Compsci 101, Fall 2020 8
Example: Simple.py parts Example: Simple.py parts • Import at the top • Import at the top • Create window • Create canvas • Create turtle • Create turtle • Call function to draw • Call function to draw • Close canvas when • Close canvas when click on it click on it 9/15/2020 Compsci 101, Fall 2020 9 9/15/2020 Compsci 101, Fall 2020 10 Example: Simple.py parts Example: Simple.py parts • Import at the top • Import at the top • Create canvas • Create canvas • Create turtle • Create turtle • Call function to draw • Call function to draw • Close canvas when • Close canvas when click on it click on it 9/15/2020 Compsci 101, Fall 2020 11 9/15/2020 Compsci 101, Fall 2020 12
Example: Simple.py DrawPicture Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 13 9/15/2020 Compsci 101, Fall 2020 14 Example: Simple.py DrawPicture Example: Simple.py DrawPicture 9/15/2020 Compsci 101, Fall 2020 15 9/15/2020 Compsci 101, Fall 2020 16
Compsci 101 Example: Simple.py DrawPicture Turtle, Bagels, Loop Tracing, Files Part 2 of 4 Susan Rodger September 15, 2020 9/15/2020 9/15/2020 Compsci 101, Fall 2020 17 9/15/2020 Compsci 101, Fall 2020 18 Useful turtle functions What are key concepts in Spiro.py? Import turtle • forward(n)/backward(n) – move turtle n pixels • left(n)/right(n) – turn turtle n degrees • pendown()/pendup() – whether actually drawing 1 – slowest • setposition(x, y) – puts turtle in this (x,y) 10 – fastest 0 – No animation coordinate (a.k.a. goto , setpos ) • sethead(n) – points turtle in this direction (n=0 is Create screen/window east) • Many more in documentation! Create turtle • https://docs.python.org/3/library/turtle.html Close on click 9/15/2020 Compsci 101, Fall 2020 19 9/15/2020 Compsci 101, Fall 2020 20
ColorMyWorld.py Turtle Concepts • Create a screen so you can .. • Exit On Click • Some other Screen Functions • Create a turtle so you can … • Move and draw using the turtle • Drawing Concepts • Pen [up and down] • Fill • Color • Position 9/15/2020 Compsci 101, Fall 2020 21 9/15/2020 Compsci 101, Fall 2020 22 Compsci 101 Code-Tracing a Loop Turtle, Bagels, Loop Tracing, Files Part 3 of 4 1. Find the changing variables/expressions 2. Create table, columns are variables/expressions Susan Rodger 1. First column is loop variable September 15, 2020 2. Add columns to help track everything else 3. Each row is an iteration of the 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 23 9/15/2020 Compsci 101, Fall 2020 24
Code-Tracing a Loop Code-Tracing a Loop 1. Find the changing variables/expressions 1. Find the changing variables 2. Create table, columns are variables/expressions 2. Create table, columns are the variables 1. First column is loop variable 1. First column is loop variable 2. Add columns to help track everything else 2. Add columns to help track everything else Other variable What should be the table’s Useful expression columns? to track Loop variable 9/15/2020 Compsci 101, Fall 2020 25 9/15/2020 Compsci 101, Fall 2020 26 Fill in table Fill in table 1. Before execute code 1. Before execute code block, copy down block, copy down each variable’s value each variable’s value 2. Execute code block, 2. Execute code block, update a value in the update a value in the row as it changes row as it changes mystery([2, 12, 4, 15, 15]) mystery([2, 12, 4, 15, 15]) #1 i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] i idxMax lst[idxMax] lst[i] lst[idxMax] < lst[i] 0 0 2 2 False 1 0 9/15/2020 Compsci 101, Fall 2020 27 9/15/2020 Compsci 101, Fall 2020 28
Compsci 101 Fill in table Turtle, Bagels, Loop Tracing, Files Part 4 of 4 1. Before execute code block, copy down Susan Rodger each variable’s value September 15, 2020 2. Execute code block, update a value in the row as it changes 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 9/15/2020 Compsci 101, Fall 2020 30 Examples of Processing Data Studying Language Evolution • Lecture 1: count letters in Bible • Ngram informs how words evolve • From dove to dived • Another example: Google Ngram viewer • https://www.youtube.com/watch?v=tFW7orQsBuo • https://books.google.com/ngrams 9/15/2020 Compsci 101, Fall 2020 31 9/15/2020 Compsci 101, Fall 2020 32
Sequences, Repetition Sequences, Repetition • Parameters? What are they to this query? • Parameters? What are they to this query? Search • https://books.google.com/ngrams/graph?conte • https://books.google.com/ngrams/graph?conte words nt=terrorism%2Cpatriot&year_start=1800&year nt=terrorism%2Cpatriot&year_start=1800&year _end=2000&corpus=15&smoothing=3 _end=2000&corpus=15&smoothing=3 Year start search Year end What can the search URL tell you? 9/15/2020 Compsci 101, Fall 2020 33 9/15/2020 Compsci 101, Fall 2020 34 Processing Data File Pattern: One line at a time • Simplest and reasonably efficient Python pattern • How do we find the longest word in .. Any text? • Open, loop, close, return/process • How do we find the word that occurs the most? • LineCounter.py in code zip file • How is this related to how Google Search works? • File as sequence • Text files can be viewed as sequences s • One line at-a-time • Sequences of lines • Each line is a string • Asymmetry in • Some clean-up because of ‘\n’ Open vs Close steps 9/15/2020 Compsci 101, Fall 2020 35 9/15/2020 Compsci 101, Fall 2020 36
altCount.py File Objects • a • a • A file is an object, like a string • Functions applied to object: len(“word”) • To get file object use open(“data.txt”) • What is returned? Integer value, file object • Often methods (aka function) applied to object • f.readlines(), f.read(), f.close() • Just like: st.lower(), st.count(“e”) 9/15/2020 Compsci 101, Fall 2020 37 9/15/2020 Compsci 101, Fall 2020 38 FileStuff.py: avgWord Text File Processing Pattern • See module FileStuff.py in code zip file • a • a • If newline ’\n’ is read, call .strip() • If want to break line into “words”, call .split() • Process the list returned by .split() • May need to convert strings to int or float or … • The for line in f: pattern is efficient • Contrast list returned by f.readlines() 9/15/2020 Compsci 101, Fall 2020 39 9/15/2020 Compsci 101, Fall 2020 40
Recommend
More recommend