Skip to content

Instantly share code, notes, and snippets.

@sahilsinha
Last active December 2, 2015 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahilsinha/5464a38aed7f4adafde6 to your computer and use it in GitHub Desktop.
Save sahilsinha/5464a38aed7f4adafde6 to your computer and use it in GitHub Desktop.

Intro

Programming is my favorite thing

Who has programmed before?

Computers do exactly what you tell them to do if you can program you can do almost anything

Syntax matters - computers are very particular about how they understand things

Sequence

Who has brushed their teeth before?

Sequence of steps

Decision

Are my teeth clean or has 2 minutes passed If I have two toothpastes pick my favorite.

we decide when to do things

Repetition

Do it two times a day Move it back and forth a number of times

Modules

Using other people’s programs https://docs.python.org/2/library/turtle.html

Objects

Instantiating turtle

t = turtle.Turtle()

t is a variable and we assign a value to it

Turtle Methods(Arguments)

right(degrees)
left(degrees)
forward(distance)
backward(distance)
home()
circle(radius)

Turtle attributes

t.color(‘blue’)

Draw Half Square together

Turtle starts facing east You’re the turtle

import turtle

t = turtle.Turtle()

t.forward(100)
t.left(90)
t.color("blue")
t.forward(100)
t.left(90)

Draw a square

import turtle

t = turtle.Turtle()

t.forward(100)
t.left(90)

t.forward(100)
t.left(90)

t.forward(100)
t.left(90)

t.forward(100)
t.left(90)

For loops

repeating count from zero

print("start test")

for i in range(4):
    print(i)
    print("test")
    
print("end test 1")

for banana in range(20):
    print(banana)
    print(banana * 2)

Draw a square with for loops

import turtle

t = turtle.Turtle()

for i in range(4):
	t.forward(100)
	t.left(90)

Draw triangle square hexagon and octagon star, spirals

Triangle

import turtle

t = turtle.Turtle()

for i in range(3):
	t.forward(100)
	t.left(120)

Square

import turtle

t = turtle.Turtle()

for i in range(4):
    t.forward(75)
    t.left(90)

Hexagon

import turtle

t = turtle.Turtle()

for i in range(6):
    t.forward(75)
    t.left(60)

Octagon

import turtle

t = turtle.Turtle()

for i in range(8):
    t.forward(75)
    t.left(45)

Star

import turtle 

star = turtle.Turtle()

for i in range(50):
    star.forward(50)
    star.right(144)
#END_SRC    

What's the minimum number of loops?

import turtle 

spiral = turtle.Turtle()

for i in range(20):
    spiral.forward(i * 10)
    spiral.right(144)

Spirals

import turtle 

painter = turtle.Turtle()

painter.color("blue")

for i in range(50):
    painter.forward(50)
    painter.left(123) # Let's go counterclockwise this time 
    
painter.color("red")
for i in range(50):
    painter.forward(100)
    painter.left(123)

Challenge: Draw any polygon

Take X for sides and draw any shape! (1-1000)

  • use variables to store our values
  • 180(n-2)/n gives you the interior angle of a regular polygon - where n is the number of sides
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment