Python Assignment: Lists and Sets
Part 1: Lists
Creating Lists:
Create a list named fruits that contains the following items: "Apple", "Banana", "Cherry", "Apple".
Print the list and its length.
Accessing List Items:
Access and print the second item in the fruits list.
Access and print the last item using negative indexing.
Access and print the first two items using slicing.
Modifying Lists:
Change the second item in the fruits list to "Blueberry".
Add "Orange" to the end of the list using the append() method.
Insert "Mango" at the beginning of the list using the insert() method.
Remove "Cherry" from the list using the remove() method.
Remove the last item from the list using the pop() method.
List Methods:
Use the count() method to find how many times "Apple" appears in the fruits list.
Sort the fruits list in alphabetical order using the sort() method.
Reverse the order of the fruits list using the reverse() method.
List Comprehension:
Create a new list named long_fruits that contains only the fruits with more than 5 characters using list comprehension.
Part 2: Sets
Creating Sets:
Create a set named colors that contains the following items: "Red", "Blue", "Green", "Red".
Print the set and its length.
Accessing Set Items:
Check if "Blue" is in the colors set and print the result.
Loop through the colors set and print each item.
Modifying Sets:
Add "Yellow" to the colors set using the add() method.
Remove "Green" from the set using the remove() method.
Try removing "Purple" using the discard() method and explain the difference between remove() and discard().
Set Methods:
Create another set named new_colors with items: "Blue", "Purple", "Orange".
Use the union() method to combine colors and new_colors and print the result.
Use the difference() method to find items that are in colors but not in new_colors and print the result.
Use the symmetric_difference() method to find items that are in either colors or new_colors but not in both and print the result.
Use the intersection() method to find items that are common to both colors and new_colors and print the result.