Posts

Showing posts from July, 2024

IMMUTABLES AND MUTABLES IN PYTHON

Image
 IMMUTABLES AND MUTABLES IN PYTHON Introduction to Python's Object Types and Functionality As a versatile programming language, Python distinguishes between mutable and immutable objects, influencing how data is stored and manipulated. Understanding these distinctions is crucial for efficient programming and debugging. I.D. and Type in Python In Python, every object has a unique identifier ( id ) and a type ( type ). The id represents the memory address where the object is stored while type  indicating the object's class or type. Let's see this in action: python a = 10 print ( id (a)) # Outputs a unique identifier for integer 10 print ( type (a)) # Outputs <class 'int'> Mutable and Immutable Objects Python objects are categorized as mutable or immutable based on whether their state can be changed after creation. Immutable objects, such as integers, strings, and tuples, cannot be modified once created. For example: python b = "Hello" # Attempti...
Image
HOW OBJECT AND CLASS ATTRIBUTES WORK. Class attributes: Class attributes are defined outside a method in the class definition and are shared by all class instances. It can also be easily modified by just reassigning a value. Example in code: #define the class class Car:          #showing class attribute          wheels = 4          #updating class attribute          wheels = 5 This makes it a class attribute because it's outside a method in the class. Instance attributes: Instance attributes are defined inside the "__init__" method of Python and are specific to each instance. Example in code: #define the class class Car:          #init method          def __init__(self, wheels):                    self.__wheels = wheels This makes it an instance attribute because it is defined inside P...