Python Editor

Showing posts with label Python | Lesson3 | Lists. Show all posts
Showing posts with label Python | Lesson3 | Lists. Show all posts

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

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)