Python Editor

Python | Lesson 3 | Lists | Append, Remove, Len

 

# Create a list
fruitsList = ["apple", "banana", "orange"]

# Index elements in a list
print(fruitsList[0]) # Output: "apple"
print(fruitsList[1]) # Output: "banana"

# Modifying elements in a list
fruitsList[1] = "mango"
print(fruitsList) # Output: ["apple", "mango", "orange"]

# Adding elements to a list
fruitsList.append("kiwi")
print(fruitsList) # Output: ["apple", "mango", "orange", "kiwi"]

# Removing elements from a list
fruitsList.remove("orange")
print(fruitsList) # Output: ["apple", "mango", "kiwi"]

# Finding the length of a list
print(len(fruitsList)) # Output: 3

# Loop through a list
for fruit in fruitsList:
    print(fruit)
# Output:
# apple
# mango
# kiwi

No comments:

Post a Comment