A list is a collection of values, similar to an array, and can store multiple items in a single variable.
marks = [92.1, 99.2, 77.4, 22]
print(marks) # Output: [92.1, 99.2, 77.4, 22]
print(type(marks)) # Output: <class 'list'>Lists are indexed, and elements can be accessed using the index.
student = ["noushad", 88, 12, "dhaka"]
print(student[0]) # Output: "noushad"Lists are mutable, meaning you can change their elements.
student[0] = "hasan"
print(student) # Output: ['hasan', 88, 12, 'dhaka']You can extract parts of a list using slicing.
marks = [99, 12, 42, 44.5, 99.1]
print(marks[1:4]) # Output: [12, 42, 44.5]
print(marks[1:]) # Output: [12, 42, 44.5, 99.1]
print(marks[:5]) # Output: [99, 12, 42, 44.5, 99.1]
print(marks[-4:-1]) # Output: [12, 42, 44.5]
print(marks[-3:]) # Output: [42, 44.5, 99.1]
print(marks[:-2]) # Output: [99, 12, 42]Adds an element to the end of the list.
list = [2, 1, 4, 2, 1]
list.append(99)
print(list) # Output: [2, 1, 4, 2, 1, 99]Sorts the list in ascending order.
list.sort()
print(list) # Output: [1, 1, 2, 2, 4, 99]Sorts the list in descending order.
list.sort(reverse=True)
print(list) # Output: [99, 4, 2, 2, 1, 1]Inserts an element at a specific index.
list.insert(2, 11)
print(list) # Output: [2, 1, 11, 4, 2, 1, 99]Removes the first occurrence of a value.
list.remove(2)
print(list) # Output: [1, 11, 4, 2, 1, 99]Removes an element at a specific index.
list.pop(2)
print(list) # Output: [1, 11, 2, 1, 99]Strings are immutable (cannot be changed), while lists are mutable (can be changed).