Photo by Alexander Andrews on Unsplash
Python Objects and Classes for Beginners
In this article, we're going to talk about basics of objects and classes.
My Github Repository for learning about Classes and Objects
Text Analysis Exercise
https://github.com/Akina-Aoki/Data-Science-Files/blob/main/text%20analysis%20mini%20project.ipynb
Structure of Classes and Objects
Introduction to Classes and object
Python is an object-oriented programming (OOP) language, which means it uses a paradigm centered around objects and classes.
Classes
A class is a blueprint or template for creating objects. It defines the structure and behavior that its objects will have. In Python, classes are created using the class
keyword.
Creating Classes
When you make a class, you decide what stuff (data) and actions (functions) objects of that class can have. Stuff is like variables, and actions are like functions.
In our example, we'll create two types of classes: a circle and a rectangle. First, we need to figure out all the information that defines each class. We call this info attributes.
For the Circle class, we'll have attributes like radius and color. For the Rectangle class, we'll have attributes like color, height, and width.
Instances of a Class: Objects and Attributes
Objects
Objects in Python represent things in the real world or abstract ideas. They can be concrete, like a car, or conceptual, like a student's grade. Each object has two main parts:
State
This is the information or characteristics that describe the object.
Behavior
These are the actions or functions that the object can do. In Python, these functions are called methods. They can change the object's state or do specific tasks.
When we create an object from a class, it's called an instance. In the example, we see three instances of the class
"circle" named blue circle, red circle, and yellow circle. These instances each have their own unique characteristics and actions.
The colour attribute for the Blue Circle is the colour blue, for the Red Circle object the colour attribute is red, and for the Yellow Circle the colour attribute is yellow.
Methods
Methods are like special tools for objects. They're functions that help objects do things. For example, if you want to make a circle bigger, you can use a method called add_radius(r) to increase its size by r. It's a way to change or interact with objects in Python.
Strucutre
Disclosure:
This post covers insights from the IBM Data Science Professional Certificate on Coursera, focusing on Python programming, particularly classes and objects. For more on the course, visit Coursera's Python for Data Science, AI & Development | Coursera.