Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Turtle in Python

0 Replies, 1773 Views

[Image: BFMMlbcQvml9HSqXcvNp]


Turtle:
      Hi guys, I hope you are doing well. Sorry for uploading late but I have brought you the most interesting thing about python. In this tutorial you will learn the turtle method which is I think the most interesting thing about python. It is used for generating graphics in python. It will give a great understanding of loops and if statements to beginners. So without wasting any time let’s just jump into the code directly.

First of all we'll know how we can import turtle method in our program

Code:
import turtle


This command will bring you turtle method in your program so first of all let’s create a square using turtle

Code:
import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)


So this is the most simple way of writing a code to make a square but we will discuss many more ways of drawing shapes. First of all let’s understand the code turtle.forward(100) means make a line of 100 pixels and turtle.left(90) means turn the pen 90 degrees towards left. So I am giving you a task to change only the values of the above code and see what happens.

Now we will use input statements with turtle

Code:
import turtle
distance = int(input("Enter the length of your line: "))
angle = int(input("Enter the angles: "))
turtle.forward(distance)
turtle.left(angle)
turtle.forward(distance)
turtle.left(angle)
turtle.forward(distance)
turtle.left(angle)
turtle.forward(distance)
turtle.left(angle)

Most interesting thing is using loops with the turtle so you can create the most attractive designs.

Let’s first create a square using a loop and then we will jump into some advanced designs.

Code:
import turtle
for i in range(4):
    turtle.forward(100)
    turtle.left(90)


Loops are amazing, they make our task so easy so now let’s print some other designs..

Code:
import turtle

for i in range(100):
    turtle.forward(100 + i*10)
    turtle.left(90)


So as you can see I have just changed the values and the design is totally changed in this way you can create your design so easily.


Code:
# This program will draw any polygon for you depending on your input size
import turtle

distance = int(input("Enter the distance: "))
sides = int(input("Enter the sides: "))
for i in range(sides):
    turtle.forward(distance)
    turtle.left(360/sides)

Now I am going to create an advance design by changing values you should run it and see how it works

Code:
import turtle

distance = int(input("Enter the distance: "))
sides = int(input("Enter the sides: "))
for i in range(sides):
    turtle.forward(distance)
    turtle.left(360/sides)
    for i in range(sides):
        turtle.forward(distance)
        turtle.right(360/sides)

Here we use nested loops to create a design of polygon that looks like a flower.

Now we will use turtle with a infinity loop

Code:
import turtle

distance = int(input("Enter the distance: "))
angle = int(input("Enter the angle: "))

while True:
    distance -= 2
    turtle.forward(distance)
    turtle.left(angle)
    if distance < 0:
        break


Printing star using turtle

Code:
from turtle import *
star = Turtle()
i = 1

while i <= 5:
    star.right(144)
    star.forward(100)
    i += 1

input()


You must be wondering why we use input() in the last part of the program if it is to wait until we see our pattern clearly.  So that’s it for now before doing anything I suggest you to



Quote:Idea read the turtle documentation on python.org




"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
Love me like you do Heart
(This post was last modified: 06-08-2022, 09:39 PM by Covid-19.)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,178 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,535 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,569 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,775 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,541 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,725 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,737 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,757 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,728 05-26-2022, 08:50 PM
Last Post: Covid-19
  Tutorial Python 3.1: OOP Covid-19 2 1,559 05-24-2022, 06:37 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)