Added code for lecture 2026-11-10

This commit is contained in:
Edward Bigos 2026-11-10 12:28:09 -05:00
commit 07b9e54bf0
52 changed files with 1248 additions and 1 deletions

View file

@ -0,0 +1,13 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
This program appends a value to a list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14]
print(f"Before append {basicList}")
basicList.append(104)
print(f"After append {basicList}")

View file

@ -0,0 +1,15 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
This program clears all values from a list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14]
print(f"Before clear {basicList}")
basicList.clear()
print(f"After clear {basicList}")

View file

@ -0,0 +1,31 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
A shallow copy is a new name for an existing object. This copy demonstrates
this principle.
1.) Create a list.
2.) Create a new name and set it equal to the original list.
3.) Show the id's of the objects are the same.
4.) Append a value to one then show the other name shows the same list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14]
print(f"Before copy {basicList}")
myList = basicList
print(f"After copy {basicList} ==> {myList}")
print(f"After copy {id(basicList)} <= Same ID => {id(myList)}")
print("\n")
print("Append a value to basicList. Examine the change to myList\n")
#basicList = [1, 34, 99, 87, 14]
myList = basicList
print(f"Before append to basicList {basicList}")
basicList.append(777)
print(f"basicList - After append {basicList}")
print(f"myList - After append {myList}")
print(f"After append {id(basicList)} <= Same ID => {id(myList)}")

View file

@ -0,0 +1,33 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
A deep copy is a new copy of an existing object. This copy demonstrates
this principle.
1.) Create a list.
2.) Create a new copy from the original list.
3.) Show the id's of the objects are the different.
4.) Append a value to basicList then show the copy in myList has not changed.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14]
print(f"Before copy {basicList}")
myList = basicList.copy()
print(f"After copy {basicList} ==> {myList}")
print(f"After copy {id(basicList)} <= Not Equal => {id(myList)}")
print("\n")
print("Append a value to basicList. Examine any changes to myList\n")
#basicList = [1, 34, 99, 87, 14]
#myList = basicList.copy()
print(f"Before append to basicList {basicList}")
basicList.append(777)
print(f"basicList - After append {basicList}")
print(f"myList - After append {myList}")
print(f"After append {id(basicList)} <= Not Equal => {id(myList)}")

View file

@ -0,0 +1,33 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
The count() method counts the number of elements in a list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14, 1, 34, 1,87 ]
print(f"Example list {basicList}")
myValue = 1
counter = basicList.count(myValue)
print(f"The number of '{myValue}' items is {counter}")
myValue = 34
counter = basicList.count(myValue)
print(f"The number of '{myValue}' items is {counter}")
myValue = 87
counter = basicList.count(myValue)
print(f"The number of '{myValue}' items is {counter}")
myValue = 314
counter = basicList.count(myValue)
print(f"The number of '{myValue}' items is {counter}")

View file

@ -0,0 +1,26 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
The extend() method appends one list to another list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14]
print(f"basicList = {basicList}")
myList = [111, 399, 1234]
print(f"myList = {myList}")
print("\nExtending basicList with myList\n")
basicList.extend(myList)
print(f"basicList = {basicList}")
print(f"myList = {myList}")
print("\n")

View file

@ -0,0 +1,38 @@
# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
The index() method locates the first element in a list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14, 1, 34, 1,87 ]
print(f"Example list {basicList}")
myValue = 1
index = basicList.index(myValue)
print(f"The location of '{myValue}' item is at position {index}")
myValue = 1
index = basicList.index(myValue,index+1)
print(f"The next location of '{myValue}' item is at position {index}")
myValue = 1
index = basicList.index(myValue,index+1)
print(f"The next location of '{myValue}' item is at position {index}")
print("This last search generates an error.")
myValue = 1
index = basicList.index(myValue,index+1)
print(f"The next location of '{myValue}' item is at position {index}")

View file

@ -0,0 +1,51 @@
#!/usr/bin/python3
# Program: list04
programMessage = '''
This program inserts a value into a list.
'''
print(programMessage)
# Lists of text values
#dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
dogNames = [ "Harry","Katrina","Tanner", "Colorado","Mandy"]
# Print the list and the length
print("Print the length and type of the list.")
print (dogNames," length = ",len(dogNames),"Type =",type(dogNames))
print("\nPrint each element of the list and it's index")
index = 0
for item in dogNames:
print(f"dogNames[{index}] = {item}")
index = index + 1
# Insert a new dog at position 1. Lists begin indexing at 0
newDogName = "Shenanigans"
print("Insert at position 1")
dogNames.insert(1,newDogName)
print(f"The modified list {dogNames}")
print(f"\nInsert a new dog at position 4. Lists begin indexing at 0.")
newDogName = "Yukon Jack"
print("Insert at position 4 after Tanner")
dogNames.insert(4,newDogName)
print(f"The modified list {dogNames}")
print(f"\nLocate a dog name, then insert a new dog after the dog we located.")
message1 ='''
Note: This program is created so the search succeeds.
If the search fails the program will generate an error and stop. This can be
remedied with a try - except block.
'''
print(message1)
searchDog = "Katrina"
newDogName = "Gimli"
print(f"Locate the dog named {searchDog}")
searchDogIndex = dogNames.index(searchDog)
print(f"Found dog {searchDog} at index {searchDogIndex}")
print(f"Insert new dog name = {newDogName} before {searchDog}")
dogNames.insert(searchDogIndex,newDogName)
print(f"The new list = {dogNames}")

View file

@ -0,0 +1,40 @@
#!/usr/bin/python3
# Program: list09MethodPop.py
programMessage = '''
This program popss a value from a list. The value is removed from
the end of the list or at the position specified by an optional index.
'''
print(programMessage)
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Print the list and the length
print("Print the length and type of the list.")
print (dogNames," length = ",len(dogNames),"Type =",type(dogNames))
print("\nPrint each element of the list and it's index")
index = 0
for item in dogNames:
print(f"dogNames[{index}] = {item}")
index = index + 1
print(f"Pop last element")
dog = dogNames.pop()
print(f"Removed {dog} from {dogNames}")
print(f"Pop last element")
dog = dogNames.pop()
print(f"Removed {dog} from {dogNames}")
print(f"Pop last element")
dog = dogNames.pop()
print(f"Removed {dog} from {dogNames}")
print(f"Pop element at position 1")
dog = dogNames.pop(1)
print(f"Removed {dog} from {dogNames}")

View file

@ -0,0 +1,24 @@
#!/usr/bin/python3
# Program: list09MethodReverse.py
programMessage = '''
This program reverses a list.
'''
print(programMessage)
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Print the list and the length
print("Print the length and type of the list.")
print (dogNames," length = ",len(dogNames),"Type =",type(dogNames))
print("\nReverse the list")
dogNames.reverse()
print("Print the length of the reversed list.")
print (dogNames)

View file

@ -0,0 +1,24 @@
#!/usr/bin/python3
# Program: list11MethodSort.py
programMessage = '''
This program sorts a list.
'''
print(programMessage)
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Print the list and the length
print("Print the length and type of the list.")
print (dogNames," length = ",len(dogNames),"Type =",type(dogNames))
print("\nSort the list")
dogNames.sort()
print("Print the length of the sorted list.")
print (dogNames)

View 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}")