Unit - 2 (5m)
Unit - 2 (5m)
Unit - 2 (5m)
Unit -2 (5m):
Explain the different list methods with an example.
1 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
These are just some of the commonly used list methods in Python.
There are many other list methods available that can be used to
manipulate and transform lists according to your specific needs.
Explain the ways of indexing and slicing the list with examples.
Indexing and slicing are powerful features of lists in Python that allow
you to access and manipulate specific elements in a list. Here are the
ways of indexing and slicing a list with examples:
1. Indexing a list:
2 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
You can access an element in a list using its index. The first element in
a list has an index of 0, the second element has an index of 1, and so
on. Here's an example:
1. Slicing a list:
You can extract a portion of a list by slicing it. To slice a list, you need
to specify the start index and end index of the portion you want to
extract. The start index is inclusive, while the end index is exclusive.
Here's an example:
You can also use negative indices to index and slice a list from the
end. For example:
3 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
the slice excludes the element with index -1, which is "elderberry".
1. dict() : This function creates a new dictionary. You can pass key-
value pairs as arguments to create a dictionary, or you can pass a
sequence of key-value pairs as an argument to create a dictionary.
Example:
4 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
Tuples are similar to lists in Python, but they are immutable, meaning
their values cannot be modified. Here are some of the commonly
used tuple methods in Python:
5 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
6 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
7 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
In Python, lists and tuples are both used to store collections of values.
However, there are some key differences between the two data
structures:
1. Mutability: Lists are mutable, which means that their contents can
be changed after they are created, whereas tuples are immutable,
which means that their contents cannot be changed once they are
created.
Note that in the second example, we had to create a new tuple with
the updated contents instead of modifying the original tuple directly.
1. Indexing: Both lists and tuples allow you to access their elements
by index, using the same syntax: my_list[index] or
my_tuple[index] .
8 of 17 12-12-2023, 19:10
Unit -2 (5m): https://awaisahmed.notion.site/Unit-2-5m-9646c91f2a0143b68ea12f54...
2. Iteration: You can iterate over both lists and tuples using a for
loop.
3. Length: Both lists and tuples have a len() function that returns
the number of elements in the collection.
9 of 17 12-12-2023, 19:10