Skip to content

Instantly share code, notes, and snippets.

@shinysu
Last active July 21, 2021 04:38
Show Gist options
  • Save shinysu/f383f1c0880d5d9eeee2e906188dfbaf to your computer and use it in GitHub Desktop.
Save shinysu/f383f1c0880d5d9eeee2e906188dfbaf to your computer and use it in GitHub Desktop.
Day 1
'''
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")
for i in range(20):
draw_shape(t, 4, 100)
t.left(18)
@Jayasuriya007
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment