Python Editor

Python | Lesson3 | Lists

#List are ordered, modifyable, and allow duplicate values.
fruits = ["banana", "banana", "cherry", "apple", "cherry"]
print(fruits)

# Find the length of the list
print(len(fruits))

# List with different data types

team=["Team1",5,"A",True]

print(team)

2 comments:

  1. # Create a list
    fruitsList = ["apple", "banana", "cherry", "coconut", "grape", "kiwi", "lemon", "orange","peach", "pear"]

    # 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

    ReplyDelete
  2. # Create a list
    fruitsList = ["apple", "banana", "cherry", "coconut", "grape", "kiwi", "lemon", "orange","peach", "pear",
    "pineapple", "raspberry", "strawberry", "watermelon"]

    # 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

    ReplyDelete