1.
Program using list
fruits=["Apple","Banana","Guava","Charry","Grape"]
print("First Fruit:",fruits[0])
print("Last Fruit:",fruits[-1])
#Modifying elements
fruits[1]="Blueberry"
print("Modified Fruits List:",fruits)
#Adding Elements
[Link]("Watermelon")
print("Fruits List after append:",fruits)
#Removing Elements
removedfruit=[Link](2)
print("removed Fruit:",removedfruit)
print(" Fruits List after removing:",fruits)
#length of the list
print ("Number of fruits",len(fruits))
#sorting fruits
[Link]()
print("Sorted fruits list:",fruits)
#reversing the list
[Link]()
print("Reversed fruit list:",fruits)
output:
First Fruit: Apple
Last Fruit: Grape
Modified Fruits List: ['Apple', 'Blueberry', 'Guava', 'Charry', 'Grape']
Fruits List after append: ['Apple', 'Blueberry', 'Guava', 'Charry', 'Grape', 'Watermelon']
removed Fruit: Guava
Fruits List after removing: ['Apple', 'Blueberry', 'Charry', 'Grape', 'Watermelon']
Number of fruits 5
Sorted fruits list: ['Apple', 'Blueberry', 'Charry', 'Grape', 'Watermelon']
Reversed fruit list: ['Watermelon', 'Grape', 'Charry', 'Blueberry', 'Apple']
[Link] using tuple
color=("Red","Green","Blue","Yellow")
#Accessing elements in the tuple
print("First color:",color[0])
print("Last color:",color[-1])
print("Slice of color:",color[1:3])
# Counting elements in the tuple
print("Number of colors:",len(color))
#checking if an element exits in the tuple
print("Is 'Red' in colors?","Red"in color)
print("Is 'Purple' in colors?","Purple"in color)
#concatenating tuples
color1=("orange","purple")
allcolors=color+color1
print("All colors:",allcolors)
#converting tuple to a list
colorlist=list(color)
print("Colors as a List",colorlist)
#converting list to a tuple
colortuple=tuple(colorlist)
print("Colors as a tuple",colortuple)
output
First color: Red
Last color: Yellow
Slice of color: ('Green', 'Blue')
Number of colors: 4
Is 'Red' in colors? True
Is 'Purple' in colors? False
All colors: ('Red', 'Green', 'Blue', 'Yellow', 'orange', 'purple')
Colors as a List ['Red', 'Green', 'Blue', 'Yellow']
Colors as a tuple ('Red', 'Green', 'Blue', 'Yellow')