Related Articles

Related Articles

Printing HELLO with turtle module
  • Last Updated : 06 Oct, 2020

Turtle is a Python Module which allows us to draw a various geometrical illustration by just importing it in Python and using the inbuilt function of the turtle Module. We can use functions like turtle.forward(…) and turtle.right(…) which can move the turtle around. Turtle is a beginner-friendly way to learn python by running some basic commands and viewing the turtle do it graphically. It is like a drawing board that allows you to draw over it. The turtle module can be used in both object-oriented and procedure-oriented ways.
 

Turtle methods used are as follows:
 

Functions used for moving the turtle

Function Description
forward(no. of steps) This function is used to move the turtle forward the specified number of steps.
backward(no. of steps) This function is used to move the turtle forward the specified number of steps.
left(angle) This function is used to rotate the turtle left/anti-clockwise with the specified angle.
right(angle) This function is used to rotate the turtle right/clockwise with the specified angle.

 

Function used for knowing the turtle’s state:

position()– This function is used to get the coordinates/position of the turtle pointer. No parameter is required here as this function is not used for giving input but is used to output the position of turtle in the console window.
 

Functions used for colouring:

Function Description
bgcolor(“color”) This function is used to give background color to the turtle window.
pencolor(“color”) This function is used to give color to the turtle pen.
fillcolor(“color”) This function is used to fill color in any closed shape.
color(pencolor, fillcolor) This is a shorthand function used to assign pencolor and fillcolor at the same time.

 



Function used for pen control:

Function Description
penup() or pu() This function is used to lift pen from the current position.
goto() This function is used to move the pen to a new location after lifting.
pendown() or pd() This function is used again put down the pen to a new location.
pensize(int) This function is used to specify the width of the line drawn by pen.

Note: To know more about turtle click here
Example:
 

python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to
# demonstrate basics of turtle
 
# Importing and making a turtle object
# with background color as white
import turtle
 
frame = turtle.Screen().bgcolor("White")
draw = turtle.Turtle()
draw.left(90)
draw.forward(100)
draw.right(90)
draw.forward(100)

chevron_right


Output: 
 

basic example of turtle

After having learned the basics of turtle, let’s write the code for printing ‘HELLO’. Below is the implementation.
 

python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to
# demonstrate printing HELLO
# using turtle
 
# Here frame is initialized with
# background colour as "White"
import turtle
frame = turtle.Screen().bgcolor("White")
draw = turtle.Turtle()
 
# The colour, width and speed of the pen is initialized
draw.color("Green")
draw.width(3)
draw.speed(10)
 
# Now lets get started with actual code
# printing letter H
draw.left(90)
draw.forward(70)
draw.penup()
draw.goto(0, 35)
draw.pendown()
draw.right(90)
draw.forward(30)
draw.penup()
draw.goto(30, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
 
# printing letter E
draw.penup()
draw.goto(40, 0)
draw.pendown()
draw.right(180)
draw.forward(70)
draw.right(90)
draw.forward(35)
draw.penup()
draw.goto(40, 35)
draw.pendown()
draw.forward(35)
draw.penup()
draw.goto(40, 0)
draw.pendown()
draw.forward(35)
 
# printing letter L
draw.penup()
draw.goto(90, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
draw.left(90)
draw.forward(35)
 
# printing letter L
draw.penup()
draw.goto(135, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
draw.left(90)
draw.forward(35)
 
# printing letter O
draw.penup()
draw.goto(210, 70)
draw.pendown()
for i in range(25):
    draw.right(15)
    draw.forward(10)

chevron_right


Output: 
 

Printing 'Hello ' using turtle

 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :