Lists and Tuples
Lists:
1. Introduction to Lists:
- Understanding lists as ordered collections.
- Lists' flexibility in holding different types of data.
2. Creating Lists:
- Defining lists using square brackets ([]).
- Adding elements separated by commas.
3. Accessing List Elements:
- Indexing: Accessing elements by their position.
- Negative indexing for counting from the end.
4. Slicing Lists:
- Extracting sublists using slicing notation.
- Specifying start, stop, and step values.
5. Modifying Lists:
- Changing elements using indexing.
- Adding elements using .append() and .insert().
6. List Methods:
- Using .append() to add elements to the end.
- Using .insert() to add elements at a specific index.
- Using .remove() to remove elements by value.
- Using .pop() to remove elements by index and retrieve the value.
- Using .index() to find the index of an element.
7. List Comprehensions:
- Creating concise lists using list comprehensions.
- Applying expressions to each element in a list.
8. Sorting Lists:
- Using .sort() to sort a list in place.
- Using sorted() to create a sorted copy.
9. Reversing Lists:
- Using .reverse() to reverse the order of elements.
- Using slicing to achieve the same effect.
10. Length and Membership:
- Finding the length of a list using len().
- Checking for membership using in and not in.
Tuples:
1. Introduction to Tuples:
- Understanding tuples as ordered, immutable collections.
- Use cases for tuples: fixed data, returning multiple values.
2. Creating Tuples:
- Defining tuples using parentheses (()).
- Creating single-element tuples with a trailing comma.
3. Accessing Tuple Elements:
- Indexing and negative indexing as with lists.
- Read-only nature of tuple elements.
4. Slicing Tuples:
- Extracting sub-tuples using slicing notation.
- Similar to list slicing.
5. Tuple Packing and Unpacking:
- Packing: Creating a tuple by grouping values.
- Unpacking: Assigning tuple elements to variables.
6. Iterating Over Tuples:
- Using loops to iterate over tuple elements.
- Combining with tuple unpacking.
7. Tuples as Return Values:
- Using tuples to return multiple values from functions.
- Unpacking returned tuples to access individual values.
8. Named Tuples:
- Creating named tuples using collections.namedtuple().
- Giving names to tuple fields for better readability.
9. Comparing Lists and Tuples:
- Differences in mutability and use cases.
- Choosing between lists and tuples based on requirements.
10. Converting Between Lists and Tuples:
- Using list() and tuple() functions for conversion.
- Adapting data structures as needed.