Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created August 24, 2020 16:08
Show Gist options
  • Save shinysu/d5ecbd46e5ea70e3702da474dbead64b to your computer and use it in GitHub Desktop.
Save shinysu/d5ecbd46e5ea70e3702da474dbead64b to your computer and use it in GitHub Desktop.
Concepts used - Statements, loops, variables, functions
'''
draw a line using turtle
'''
import turtle
t = turtle.Turtle()
t.forward(100)
'''
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)
'''
draw a square using loop
The statements within the for loop executes 4 times for i values, 0, 1, 2,3
key concept - using loops for repeated statements
'''
import turtle
t = turtle.Turtle()
for i in range(4):
t.forward(100)
t.left(90)
'''
draw a square - replace all magic numbers with variables
key concept - using variables for storing values
'''
import turtle
t = turtle.Turtle()
sides = 4
length = 100
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
'''
draw a polygon - using a function
key concept - using functions for block of code that performs an action
syntax: def function_name():
'''
import turtle
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
t = turtle.Turtle()
draw_shape(t, 4, 100)
'''
draw a pattern
'''
import turtle
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
t=turtle.Turtle()
t.color("red")
t.begin_fill()
for i in range(20):
draw_shape(t, 4, 100)
t.left(18)
t.end_fill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment