Chapter 4 – Lists and Tuples in Python


📌 lists.py

name = ["Prathamesh", "hello", 3890273, 873487.23982398, True, None]
print(name[4])  # Accessing 5th element: True

name[4] = False  # Modifying an element
print(name[4])   # Now it prints False

print(name[1:5:2])  # Slicing with a step of 2 → ['hello', 873487.23982398]

🧠 Concepts:


📌 listmethods.py

name = ["Prathamesh", "hello", 3890273, 873487.23982398, True, None]
name.append(False)  # Adds item at end
print(name)

l1 = [12, 212, 3, 44, 55252, 5, 355, 363, 837, 29867, 5234967, 93485, 42]
l1.sort()           # Sorts the list in ascending order
print(l1)

l1.pop(12)          # Removes element at index 12
print(l1)

l1.count(3)         # Returns count of value 3
print(l1)

l1.reverse()        # Reverses the list in-place
print(l1)

l1.insert(3, 3333)  # Inserts 3333 at index 3
print(l1)

l1.remove(3)        # Removes first occurrence of 3
print(l1)

🧠 Important List Methods:

Method Description
.append(x) Add x to end of list
.sort() Sort list (ascending, by default)
.pop(i) Remove element at index i
.count(x) Count number of times x appears
.reverse() Reverse the order of elements
.insert(i,x) Insert x at index i
.remove(x) Remove first occurrence of x

🔎 Note: List methods change the original list (in-place operations).


📌 tuple.py

# Wrong: a = (1)       → This is an integer, not a tuple
# Correct: a = (1,)    → This is a single-element tuple

name = (12, 27823, True, "Prathamesh", 263.782367)
print(name)
print(type(name))  # <class 'tuple'>

🧠 Concepts: