How to make a triangle in python

How to make a triangle in python

Drawing with Turtle in Python is really fun. In the past handful of tutorials, we learned how to import the Turtle module for use in our programs saw how to make the turtle (pen) move on the canvas, made the turtle change directions on the canvas, saw how to use loops in turtle, and made drawings of shapes using variables. Now we will take a look at drawing another type of Polygon, the triangle using the Turtle library in Python.


Define A Triangle Function

To draw a triangle, we want to use a function, and it should make use of variables. We do this so that we can call the function many times if we like to draw many triangles of different sizes.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


draw_triangle()

done()

How to make a triangle in python

Nice! The triangle function works. Notice that the loop uses 3 iterations shown by range(3) whereas when we drew a square it used 4 iterations in the loop. Another thing we notice is that we are passing in 120 as the degrees to turn for the left() function. Why is that? This is because when drawing a triangle, you need to use the outside angle rather than the inside angle. What we are drawing here is an equilateral triangle since all three angles of an equilateral triangle add up to 180 degrees.

How to make a triangle in python


Drawing More Triangles

Now we can use the draw_triangle() function a couple of times in combination with moving the turtle to a different spot on the canvas for a nice effect.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


draw_triangle()
right(180)
forward(100)
right(180)
draw_triangle(200)

done()

How to make a triangle in python

The following iteration takes that idea a step further to draw three triangles in different spots on the canvas.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


draw_triangle()
right(180)
forward(100)
right(180)
draw_triangle(200)
right(180)
forward(100)
right(180)
draw_triangle(250)

done()

How to make a triangle in python


Drawing Triangles In A Loop

Calling the draw_triangle() inside of a loop makes for some really cool effects.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


for i in range(20):
    draw_triangle()
    right(1)

done()

How to make a triangle in python

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('triangle')


def draw_triangle(length=150):
    for i in range(3):
        forward(length)
        left(120)


for i in range(40):
    draw_triangle()
    right(10)

done()

How to make a triangle in python

How do you draw shapes in python?

Object-oriented Programming in Python: Create Your Own Adventure Game.
from shapes import Paper, Triangle, Rectangle, Oval..
paper = Paper().
rect1 = Rectangle().
rect1. set_width(200) rect1. set_height(100) rect1. ... .
rect1. draw().
paper. display().
# put the code to create the 2nd Rectangle here paper. display().

Is triangle a function in python?

triangular() method in Python. triangular() is an inbuilt method of the random module. It is used to return a random floating point number within a range with a bias towards one extreme. if the parameters are (10, 100, 20) then due to the bias, most of the random numbers generated will be closer to 10 as opposed to 100 ...

How do you create a pattern in python?

Pattern - 1: Number Pattern.
rows = int(input("Enter the number of rows: ")).
# Outer loop will print number of rows..
for i in range(rows+1):.
# Inner loop will print the value of i after each iteration..
for j in range(i):.
print(i, end=" ") # print number..
# line after each row to display pattern correctly..
print(" ").