Python Ppt
Python Ppt
LIST
1. Lists are ordered collection of data items.
2. They store multiple items in a single variable.
3. List items are separated by commas and enclosed within square brackets [ ].
4. Lists are mutable (changeable), meaning we can alter them after creation.
5. Example:
list = ["Red", "Green", "Blue"]
print(list)
Output:
['Red', 'Green', 'Blue’]
List Index
Each item/element in a list has its own unique index. This index can be used to access any particular item from the list.
Example: colors = ["Red", "Green", "Blue", "Yellow", "Green"] # [0] [1] [2] [3] [4]
Positive Indexing:
Example: colors = ["Red", "Green", "Blue", "Yellow", "Green"]
# [0] [1] [2] [3] [4]
print(colors[2])
Output: Blue
Negative Indexing:
Example: colors = ["Red", "Green", "Blue", "Yellow", "Green"] PRESENTED BY :
# [-5] [-4] [-3] [-2] [-1] MEENAKSHI
print(colors[-1]) ROLL NO. : 233211
Output: Green BRANCH : CSE
SECTION : A
SEMESTER : 3rd
SUBJECT :
append(): Adds an item to the end of the list.
Example:
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
# Output: [1, 2, 3, 4]
extend(): Adds items of lists and other iterables to the end of the list.
Example:
color1 = ["violet", "indigo", "blue"]
color2 = ["green", "yellow", "orange", "red"]
colors.extend(color2)
print(colors)
Output: ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red’]