Added code for lecture 2026-11-10
This commit is contained in:
parent
5b630435ae
commit
07b9e54bf0
52 changed files with 1248 additions and 1 deletions
77
lecture20261110/listmethods/list12MethodDemoMost.py
Normal file
77
lecture20261110/listmethods/list12MethodDemoMost.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
|
||||
|
||||
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}")
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue