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 library used to create graphics, patterns, and drawings by moving a virtual pen (called a turtle) on the screen using basic programming commands. Turtle is a module in Python that allows us to bring shapes, figures, and designs to life on a screen.

It is widely used by beginners and students to learn programming concepts through visual output.

 

i. Easy Learning

Turtle helps in learning coding by showing drawings instead of only text.

 

ii. Fun Learning

Shapes are drawn using simple commands, making coding more interesting.

 

iii. Creativity

Drawings and designs can be created using code.

 

iv. Easy to Find Mistakes

Drawings help in easily identifying errors in programs.

 

v. Learn Shapes

Helps in understanding shapes, angles, and positions.

 

vi. Better Thinking

Steps are followed to draw, which improves logical thinking and organization.

 

Basic Structure of a Turtle Program

 

import turtle

Imports the Turtle module so turtle graphics commands can be used.

Python cannot draw by itself. This line loads the Turtle library.

Without this line, Turtle commands will not work.

 

import turtle as t

# imports the turtle module and assigns it a short name (alias) t for easier use in the program.

screen = turtle.Screen( )

Creates a drawing window (screen) where the turtle can move and draw.

Think of it as opening a blank canvas or paper.

A white window appears.

It is needed when you want control over the screen.

You use it when you want to:

Change background color

Set window size

Change title

Detect key presses / mouse clicks

 

pen = turtle.Turtle( )
# creates a turtle object and assigns it to the variable pen, which is used to draw graphics on the screen.

turtle.done( )

# keeps the turtle graphics window open until it is closed manually by the user.

 

Think of it like:

Without done() → Movie starts and ends in 0.1 seconds

With done() → Movie plays and stays until you exit

 

📌 turtle.mainloop()

It keeps the Turtle graphics window open and starts an event loop that listens for user actions (like closing the window).

Keeps window open

Waits for user interaction

Prevents program from ending immediately


 

Important Turtle Commands

 

Turtle Motion

Turtle motion refers to the movement of the turtle on the screen. The turtle can move forward and backward in the direction it is facing.

 

Movement Commands

 

1. forward( ) - [fd] à Moves the turtle forward by a specified distance.
Syntax: forward(distance)
Example: pen.forward(100)

 

2. backward( ) - [bk] à Moves the turtle backward by a specified distance.
Syntax: backward(distance)
Example: pen.backward(50)

 

3. right( ) - [rt]  à Turns the turtle clockwise by a specified angle.
Syntax: right(angle)
Example: pen.right(90)

 

4. left( ) - [lt] à Turns the turtle anticlockwise by a specified angle.
Syntax: left(angle)
Example: pen.left(90)

 

Shape Commands

 

5. circle( ) à Draws a circle of a given radius.
Syntax: circle(radius)
Example: pen.circle(50)

 

Color and Style Commands

 

6. color( ) à Changes the pen color.
Syntax: color(color_name)
Example: pen.color("red")

 

7. fillcolor( )  à Sets the fill color of a shape.
Syntax: fillcolor(color_name)
Example: pen.fillcolor("yellow")

8. pensize( ) à Sets the thickness of the drawing line.
Syntax: pensize(size)
Example: pen.pensize(5)

 

9. speed( ) à Controls the drawing speed of the turtle.
Syntax: speed(value)
Example: pen.speed(0)

Note:
0 = Fastest
1 = Slowest

3 = Slow

5 = medium
10 = Fast

 

Pen Control Commands

10. penup( ) à Lifts the pen so movement does not draw.
Syntax: penup()
Example: pen.penup()

 

11. pendown( ) à Places the pen down to resume drawing.
Syntax: pendown( )
Example: pen.pendown( )

 

Position Commands

 

12. goto( ) à Moves the turtle to a specific coordinate (x, y).
Syntax: goto(x, y)
Example: pen.goto(100, 50)  # moves the turtle to x = 100 and y = 50.

 

13. home( ) à Moves the turtle to the starting position (0,0).
Syntax: home()
Example: pen.home()

 

Coordinate System in Turtle

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

Positive x → Right

Negative x → Left

Positive y → Up

Negative y → Down

 

Fill Commands

14. begin_fill( ) à Starts filling a shape with color.
Syntax: begin_fill()
Example: pen.begin_fill()

 

15. end_fill( ) à Ends filling a shape with color.
Syntax: end_fill()
Example: pen.end_fill()

 

 

 

 

Text Command

16. write( ) à Displays text on the screen.
Syntax: write(text)
Example: pen.write("Hello Python")

 

Screen Commands

17. clear( ) à Removes all drawings from the screen.
Syntax: clear()
Example: pen.clear()

 

18. reset( ) à Clears the screen and returns the turtle to its starting position.
Syntax: reset()
Example: pen.reset()

 

19. hideturtle( ) à Hides the turtle cursor.
Syntax: hideturtle()
Example: pen.hideturtle()

 

20. showturtle( ) à Displays the turtle cursor.
Syntax: showturtle()
Example: pen.showturtle()

 

21. bgcolor( ) à Changes the background color of the screen.
Syntax: screen.bgcolor(color_name)
Example: screen.bgcolor("lightblue")

 

22. turtle.done( ) à Keeps the Turtle graphics window open.
Syntax: turtle.done()
Example: turtle.done()

 

📌 pen.shape("turtle")

It changes the shape of the turtle cursor (the drawing pen) on the screen.

Default shape: small arrow

Why use it?

Makes drawing easier to understand visually

More fun for beginners

Helps you actually see the turtle moving (not just a boring arrow pretending to be a programmer)

It is a Turtle graphics function used to change the shape of the turtle cursor to a turtle shape so that the movement is more visible and attractive
















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.