Wednesday, June 3, 2026

4.4 GRAPHICS IN PYTHON USING TURTLE



4.4 GRAPHICS IN PYTHON USING TURTLE

Introduction to Turtle

Turtle is a Python module used to create drawings, shapes, patterns and designs on the screen. It works by moving a virtual pen called a turtle using simple commands.

Turtle helps beginners learn programming through visual output. Instead of printing only text, it can draw shapes, patterns, and pictures.

 

Features of turtle

Feature

Description

Easy Learning

Helps learn programming through drawings and graphics.

Fun Learning

Makes coding interesting by creating shapes and designs.

Creativity

Allows users to create different drawings and patterns.

Easy Error Detection

Mistakes can be identified easily by looking at the drawing.

Learn Shapes

Helps understand shapes, angles, and positions.

Better Thinking

Improves logical thinking by following drawing steps.

 

 

Basic Structure of a Turtle Program

 

import turtle

Meaning: Imports the Turtle module so Turtle commands can be used.

Simple Explanation: Python cannot draw by itself. This command loads the Turtle library.

Without it: Turtle programs will not work.

 

import turtle as t

Meaning: Imports the Turtle module and gives it a short name (t).

Simple Explanation: Instead of writing turtle every time, we can write only t.

 

screen = turtle.Screen( )

Meaning: Creates a drawing window where the turtle can move and draw.

Simple Explanation: It opens a blank screen or canvas for drawing.

Used for:

  • Changing background color
  • Setting window size
  • Changing window title
  • Detecting keyboard and mouse actions

 

pen = turtle.Turtle( )
Meaning:
Creates a turtle object and stores it in the variable pen.

Simple Explanation: This creates the drawing turtle that will move and draw on the screen.

 

turtle.done( )

Meaning:
Keeps the Turtle graphics window open until it is closed manually.

Simple Explanation:
Stops the program from closing immediately after drawing.

 

Without done(), the window may close immediately after the drawing finishes. Humans do enjoy spending ten minutes drawing a square only to watch the window vanish instantly. 🐢

 

 

turtle.mainloop( )

Meaning:
Keeps the Turtle window open and waits for user actions.

Simple Explanation:
It allows the program to keep running until the user closes the window.

Uses

  • Keeps the window open
  • Waits for keyboard or mouse events
  • Prevents the program from ending immediately

 

🐢 Important Turtle Commands

1. Turtle Motion Commands

Turtle motion commands are used to move the turtle on the screen. The turtle can move forward, backward, left, and right while drawing.

Command

Definition

Syntax

Example

forward() / fd()

Moves the turtle forward by a specified distance.

forward(distance)

pen.forward(100)

backward() / bk()

Moves the turtle backward by a specified distance.

backward(distance)

pen.backward(50)

right() / rt()

Turns the turtle clockwise by a specified angle.

right(angle)

pen.right(90)

left() / lt()

Turns the turtle anticlockwise by a specified angle.

left(angle)

pen.left(90)


2. Shape Command

Command

Definition

Syntax

Example

circle()

Draws a circle with a specified radius.

circle(radius)

pen.circle(50)

 

3. Color and Style Commands

These commands are used to change the appearance of drawings.

Command

Definition

Syntax

Example

color()

Changes the pen color.

color(color_name)

pen.color("red")

fillcolor()

Sets the fill color of a shape.

fillcolor(color_name)

pen.fillcolor("yellow")

pensize()

Changes the thickness of the drawing line.

pensize(size)

pen.pensize(5)

speed()

Controls the drawing speed of the turtle.

speed(value)

pen.speed(0)

 

Speed Values

0 – Fastest , 1 – Slowest , 3 – Slow , 5 – Medium , 10 - Fast

 

4. Pen Control Commands

These commands control whether the turtle draws while moving.

Command

Definition

Syntax

Example

penup()

Lifts the pen so movement does not draw lines.

penup()

pen.penup()

pendown()

Places the pen down so movement draws lines.

pendown()

pen.pendown()

Example

pen.penup()
pen.forward(100)

pen.pendown()
pen.forward(100)

The first movement draws nothing. The second movement draws a line.

 

5. Position Commands

These commands control the turtle's position on the screen.

Command

Definition

Syntax

Example

goto()

Moves the turtle to a specific coordinate (x, y).

goto(x,y)

pen.goto(100,50)

home()

Returns the turtle to the starting position (0,0).

home()

pen.home()

 

Direction

Coordinate Value

Right

Positive X (+X)

Left

Negative X (-X)

Up

Positive Y (+Y)

Down

Negative Y (-Y)

📌 The center of the Turtle screen is (0,0).

 

6. Fill Commands

These commands are used to fill shapes with color.

Command

Definition

Syntax

Example

begin_fill()

Starts filling a shape with color.

begin_fill()

pen.begin_fill()

end_fill()

Ends filling and fills the shape.

end_fill()

pen.end_fill()

 

7. Text Command

Command

Definition

Syntax

Example

write()

Displays text on the screen.

write(text)

pen.write("Hello Python")

Example

pen.write("Welcome to Turtle Graphics")

 

8. Screen Commands

Command

Definition

Syntax

Example

clear()

Removes all drawings from the screen.

clear()

pen.clear()

reset()

Clears the screen and returns the turtle to the starting position.

reset()

pen.reset()

hideturtle()

Hides the turtle cursor.

hideturtle()

pen.hideturtle()

showturtle()

Displays the turtle cursor.

showturtle()

pen.showturtle()

bgcolor()

Changes the screen background color.

screen.bgcolor(color)

screen.bgcolor("lightblue")

turtle.done()

Keeps the Turtle window open.

turtle.done()

turtle.done()

 

9. Shape Command

pen.shape("turtle")

Definition : The shape() function is used to change the appearance of the turtle cursor.

Syntax

pen.shape("turtle")

Common Shapes

Shape

Example

"arrow"

Default arrow cursor

"turtle"

Turtle shape

"circle"

Circle shape

"square"

Square shape

"triangle"

Triangle shape

"classic"

Classic turtle cursor

 
















Sunday, May 31, 2026

4.3 Concept of Module, Library and Packages in Python

 


🐍 Modules, Packages & Libraries in Python

📄 Module

Definition

A module is a single Python file containing reusable code such as functions, classes, variables, and statements that can be imported and used in other Python programs.


Uses / Importance / Advantages

  1. Reuses code in different programs.
  2. Reduces code duplication.
  3. Makes code easy to organize.
  4. Simplifies testing and debugging and improves code maintenance.

Types of Modules

  • Built-in Modules: Provided by Python.
  • User-Defined Modules: Created by programmers.

Module Examples

Built-in Modules

  • math
  • random
  • datetime
  • os
  • sys

Example:

import math
print(math.sqrt(25))

User-Defined Module

calculator.py

def add(a, b):
    return a + b

Additional Points

  • File extension: .py
  • Import methods:

import module_name
from module_name import function
import module_name as alias


📦 Package

Definition

A package is a collection of related Python modules organized in a directory (folder) to provide a structured way of managing and reusing code.


Uses / Importance / Advantages

  1. Groups related modules together.
  2. Organizes large projects efficiently.
  3. Improves code management.
  4. Prevents naming conflicts and makes applications scalable.

 

Package Structure

calculator/
── add.py
── subtract.py
└── __init__.py

Package Examples

  • numpy.random
  • django.contrib
  • email.mime

Import Example

from calculator.add import add

Additional Points

  • Contains one or more modules.
  • Usually includes __init__.py.
  • Can contain sub-packages.
  • Helps in hierarchical organization of code.

🏛️ Library

Definition

A library is a collection of packages and modules that provides reusable functionality for specific tasks, allowing developers to perform complex operations without writing code from scratch.

Uses / Importance / Advantages

  1. Provides ready-made code.
  2. Saves development time.
  3. Increases productivity.
  4. Reduces coding effort and helps perform complex tasks easily

Types of Libraries

1. Standard Library

Comes pre-installed with Python.

Examples:

  • math
  • os
  • random
  • datetime

2. External Library

Installed using pip.

Examples:

  • NumPy
  • Pandas
  • Matplotlib
  • Requests
  • TensorFlow

Popular Python Libraries

Library

Use

NumPy

Numerical Computing

Pandas

Data Analysis

Matplotlib

Data Visualization

Requests

API Calls

TensorFlow

Machine Learning

OpenCV

Computer Vision

Django

Web Development

Interview Examples

  • Module: math, random, calculator.py
  • Package: numpy.random, django.contrib
  • Library: NumPy, Pandas, Matplotlib, TensorFlow

📦 Python pip

Definition

pip (Pip Installs Packages) is Python's package manager used to install, update, and remove external packages and libraries.

Common Commands

pip install numpy
pip uninstall numpy
pip list
pip --version

Importance of pip

  1. Installs external libraries easily.
  2. Updates existing packages.
  3. Removes unwanted packages.
  4. Manages project dependencies and simplifies library management.

📊 Difference Between Module, Package & Library

Feature

Module

Package

Library

Meaning

Single Python file

Folder of modules

Collection of packages/modules

Size

Smallest unit

Medium unit

Largest unit

Extension

.py file

Directory

Collection

Purpose

Reuse code

Organize modules

Provide complete functionality

Example

math.py

numpy.random

NumPy




🎤 Final Interview One-Liner

A module is a single Python file, a package is a collection of related modules, and a library is a collection of packages and modules that provides reusable functionality.