Write a Python Program to Change the Value “banana” & “Cherry” with “Blackcurrant” & “Watermelon”
Introduction
In Python, lists are used to store multiple values in a single variable. We can modify or change elements in a list using their index position.
Logic to Write the Code
- Create a list of fruits.
- Find the index position of
"banana"and"Cherry". - Replace
"banana"with"Blackcurrant". - Replace
"Cherry"with"Watermelon". - Print the updated list.
Source Code (Beginner Friendly)
# Python program to replace banana and Cherry in the list
# Creating the list
Fruit = ["apple", "banana", "Cherry", "Mango", "Kiwi", "dragon"]
# Replacing the values
Fruit[1] = "Blackcurrant"
Fruit[2] = "Watermelon"
# Printing the updated list
print(Fruit)
Sample Output
['apple', 'Blackcurrant', 'Watermelon', 'Mango', 'Kiwi', 'dragon']
Viva Questions with Answers
1. What is a list in Python?
A list is a collection of items stored in a single variable.
2. How do you access elements in a list?
By using index numbers inside square brackets [ ].
3. What is the index of the first element in a list?
The first element has index 0.
4. Can we change values in a list?
Yes, lists are mutable, so their values can be changed.
5. What will happen if we use an index that does not exist?
It will give an IndexError.
