What is anArray?
• typically referred to as a "list." A list is a versatile and widely used
data structure that allows you to store and manipulate a collection
of items. Lists in Python are ordered, mutable, and can contain
elements of different data types.
• In Python, an array is not a built-in data type like in some other
programming languages. Instead, Python provides a versatile and
powerful built-in data type called a "list" that can be used to
represent arrays. Lists in Python are similar to arrays in other
languages but have additional features that make them more
flexible.
3.
CREATION
- Lists canbe created using square brackets [ ].
Example:
python
my_list = [1, 2, 3, 'apple', 'banana']
4.
METHODS
Lists have severalbuilt-in methods for common
operations, such as append(), remove(),
pop(), extend(), sort(), and more.
5.
ACCESSING ELEMENTS
- Elementsin a list are accessed using indices.
- Indices start from 0 for the first element.
- Negative indices can be used to access elements
from the end.
Example:
first_element = my_list[0]
last_element = my_list[-1]
6.
SLICING
- Lists supportslicing to extract a portion of the
list.
- Example: sub_list = my_list[1:4] # Elements at
index 1, 2, and 3
7.
MODIFICATION
- Lists aremutable, meaning you can modify
elements by assigning new values.
- Example:
my_list[2] = 42
8.
LENGTH
- The len()function is used to get the length
(number of elements) of a list.
- Example:
length = len(my_list
9.
NESTED LISTS
- Listscan contain other lists, allowing for the
creation of nested structures.
- Example:
nested_list = [1, [2, 3], 'four']
10.
COMMON OPERATIONS
- Listssupport common operations like
concatenation (+), repetition (*), and membership
(in).
11.
ITERING THROUGH ALIST
- You can use loops to iterate through the elements of a list.
- Example:
for item in my_list:
print(item)
APPEND
Imagine you havea list like a shopping bag.
append(x) is like putting a new item
(x) into your bag at the end.
my_list = [1, 2, 3]
my_list.append(4)
Result: [1, 2, 3, 4]
14.
EXTEND
If you haveanother bag of items (an iterable),
extend(iterable) is like adding
all those items to your original bag.
my_list = [1, 2, 3]
my_list.extend([4, 5])
Result: [1, 2, 3, 4, 5]
15.
INSERT
If your bagis full and you want to add something
at a specific spot (index i),
insert(i, x) is like making room for that new item
my_list = [1, 2, 3]
my_list.insert(1, 10)
Result: [1, 10, 2, 3]
16.
REMOVE
If you wantto take a specific item (x) out of your
bag, remove(x) does just that
(but only the first occurrence)
my_list = [1, 2, 3, 2]
my_list.remove(2)
Result: [1, 3, 2]
17.
POP
If you wantto take an item out of your bag and also
know what it was, pop(i) does
that. If you don't specify i, it takes out the last item
my_list = [1, 2, 3]
popped_item = my_list.pop(1)
Result: popped_item = 2, my_list = [1, 3]
18.
INDEX
If you wantto find where a specific item (x) is in
your bag, index(x) tells
you its position
my_list = [1, 2, 3, 2]
index = my_list.index(2)
Result: index = 1
19.
COUNT
If you wantto know how many times a specific
item (x) is in your bag, count(x)
gives you that count
my_list = [1, 2, 3, 2]
count = my_list.count(2)
Result: count = 2
20.
SORT
(key=None, reverse=False)
If youwant to know how many times a specific
item (x) is in your bag, count(x)
gives you that count
my_list = [3, 1, 4, 1, 5, 9, 2]
my_list.sort()
Result: [1, 1, 2, 3, 4, 5, 9]
21.
REVERSE
If you wantto flip the order of items in your bag,
reverse() does that
my_list = [1, 2, 3]
my_list.reverse()
Result: [3, 2, 1]
22.
COPY
If you wantto make a duplicate of your bag (but
not share items with the original),
copy() does that.
my_list = [1, 2, 3]
copied_list = my_list.copy()
Result: copied_list = [1, 2, 3]