Created
August 24, 2020 16:08
-
-
Save shinysu/d5ecbd46e5ea70e3702da474dbead64b to your computer and use it in GitHub Desktop.
Concepts used - Statements, loops, variables, functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
draw a line using turtle | |
''' | |
import turtle | |
t = turtle.Turtle() | |
t.forward(100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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