In this project, we will discuss a Python code snippet that demonstrates various operations on a list of fruits. We'll explain each step of the code and provide the final working code at the end.
Creating and Displaying the List
To begin, we create a list called fruits
containing three fruit names: "Apple", "Banana", and "Pear". We then display the list using the print()
function:
fruits = ["Apple", "Banana", "Pear"]
print(fruits)
This code creates the list fruits
and prints its contents.
Accessing Elements of the List
We can access individual elements of the list by using their index. In this code, we access the first element of the list and print it:
print("First fruit:", fruits[0])
The index 0
corresponds to the first element of the list. We use this index to access and print the first fruit name.
Modifying the List
We can modify the list by adding, replacing, or removing elements. In the following code, we append the fruit "Plum" to the end of the list, replace the second element with "Grapes," and print the modified list:
fruits.append("Plum")
print("Last fruit:", fruits[len(fruits)-1])
fruits[1] = "Grapes"
print(fruits)
The append()
function adds "Plum" to the end of the list. We use len(fruits)-1
to access the last element and print it.
The line fruits[1] = "Grapes"
replaces the second element, which is "Banana," with "Grapes." We then print the modified list.
Searching for an Element
We can search for a specific element in the list using the in
operator. In this code, we check if "Apple" exists in the list and print the corresponding message:
if "Apple" in fruits:
print("Apple is present.")
else:
print("Apple is not present.")
If "Apple" is found in the list, we print "Apple is present." Otherwise, we print "Apple is not present."
Obtaining the Length of the List
We can determine the length of the list using the len()
function. In the following code, we calculate the length of the list and print it:
print("The list contains:", len(fruits), "fruits.")
The len(fruits)
expression calculates the number of elements in the list, and we print the result.
Removing an Element from the List
We can remove an element from the list using the remove()
function. In this code, we remove the element "Pear" from the list and print the modified list:
fruits.remove("Pear")
print(fruits)
The remove("Pear")
function removes the element "Pear" from the list. We then print the modified list.
Reversing and Sorting the List
We can reverse and sort the elements of the list using the reverse()
and sort()
functions, respectively. In the following code, we reverse and sort the list, and then print the result:
fruits.reverse()
fruits.sort()
print
(fruits)
The reverse()
function reverses the order of the elements in the list. We then use the sort()
function to sort the elements in ascending order. Finally, we print the sorted list.
Final Code
Here's the complete Python code that manipulates a list of fruits:
fruits = ["Apple", "Banana", "Pear"]
print(fruits)
print("First fruit:", fruits[0])
fruits.append("Plum")
print("Last fruit:", fruits[len(fruits)-1])
fruits[1] = "Grapes"
print(fruits)
if "Apple" in fruits:
print("Apple is present.")
else:
print("Apple is not present.")
print("The list contains:", len(fruits), "fruits.")
fruits.remove("Pear")
print(fruits)
fruits.reverse()
fruits.sort()
print(fruits)
That's it! You now have a Python code snippet that demonstrates various operations on a list of fruits. Feel free to modify the code or use it as a starting point for your own projects. Happy coding!