Mutability

Mutability in programming refers to the ability of an object to be changed or modified after it had been created. Depending on the type of object, it may either be:


Mutable: Can be changed after creation.

Immutable: Cannot be changed after creation.

Understanding mutability is important in programming to control how data behaves in memory.

Example of mutable objects are variable.

Example of immutable objects are constants.

YouTube video link

Mutable Objects (Changeable)

What is a Mutable Object?

A mutable object is one that allows you to change its content (data) after it has been created without creating a new object. In simple terms, you can:


Add, remove, or update data.

Retain the same memory address after modification.

Examples of Mutable Objects

Lists → Example: ["apple", "banana", "cherry"]

Dictionaries → Example: {"name": "John", "age": 25}

Sets → Example: {1, 2, 3}

Bytearrays → Example: bytearray(b"hello")

Immutable Objects (Unchangeable)

What is an Immutable Object?

An immutable object is one that cannot be modified after it has been created. If you try to modify it, Python will create a new object instead of modifying the existing one.


Examples of Immutable Objects

Strings → Example: "apple"

Tuples → Example: ("apple", "banana")

Integers → Example: 5, 10, 20

Floats → Example: 3.14, 5.6

Booleans → Example: True, False


Comments

Popular posts from this blog

Absolute and relative path in HTML pages

Errors

goto PHP operator