Mutable and Immutable Objects, List: Creating, Initializing, Accessing, Slicing, and Traversing List

Python objects are classified into Mutable and Immutable based on whether their values can be changed after creation.

Mutable Objects:

Mutable objects can be modified after they are created.

Examples

  1. List
  2. Dictionary
  3. Set
  4. Bytearray

Example

numbers = [10, 20, 30]
numbers[1] = 50

print(numbers)

Output

[10, 50, 30]

Here, the list is modified without creating a new object.

Characteristics

  1. Values can be changed.
  2. Memory location remains the same after modification.
  3. Suitable for dynamic data.

Immutable Objects:

Immutable objects cannot be changed after creation.

Examples

  1. String
  2. Integer
  3. Float
  4. Tuple
  5. Boolean

Example

name = "Python"

name = name + " Programming"

print(name)

Output

Python Programming

A new string object is created instead of modifying the existing one.

Characteristics

  1. Values cannot be modified.
  2. New object is created when changes are made.
  3. More secure and memory efficient.

Difference Between Mutable and Immutable Objects

Basis Mutable Immutable
Meaning Can be modified Cannot be modified
Memory Same object remains New object created
Performance Faster modifications Safer data handling
Examples List, Set, Dictionary String, Tuple, Integer

List in Python:

A List is an ordered collection of elements enclosed within square brackets [ ].

Features of List

  1. Ordered collection.
  2. Mutable in nature.
  3. Allows duplicate values.
  4. Can store different data types.
  5. Supports indexing and slicing.

Example

student = ["Kaif", 20, 85.5, "Commerce"]

Creating a List:

Lists can be created using square brackets.

Example

fruits = ["Apple", "Mango", "Orange"]
print(fruits)

Output

['Apple', 'Mango', 'Orange']

Initializing a List:

Initialization means assigning values while creating the list.

Example 1

numbers = [10, 20, 30, 40]

Example 2

mixed = ["Python", 100, 45.5, True]

Example 3

empty_list = []

Accessing List Elements:

List elements are accessed using indexes.

Positive Indexing

fruits = ["Apple", "Mango", "Orange"]

print(fruits[0])
print(fruits[1])

Output

Apple
Mango

Negative Indexing

fruits = ["Apple", "Mango", "Orange"]

print(fruits[-1])
print(fruits[-2])

Output

Orange
Mango

Index Representation

Element Apple Mango Orange
Positive Index 0 1 2
Negative Index -3 -2 -1

Slicing a List:

Slicing extracts a portion of a list.

Syntax

list[start:end:step]

Example

numbers = [10, 20, 30, 40, 50, 60]

print(numbers[1:4])

Output

[20, 30, 40]

More Examples

numbers = [10, 20, 30, 40, 50, 60]

print(numbers[:3])
print(numbers[2:])
print(numbers[::2])
print(numbers[::-1])

Output

[10, 20, 30]
[30, 40, 50, 60]
[10, 30, 50]
[60, 50, 40, 30, 20, 10]

Slicing Rules

  1. Start index is inclusive.
  2. End index is exclusive.
  3. Step determines interval.
  4. Negative indexing is supported.

Traversing a List:

Traversing means accessing each element of a list one by one.

Method 1: Using for Loop

fruits = ["Apple", "Mango", "Orange"]

for fruit in fruits:
print(fruit)

Output

Apple
Mango
Orange

Method 2: Using Index

fruits = ["Apple", "Mango", "Orange"]

for i in range(len(fruits)):
print(fruits[i])

Output

Apple
Mango
Orange

Method 3: Using while Loop

fruits = ["Apple", "Mango", "Orange"]

i = 0

while i < len(fruits):
print(fruits[i])
i += 1

Output

Apple
Mango
Orange

Advantages of Lists

  1. Easy to create and use.
  2. Can store multiple values in a single variable.
  3. Supports indexing and slicing.
  4. Allows modification of elements.
  5. Supports various built in methods.

Leave a Reply

error: Content is protected !!