{"id":102,"date":"2026-03-02T08:01:59","date_gmt":"2026-03-02T03:01:59","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=102"},"modified":"2026-03-15T17:02:54","modified_gmt":"2026-03-15T12:02:54","slug":"classes-and-objects","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/","title":{"rendered":"Classes and Objects"},"content":{"rendered":"\n<p>Classes and objects are the foundation of Object-Oriented Programming (OOP).<\/p>\n\n\n\n<p>They help you structure your code in a clean, reusable, and organized way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHAT IS A CLASS?<\/strong><\/h2>\n\n\n\n<p>A class is a blueprint or template used to create objects.<\/p>\n\n\n\n<p>It defines:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Attributes (variables)<\/li>\n\n\n\n<li>Methods (functions)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example of a Class<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car:<br>    pass<\/pre>\n\n\n\n<p>This is a simple class named <code>Car<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHAT IS AN OBJECT?<\/strong><\/h2>\n\n\n\n<p>An object is an instance of a class.<\/p>\n\n\n\n<p>It represents a real-world entity created from a class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car:<br>    passcar1 = Car()<br>print(type(car1))<\/pre>\n\n\n\n<p>Here, <code>car1<\/code> is an object of the <code>Car<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>CLASS WITH ATTRIBUTES (USING CONSTRUCTOR)<\/strong><\/h2>\n\n\n\n<p>The constructor method <code>__init__()<\/code> initializes object data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car:<br>    def __init__(self, brand, color):<br>        self.brand = brand<br>        self.color = colorcar1 = Car(\"Toyota\", \"White\")<br>print(car1.brand)<br>print(car1.color)<\/pre>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__init__<\/code> runs automatically when object is created<\/li>\n\n\n\n<li><code>self<\/code> refers to the current object<\/li>\n\n\n\n<li><code>brand<\/code> and <code>color<\/code> are attributes<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>CLASS WITH METHODS<\/strong><\/h2>\n\n\n\n<p>Methods are functions defined inside a class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car:<br>    def __init__(self, brand):<br>        self.brand = brand    def start(self):<br>        print(self.brand, \"car is starting.\")car1 = Car(\"Honda\")<br>car1.start()<\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>start()<\/code> is a method<\/li>\n\n\n\n<li>It uses object data<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MULTIPLE OBJECTS<\/strong><\/h2>\n\n\n\n<p>You can create many objects from one class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">car1 = Car(\"Toyota\")<br>car2 = Car(\"BMW\")car1.start()<br>car2.start()<\/pre>\n\n\n\n<p>Each object has its own data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>REAL-WORLD ANALOGY<\/strong><\/h2>\n\n\n\n<p>Class = Blueprint of a house<br>Object = Actual house built from that blueprint<\/p>\n\n\n\n<p>Class = Student<br>Objects = Hira, Ali, Ahmed<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY DIFFERENCE<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Class<\/th><th>Object<\/th><\/tr><\/thead><tbody><tr><td>Blueprint<\/td><td>Instance<\/td><\/tr><tr><td>Defined once<\/td><td>Created multiple times<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHY USE CLASSES AND OBJECTS?<\/strong><\/h2>\n\n\n\n<p>\u2022 Organize large programs<br>\u2022 Make reusable code<br>\u2022 Model real-world systems<br>\u2022 Improve maintainability<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAY<\/strong><\/h2>\n\n\n\n<p>A class defines structure and behavior.<br>An object is a real instance created from that class.<\/p>\n\n\n\n<p>Understanding classes and objects is the first major step in mastering OOP in Python.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Classes and Objects<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773465318141\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p><\/p>\n","protected":false},"menu_order":47,"template":"","class_list":["post-102","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Classes and Objects - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python classes and objects, constructors, methods, and OOP concepts to write clean, reusable, and organized code efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Classes and Objects - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python classes and objects, constructors, methods, and OOP concepts to write clean, reusable, and organized code efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T12:02:54+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/classes-and-objects\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/classes-and-objects\\\/\",\"name\":\"Classes and Objects - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:01:59+00:00\",\"dateModified\":\"2026-03-15T12:02:54+00:00\",\"description\":\"Learn Python classes and objects, constructors, methods, and OOP concepts to write clean, reusable, and organized code efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/classes-and-objects\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/classes-and-objects\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/classes-and-objects\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Classes and Objects\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Classes and Objects - One Language. Endless Possibilities","description":"Learn Python classes and objects, constructors, methods, and OOP concepts to write clean, reusable, and organized code efficiently.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/","og_locale":"en_US","og_type":"article","og_title":"Classes and Objects - One Language. Endless Possibilities","og_description":"Learn Python classes and objects, constructors, methods, and OOP concepts to write clean, reusable, and organized code efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-15T12:02:54+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/","url":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/","name":"Classes and Objects - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:01:59+00:00","dateModified":"2026-03-15T12:02:54+00:00","description":"Learn Python classes and objects, constructors, methods, and OOP concepts to write clean, reusable, and organized code efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/classes-and-objects\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Classes and Objects"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}