Python Coding Day 22 | Python OOP for Beginners: Classes, Objects, and Turtle Graphics
Python OOP for Beginners: Classes, Objects, and Turtle Graphics
Watch the lesson tutorial 🔻
If you are learning Python, you have probably heard the term OOP (Object-Oriented Programming). It sounds scary, but it is actually the best way to organize your code!
Today, we will break down the basics: Classes, Objects, and Methods, using practical examples you can run right now in Google Colab.
1. The Concept: The Blueprint and the House
Before we code, imagine a House Blueprint.
The Blueprint (Class) lists the design: it has doors, windows, and walls. You cannot live in the blueprint.
The House (Object) is the actual building made from that blueprint.
You can build many houses (Objects) from just one blueprint (Class).
2. Practical Example: The Turtle
Let's use a library called ColabTurtle to see this in action. Here, the code acts as our factory to create a "Turtle" object.
Step 1: Creating the Object
First, we need to install the library and create our first object. Let's call him timmy.
# 1. Install the library (Required for Google Colab)
!pip install ColabTurtle
# 2. Import the library
# Think of this as grabbing the "Blueprint" from the shelf
import ColabTurtle.Turtle as timmy
# 3. Initialize the Object
# This creates the turtle and the screen it lives on
timmy.initializeTurtle()
# 4. Check the Object
print(timmy)
What is happening here?
We installed
ColabTurtle.timmyis now our Object.initializeTurtle()is the command that builds the turtle and the canvas on your screen.
(Insert your Screenshot of the empty turtle canvas here)
3. Methods: Making the Object Move
Objects have Methods. A method is just a fancy word for a function that belongs to an object. It tells the object to do something.
Let's tell timmy to change his color and walk forward.
# We are still using the 'timmy' object we created
# Method 1: Change Appearance
# We tell the object to change its color to "coral"
timmy.color("coral")
# Method 2: Movement
# We tell the object to move forward by 100 pixels
timmy.forward(100)
Understanding the Code:
timmy.color(...): This accesses the turtle's properties and changes them.timmy.forward(100): This is an action. We are commanding the object to move.
(Insert your Screenshot of the coral turtle line here)
4. Using External Packages: PrettyTable
OOP is powerful because you can use Classes created by other developers. A great example is prettytable. It is a class designed to create beautiful text tables.
We don't need to know how it draws the lines; we just need to create an Object and use its Methods.
# 1. Install the package
!pip install prettytable
# 2. Import the Class
from prettytable import PrettyTable
# 3. Create the Object
# 'table' is now a new object created from the PrettyTable blueprint
table = PrettyTable()
# 4. Use Methods to add data
table.add_column("Pokemon Name", ["Pikachu", "Squirtle", "Charmander"])
table.add_column("Type", ["Electric", "Water", "Fire"])
# 5. Change Attributes (Settings)
# We align the text to the left ('l')
table.align = "l"
# 6. Print the final object
print(table)
The Output:
You will see a neatly formatted ASCII table like this:
+--------------+----------+
| Pokemon Name | Type |
+--------------+----------+
| Pikachu | Electric |
| Squirtle | Water |
| Charmander | Fire |
+--------------+----------+
(Insert your Screenshot of the table output here)
Summary
Class: The blueprint or template (e.g.,
PrettyTable).Object: The specific thing we built (e.g.,
timmyortable).Method: An action the object performs (e.g.,
.forward()or.add_column()).
Using OOP allows you to write clean, reusable code. Try changing the turtle's color or adding more columns to the table to practice!
- by Chirana Nimnaka

Comments
Post a Comment