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
13
lecture20261110/listmethods/list01MethodAppend.py
Normal file
13
lecture20261110/listmethods/list01MethodAppend.py
Normal 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}")
|
||||
|
||||
15
lecture20261110/listmethods/list02MethodClear.py
Normal file
15
lecture20261110/listmethods/list02MethodClear.py
Normal 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}")
|
||||
|
||||
31
lecture20261110/listmethods/list03MethodShallowCopy.py
Normal file
31
lecture20261110/listmethods/list03MethodShallowCopy.py
Normal 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)}")
|
||||
|
||||
|
||||
33
lecture20261110/listmethods/list04MethodDeepCopy.py
Normal file
33
lecture20261110/listmethods/list04MethodDeepCopy.py
Normal 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)}")
|
||||
|
||||
|
||||
|
||||
33
lecture20261110/listmethods/list05MethodCount.py
Normal file
33
lecture20261110/listmethods/list05MethodCount.py
Normal 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}")
|
||||
|
||||
|
||||
|
||||
26
lecture20261110/listmethods/list06MethodExtend.py
Normal file
26
lecture20261110/listmethods/list06MethodExtend.py
Normal 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")
|
||||
38
lecture20261110/listmethods/list07MethodIndex.py
Normal file
38
lecture20261110/listmethods/list07MethodIndex.py
Normal 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}")
|
||||
|
||||
|
||||
51
lecture20261110/listmethods/list08MethodInsert.py
Normal file
51
lecture20261110/listmethods/list08MethodInsert.py
Normal 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}")
|
||||
|
||||
40
lecture20261110/listmethods/list09MethodPop.py
Normal file
40
lecture20261110/listmethods/list09MethodPop.py
Normal 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}")
|
||||
|
||||
24
lecture20261110/listmethods/list10MethodReverse.py
Normal file
24
lecture20261110/listmethods/list10MethodReverse.py
Normal 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)
|
||||
|
||||
|
||||
24
lecture20261110/listmethods/list11MethodSort.py
Normal file
24
lecture20261110/listmethods/list11MethodSort.py
Normal 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)
|
||||
|
||||
|
||||
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