Graphic Coding

--Everyday life is like programming. If you love something you can put beauty into it. Donald Knuth--





One way to learn Python is by learning to draw shapes, patterns, and spirals using the turtle module. To get started, you can watch the Trinket Hour of Code module and practice step by step. You can also practice drawing lines by using Python code and the turtle module in the online interactive Python environment called Trinket Click on Run in the Trinket window to see the code working.
3.Adding color with Trinket turtle

You can change the value of your variables either by setting them to a new value, or by increasing and decreasing them. You can change the color of the turtle as well. Run the code below to see what happens: Then try the challenge: Complete the triangle above with a colour of your choice. Draw a square with sides which are four different shades of red.

4.Adding color with Trinket turtle

Repeating lines of code, instead of rewriting the instructions, is one of the fastest ways to get something done. For example, the square you created earlier uses the same two instructions four times. Rather than writing them out four times, you could write them out once but add an instruction to repeat them. In Python there are two types of loops that you are likely to use: a while loop and a for loop. If you want a section of code to repeat forever, or until a condition is met, then a while loop might be best. If you want to loop for a set number of times, then a for loop is preferable. Here, we have used a while True loop. This means that the code inside the loop (i.e. the code which is indented) will repeat forever. You can try it in Trinket to see what it does, but remember it will loop forever!
from turtle import Turtle, Screen
turtle = Turtle()
while True:
  turtle.forward(1)
  turtle.right(1)
This type of loop is not going to be very useful for drawing shapes with Turtle. In this example, a for loop has been used. Press Run to see what happens.

5.Loops Trinket turtle

A for loop repeats instructions a set number of times, in this case 8 times. A for loop has an associated variable (called i here). In this example, i starts from 0 and increases by 1 each time. Let's apply this to the code to draw a square:
from turtle import Turtle, Screen
turtle = Turtle()
for i in range(4):
  turtle.forward(100)
  turtle.right(90)
Copy and paste this code into the Trinket editor above and run it. The turtle has been asked to repeat two instructions four times to make a square. Once you have created one shape using a loop, you can repeat the shape again and again by putting it inside another loop. This is a great way to draw spirals. A spiral can be made by turning a small degree and then moving forward a small amount. The section of code for making a square is inside another for loop that repeats it 30 times, each time turning the cursor 25 degrees to make a pleasing spiral shape.Adapt your code by making it look like this:
from turtle import Turtle, Screen
turtle = Turtle()

for i in range(30):
  for i in range(4):
      turtle.forward(100)
      turtle.right(90)
  turtle.right(25)
Try to complete each of these challenges: 1. Alter the for loop so that it draws a more interesting spiral using one of the shapes you made earlier, like a triangle or circle? 2. Adding a few extra lines where you alter the variables R, G, and B would allow you to make a multicoloured spiral.

Embedding your program into your web page HTML

If you would like to embed your turtle program in a web page, do the following: Create your program, hit save, and then share. You can get the link code or the embed code. Copy the embed code, and place it into your html code as an iframe:

<iframe src="https://trinket.io/embed/python/5f5a86f2b2" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>