77 lines
1.9 KiB
Python
77 lines
1.9 KiB
Python
|
|
|
|
programMessage = '''
|
|
This program demonstrates many of the methods in a single program.
|
|
|
|
'''
|
|
print(programMessage)
|
|
|
|
# Create some lists
|
|
cars01 = ["Tundra", "Accord", "Ridgeline", "Stingray"]
|
|
moreCars = ["Elantra", "Tacoma", "Silverado", "Denali"]
|
|
|
|
print(f"cars01 = {cars01}")
|
|
print(f"morecars = {moreCars}")
|
|
print("\n")
|
|
|
|
# demonstrate append
|
|
newCar = "f150"
|
|
print(f"Append {newCar} to cars01")
|
|
cars01.append(newCar)
|
|
print(f"cars01 = {cars01}\n")
|
|
|
|
# demonstrate count
|
|
targetCar = "f150"
|
|
print(f"count values of {targetCar} in cars01 = {cars01.count(targetCar)}\n")
|
|
|
|
targetCar = "Corvette"
|
|
print(f"count values of {targetCar} in cars01 = {cars01.count(targetCar)}\n")
|
|
|
|
print("Demonstrate extend the list\n== Original lists:" )
|
|
|
|
cars01 = ["Tundra", "Accord", "Ridgeline", "Stingray"]
|
|
moreCars = ["Elantra", "Tacoma", "Silverado", "Denali"]
|
|
print(f"cars01 = {cars01}")
|
|
print(f"morecars = {moreCars}")
|
|
|
|
print("== Extend cars01 with moreCars")
|
|
cars01.extend(moreCars)
|
|
print(f"cars01 = {cars01}")
|
|
print(f"morecars = {moreCars}")
|
|
|
|
# Restore the original lists
|
|
cars01 = ["Tundra", "Accord", "Ridgeline", "Stingray"]
|
|
moreCars = ["Elantra", "Tacoma", "Silverado", "Denali"]
|
|
|
|
# demonstrate index
|
|
print("\nDemonstrate the Index (find) method")
|
|
|
|
print(f"cars01 = {cars01}")
|
|
myValue = "Accord"
|
|
index = cars01.index(myValue)
|
|
print(f"The location of '{myValue}' item is at position {index}")
|
|
|
|
# Uncomment the following lines below to demonstrate a failure
|
|
#myValue = "Tacoma"
|
|
#index = cars01.index(myValue)
|
|
#print(f"The location of '{myValue}' item is at position {index}")
|
|
|
|
print("\n")
|
|
|
|
# Demonstrate sort
|
|
cars01 = ["Tundra", "Accord", "Ridgeline", "Stingray"]
|
|
print(f"cars01 = {cars01}")
|
|
|
|
cars01.sort()
|
|
print(f"Sorted cars01 = {cars01}")
|
|
print("\n")
|
|
|
|
# Demonstrate reverse
|
|
cars01 = ["Tundra", "Accord", "Ridgeline", "Stingray"]
|
|
print(f"cars01 = {cars01}")
|
|
|
|
cars01.reverse()
|
|
print(f"Reversed cars01 = {cars01}")
|
|
|
|
|
|
|