DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

Python Day 18: Introduction to Objects, Classes and Object-Oriented Programming

What is an Object?

An object is an entity that contains data and can perform actions.

Object = Entity

Examples from real life:

  • Samsung mobile
  • Television

Each object has its own properties and actions.

For example, a television has:

  • Properties: Brand, size, price, colour.
  • Actions: Watch, switch on, switch off, increase volume.

What is a Class?

A Class is a blueprint or logical definition used to create objects.

  • Class defines the structure and behaviour.
  • Object is an instance created from that class.

Class = Logical Entity

For example:

  • Television design or blueprint = Class
  • Actual television = Object

Everything in Python is an Object

Everything in Python is an object.

Numbers, strings, Boolean values, lists, and functions are all objects.

For example:

no = 10
name = 'Raja'
amount = 5.5
status = True
Enter fullscreen mode Exit fullscreen mode

Each variable refers to an object.

We can check the type of an object using type().

print(type(no))
print(type(name))
print(type(amount))
print(type(status))

Enter fullscreen mode Exit fullscreen mode
Output:

<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>
Enter fullscreen mode Exit fullscreen mode

Variable and Object

Consider this example:

no = 10

Here:

  • 10 is an integer object.
  • int is its class (data type).
  • no is a variable that refers to that object.

Another example:

name = 'Raja'
print(name)
Enter fullscreen mode Exit fullscreen mode

Here:

  • 'Raja' is a string object.
  • str is its class.
  • name is the variable referring to the string object.

Important: A variable is not the object itself. It is a name that refers to an object.

Top comments (0)