Python Turtle Graphics Tutorial for Absolute Beginners - drawing triangles

preview_player
Показать описание
Learn programming in Turtle, a Python Library, in this 4 minute tutorial for beginners. Playing around with the Turtle library is a great way to practise the basics susch as variables, lists, looping, functions and more.

🦁 Who are you?
My name is Raza. I am an IT - manager right now, but I’ve been an accountant for the majority of my career.
I’m in love with #Python :)

You can find me here:
—————————————————————————————

Goals for 2021
Training Python hours: 70/1000
Python/Django Projects: 3/30
Subscribers: 4588/10,000

—————————————————————————————-
#100SecondsOfCode
#pythonforbeginners #programming

—————————————————————
Drawing with Turtle - Triangles
—————————————————————

In this tutorial we will create this pattern in the python library called turtle

The basic shape in this pattern is a triangle, so we’ll draw a triangle first

import turtle

So here we import the library turtle, so we can use all the functions defined in this library
The forward function draws a line and the left function takes an angel, in this case we take an angel of 120

Now that we have our basic shape we can repeat the basic shape for the pattern
Before we do that we need to start at top left corner
The current shape is drawn at the center of the canvas

To do this we need to move the pen to the top left corner.
Turtle works with coordinates, so we need to pass the coordinates to the pen for the pen to move
Here we lifted the pen, because we don’t want to draw
Then we move the pen and put the pen down again on the canvas

Now we need to create a line of triangles

For numbers in range(7):
Here I adjusted the code for the triangle
I’ve drawn an extra line to adjust the starting position for the next triangle
By drawing the extra line the starting point is this corner, instead of this one
Now I need to go to the next row
For this I need to pass new coordinates and tell the pen where the next starting point is.
The next row is defined by the height of the triangle
Because we want to start immediately under the bottom of the first row of triangels
I used an online calculator to find out the height of our triangle which is 86
Because all the lines are the same height the formula is simple:
100*√3/2 = 86
Link to the calculator in the description

We need to decrease the starting coordinate on the Y axis, which is the vertical axis, with 86.

X = 250-86

For numbers in range(7):

We can simplify the code by using a for loop to continuously draw a triangle and move down a row and use a function to draw the triangle. Lets do that.

import turtle
t = turtle.Turtle()
def triangle():
for numbers in range(7):
x = 250 - (86*numbers)
for number in range(7):
triangle()

The first part of the code is the function to keep the code clean.
If we want to draw a triangle we can call the function, which we do right here.
The first loop determines the position of the pen.
As we know, python starts counting at zero.
If we start the first loop, we pass the number zero. Zero times 86 is zero and thus nothing is subtracted from 250.
Therefor the starting position is the top left corner
With each iteration, the next starting position is decreased on the Y axis with 86 points
Next there is a nested loop to draw the triangle 7 times by calling the function
After this the initial loop is started again and we the pen is lowered with 86 and starts drawing again.
Рекомендации по теме
Комментарии
Автор

This is the best channel to learn 🙏
Also, plzz tell the background music of piano you are using sir

ghoulbond
Автор

In which app or whatever r u doing ths all, like I've to switch on the laptop n do wht😄
, plz let me know

manahilghaffar