layout: true class: middle --- # Girl Develop It - Rochester --- # Agenda 1. Introductions 1. Install Python 1. Write your first Python! 1. How to find help 1. Building on other code 1. Draw something, your choice 1. Control structures (if this then that) 1. More finding help .footnote[Stop me any time for questions] --- layout: false .left-column[ ## About me ] .right-column[ I write Python and bake artisan bread. ] --- layout: false .left-column[ ## About me ## About you ] .right-column[ .fade[I write Python and bake artisan bread.] You want to learn Python, your name is _____ and: 1. Something you want to learn (other than Python) 1. Why you want to learn Python 1. One of your hobbies 1. Worst ice-breaker question/game you've ever played ] --- layout: false .left-column[ ## About me ## About you ## About this class ] .right-column[ .fade[I write Python and bake artisan bread. You want to learn Python, your name is _____ and: 1. Something you want to learn (other than Python) 1. Why you want to learn Python 1. One of your hobbies 1. Worst ice-breaker question/game you've ever played] We're going to start with drawing things as a way to teach you programming concepts. ] --- # Rules of the Road .large[ 1. Every question is important 1. Help each other 1. Ask for help 1. .red[Have fun!] ] --- # Learning to program 1. Lots of very helpful people from the internet (StackOverflow, mailing lists, IRC) 1. Patience, patience, patience 1. Pick it back up again the next day --- class: center, middle # Programming "The *craft* of programming begins with empathy, not formatting or languages or tools or algorithms or data structures" - Kent Beck --- class: center, middle # Installing Python .large[http://continuum.io/downloads] 1. Download Python for your operating system 1. .red[Make sure to download version 3.4] 1. Click on the installer after it's downloaded to install --- class: center, middle # Day 1: Drawing with Turtles Let's open IDLE --- class: center, middle # The shell is an interpreter .large[Input -> Computer -> Output] You say something, the computer translates your idea and responds --- class: middle # Is this thing on? ``` >>> print("Hi there!") >>> len("Python!") >>> max(5,6) >>> 5+6 >>> 5/2 >>> round(4.2) >>> newProgrammer = "you" >>> print(newProgrammer) ``` --- class: middle # Yes! .no-margins[ `>>> print("Hi there!")` .red[Hi there!] `>>> len("Python!")` .red[7] `>>> max(5,6)` .red[ 6 ] `>>> 5+6` .red[11] `>>> 5/2` .red[2.5] `>>> round(4.2)` .red[4] `>>> newProgrammer = "you"` `>>> print(newProgrammer)` .red[you] ] --- class: middle # Put your work somewhere .large[File -> New File] ``` import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(100) alex.left(90) alex.forward(100) wn.exitonclick() ``` --- class: middle center # Save as .large[Your file *cannot* be named turtle.py] --- class: middle center # Go baby go .large[Run -> Run module] Or F5, if you like less clicking --- class: middle # Bam, first script done! --- class: middle # Interpreter v. Script Interpreter is great for testing things out, but scripts can be rerun, tweaked, and run again. --- class: middle ``` import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(100) alex.left(90) alex.forward(100) wn.exitonclick() ``` --- class: middle # Code reuse ``` import turtle if __name__ == "__main__": wn = turtle.Screen() alex = turtle.Turtle() alex.forward(100) alex.left(90) alex.forward(100) wn.exitonclick() ``` --- class: middle # Play time - 10 minutes 1. Ask questions 1. Add a movement 1. Change some numbers 1. Print your name 1. Help your neighbor --- class: middle center .large[ Draw a square Draw a pentagon ] --- class: middle # One way to draw a pentagon ``` alex = turtle.Turtle() alex.forward(150) alex.left(72) alex.forward(150) alex.left(72) alex.forward(150) alex.left(72) alex.forward(150) alex.left(72) alex.forward(150) alex.left(72) ``` --- class: middle center # Loops go around and around --- class: middle # Print three "programs" ``` for num in [1, 2, 3]: print("program", num) ``` --- class: middle # It's hip to be square ``` alex = turtle.Turtle() for side in [1, 2, 3, 4]: alex.forward(100) alex.left(90) ``` --- class: middle center # Now draw a pentagon in a loop --- class: middle # Looping a pentagon ``` alex = turtle.Turtle() for side in [1, 2, 3, 4, 5]: alex.forward(100) alex.left(72) ``` --- class: middle # Shortcut: range ``` alex = turtle.Turtle() for side in range(5): alex.forward(100) alex.left(72) ``` `range(5)` and `[0, 1, 2, 3, 4]` are identical --- class: middle # Add a side! Now let's try to draw a hexagon --- class: middle # Math class reminder (5 + 3) * 2 5 + 3 * 2 --- class: middle # Draw your hexagon How much did you have to change? --- class: middle # Can we do better? Every shape's angles add up to 360 degrees We only have to write the angle once, and the number of sides --- class: middle ``` for side in range(number_of_sides): alex.forward(100) alex.left(angle) ``` --- class: middle ``` number_of_sides =
angle =
for side in range(number_of_sides): alex.forward(100) alex.left(angle) ``` --- class: middle ``` number_of_sides = 6 angle = 360 / 6 for side in range(number_of_sides): alex.forward(100) alex.left(angle) ``` --- class: middle ``` number_of_sides = 6 angle = 360 / number_of_sides for side in range(number_of_sides): alex.forward(100) alex.left(angle) ``` --- class: middle # Now an octagon ``` number_of_sides = 8 angle = 360 / number_of_sides for side in range(number_of_sides): alex.forward(100) alex.left(angle) ``` We just needed to change the *value* of the variable --- class: middle center # Activity time Let's learn to do more with turtle. .large[Google "python turtle 3.4"] --- class: middle center # 15 minutes .large[Teach yourself as many new features as you want] When your neighbor pauses, share your new function with them --- class: middle # Try these `alex.shape("turtle")` `alex.down()` `alex.up()` `alex.backward(someNumber)` `alex.color("red")` `alex.circle(someNumber)` `alex.stamp()` --- class: middle # What happens here ``` for color_now in ["red", "blue", "yellow", "purple", "green"]: alex.color(color_now) alex.forward(100) alex.left(72) ``` --- class: middle # Challenges Draw a star Draw a spiral Draw two squares --- class: middle # True or False Just like an exam question. You ask "Is a dog a chicken?" and Python answers ``` >>> "dog" == "chicken" False ``` ``` >>> "dog" == "dog" True ``` --- class: middle # Decisions in Code `True` and `False` can be used to make decisions ``` if animal == "dog": print("Woof") else: print("Some other sound") ``` --- class: middle ``` if animal == "dog": print("Woof") elif animal == "cat": print("Meow") elif animal == "cow": print("Moo") else: print("Some other sound") ``` --- class: middle ``` for num in range(5): if num % 2: alex.color("green") else: alex.color("red") alex.forward(100) alex.left(72) ``` --- class: middle # Recyling your code You may notice, a lot of the code we've written has similarities - Changing values - Repeating things - Sharing code with others --- class: middle center # Share functionality by defining functions --- class: middle # Simple function ``` def drawSquare(): for num in range(4): alex.forward(100) alex.left(90) drawSquare() alex.up() alex.forward(200) alex.down() drawSquare() ``` --- class: middle # More options ``` def drawSquare(side): for num in range(4): alex.forward(side) alex.left(90) drawSquare(100) drawSquare(200) drawSquare(300) ``` --- class: middle # Questions? .footnote[These slides are available at [rsb.io/talks/gdi/python-1](http://rsb.io/talks/gdi/python-1/)]