Added lecture 2026113

This commit is contained in:
Edward Bigos 2026-11-03 12:49:45 -05:00
commit 5b630435ae
27 changed files with 738 additions and 0 deletions

View file

@ -0,0 +1,15 @@
# dogs-lists08
dogs = ["Mandy", "Harry", "Shenanigans", "Colorado", "Katrina", "Rusty" ]
# Poor method to print
print(dogs)
# Print the list
positionId = 0
for dog in dogs:
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")

View file

@ -0,0 +1,50 @@
# dogs-lists09
#Dog Description List format = [<dogname>,<age>,<sex>, <breed>]
dogs = [["Mandy", 2, "F", "Chinook"], ["Harry",11,"M", "Chinook"], ["Shenanigans",8,"F", "Chinook"],
["Colorado",10,"F", "Chinook"], ["Katrina", 9, "F", "Chinook"], ["Rusty",10.5,"M", "Golden Retriever" ],
["Tanner", 16, "M", "Golden Retriever"],["Gimli", 2, "M", "Chinook"] ]
# Poor method to print
print(dogs)
# Print the list
positionId = 0
for dog in dogs:
print("{} {}".format(positionId,dog))
positionId += 1
# Print males in the list
print("\n - These are the male dogs")
positionId = 0
for dog in dogs:
if dog[2] == "M":
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")
# Print females in the list
print("\n - These are the female dogs")
positionId = 0
for dog in dogs:
if dog[2] == "F":
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")
# Print ages in the list
print("\n - These are the dogs ages")
positionId = 0
for dog in dogs:
if dog[1] <= 11:
print("{} {}, age = {}".format(positionId,dog,dog[1]))
positionId += 1
print("\n")

View file

@ -0,0 +1,55 @@
# dogs-lists10
#Example using lambda
#Dog Description List format = [<dogname>,<age>,<sex>, <breed>]
dogs = [["Mandy", 2, "F", "Chinook"], ["Harry",11,"M", "Chinook"], ["Shenanigans",8,"F", "Chinook"],
["Colorado",10,"F", "Chinook"], ["Katrina", 9, "F", "Chinook"], ["Rusty",10.5,"M", "Golden Retriever" ],
["Tanner", 16, "M", "Golden Retriever"] ]
# Poor method to print list
print(dogs)
# Print the list & position
positionId = 0
for dog in dogs:
print("{} {}".format(positionId,dog))
positionId += 1
# Print males in the list
print("\n - These are the male dogs")
positionId = 0
for dog in dogs:
if dog[2] == "M":
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")
# Print females in the list
print("\n - These are the female dogs")
positionId = 0
for dog in dogs:
if dog[2] == "F":
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")
print("Sort by sex")
print(sorted(dogs, key=lambda x: x[2]))
print("Sort by name")
print(sorted(dogs, key=lambda x: x[0]))
print("Sort by age")
print(sorted(dogs, key=lambda x: x[1]))
print("Sort by breed")
print(sorted(dogs, key=lambda x: x[3]))
print("\n")
print(dogs)

View file

@ -0,0 +1,57 @@
# dogs-lists10a
#Example using itemgetter
from operator import itemgetter
#Dog Description List format = [<dogname>,<age>,<sex>, <breed>]
dogs = [["Mandy", 2, "F", "Chinook"], ["Harry",11,"M", "Chinook"], ["Shenanigans",8,"F", "Chinook"],
["Colorado",10,"F", "Chinook"], ["Katrina", 9, "F", "Chinook"], ["Rusty",10.5,"M", "Golden Retriever" ],
["Tanner", 16, "M", "Golden Retriever"] ]
# Poor method to print list
print(dogs)
# Print the list & position
positionId = 0
for dog in dogs:
print("{} {}".format(positionId,dog))
positionId += 1
# Print males in the list
print("\n - These are the male dogs")
positionId = 0
for dog in dogs:
if dog[2] == "M":
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")
# Print females in the list
print("\n - These are the female dogs")
positionId = 0
for dog in dogs:
if dog[2] == "F":
print("{} {}".format(positionId,dog))
positionId += 1
print("\n")
print("Sort by name")
print(sorted(dogs, key=itemgetter(0)))
print("Sort by sex")
print(sorted(dogs, key=itemgetter(2)))
print("Sort by breed")
print(sorted(dogs, key=itemgetter(3)))
print("Sort by age")
print(sorted(dogs, key=itemgetter(1)))
#z = sorted(dogs, key=itemgetter(1))
#print(type(z),z)

View file

@ -0,0 +1,19 @@
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
primeList = [1, 2, 3, 7, 11, 13, 17]
print(dogNames)
print(primeList)
print("Dog Names")
for aDogName in dogNames:
print(aDogName)
print("\n")
print("Prime Numbers")
for aPrimeNumber in primeList:
print(aPrimeNumber)
print("\n")

View file

@ -0,0 +1,29 @@
import math
#Integers
someInteger = 3
someInteger2 = 456
#Floats
someFloat = 7.
someFloat2 = 78.9
dollarAmount = 696.05
piFloat = math.pi
#Basic Print
print("Basic Print")
print(someInteger, someFloat2)
print(someInteger2, piFloat)
# formatted Print without print spec
print("Formatted Print without print spec")
print(f"{someInteger} {someFloat2}")
print(f"{someInteger2} {piFloat}")
# formatted Print with print spec
# Note - 8.6f means format a float with a minimum
# of 8 spaces wide and 6 places to the right of the decimal point.
print("Formatted Print with print spec")
print(f"{someInteger:5d} {someFloat2:6.2f}")
print(f"{someInteger2:5d} {piFloat:8.6f}")

View file

@ -0,0 +1,41 @@
#!/usr/bin/python3
# Defining and printing list lengths.
# Lists of text values
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
firstNames = ["edward","david", "Bob","mary","alice"]
txtList = ["7", "2", "12", "18", "16", "21"]
# Lists of nubers. These could be integers or floats.
intList = [7, 2, 12, 18, 16, 21]
primeList = [1, 2, 3, 7, 11, 13, 17, 19]
# Print the entire list
print("Print the entire list.")
print (dogNames)
print(firstNames)
print(txtList)
print(intList)
print(primeList)
print("\n")
# Print the length of the list
print("Print the length of the lists.")
print( len(dogNames) )
print(len(firstNames) )
print(len(txtList) )
print(len(intList) )
print(len(primeList) )
print("\n")
# Print the list and the length
print("Print the length of the lists.")
print (dogNames," length = ",len(dogNames))
print(firstNames," length = ",len(firstNames))
print(txtList," length = ",len(txtList))
print(intList," length = ",len(intList))
print(primeList," length = ",len(primeList))

View file

@ -0,0 +1,13 @@
# Create a simple list
list1 = ["Mary","Harry","Larry","Moe","Alice", "Katrina"]
print("The length of list 1 is ",len(list1))
print( list1[1:4])
print( "list1[0] =", list1[0])
print( "list1[1] =", list1[1])
print( "list1[2] =", list1[2])
print( "list1[3] =", list1[3])

View file

@ -0,0 +1,71 @@
#!/usr/bin/python3
# Accessing List elements.
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
firstNames = ["edward","david", "Bob","mary","alice"]
txtList = ["7", "2", "12", "18", "16", "21"]
intList = [7, 2, 12, 18, 16, 21]
primeList = [1, 2, 3, 7, 11, 13, 17, 19]
# Same list print as in the first program.
print (dogNames," length = ",len(dogNames))
print(firstNames," length = ",len(firstNames))
print(intList," length = ",len(intList))
print(primeList," length = ",len(primeList))
print(txtList," length = ",len(txtList))
# Access each element in a list.
# The index starts at 0
# If you uncomment '#print(dogNames[7])' the program will
# generate an error. There is no element #7
print (dogNames," length = ",len(dogNames))
print("\nOutput for each element index.")
print(dogNames[0])
print(dogNames[1])
print(dogNames[2])
print(dogNames[3])
print(dogNames[4])
print(dogNames[5])
print(dogNames[6])
#print(dogNames[7])
# Access elements from the end of the list. Use
# negative numbers to access the values starting at the end of the list.
print (dogNames," length = ",len(dogNames))
print("\nOutput for each negative element index.")
print(dogNames[-1])
print(dogNames[-2])
print(dogNames[-3])
print(dogNames[-4])
print(dogNames[-5])
print(dogNames[-6])
print(dogNames[-7])
#print(dogNames[-8])
# We are getting ahead of ourselves, but we can examine the for loop
# to print the elements of the list. This loop determines the length
# of the list and processes every element.
# Starting with the first element in the list each element in the list will
# be assigned to aDogName. You ca do anything you like with this one element.
print("\n")
print (dogNames," length = ",len(dogNames))
print("\nOutput from the for loop")
for aDogName in dogNames:
print(aDogName)
print("\n")
# Slices aloow one to access a group of elements from the list.
print (dogNames," length = ",len(dogNames))
print("\nPrint list slices. Notice the slice is a new list.")
# Print elements 1 through element 4
print(dogNames[1:4])
# Print elements 4 through the end of the list
print(dogNames[4:])
# Print elements from the start of the list through element 4
print(dogNames[:4])

View file

@ -0,0 +1,58 @@
#!/usr/bin/python3
# List element manipulation.
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
dogNames2 = ["Oscar", "Rusty"]
print (dogNames," length = ",len(dogNames))
print("Append element Rusty to the list.")
dogNames.append("Rusty")
print (dogNames," length = ",len(dogNames))
print("\n")
# Reset dogNames back to the original list
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Insert a value into the list
print("Insert a list into position #2")
print (dogNames," length = ",len(dogNames))
print (dogNames2," length = ",len(dogNames2))
dogNames.insert(2,dogNames2)
print (dogNames," length = ",len(dogNames))
print("\n")
# Reset dogNames back to the original list
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Insert a list into the list
print("Append a list at the end")
print (dogNames," length = ",len(dogNames))
print (dogNames2," length = ",len(dogNames2))
dogNames.append(dogNames2)
print (dogNames," length = ",len(dogNames))
print("\n")
# Reset dogNames back to the original list
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Insert a list into the list
print("Extend a list at the end. This expands the list at the end of the first list.")
print (dogNames," length = ",len(dogNames))
print (dogNames2," length = ",len(dogNames2))
dogNames.extend(dogNames2)
print (dogNames," length = ",len(dogNames))
print("\n")
# Reset dogNames back to the original list
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Insert a list into the list
print("Remove the value katrina from the list. ")
print (dogNames," length = ",len(dogNames))
dogNames.remove("katrina")
print (dogNames," length = ",len(dogNames))
print("\n")

View file

@ -0,0 +1,34 @@
#!/usr/bin/python3
# List operating as a stack.
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
dogNames2 = ["Oscar", "Rusty"]
firstNames = ["edward","david", "Bob","mary","alice"]
intList = [7, 2, 12, 18, 16, 21]
primeList = [1, 2, 3, 7, 11, 13, 17, 19]
txtList = ["7", "2", "12", "18", "16", "21"]
print("pop() can be used to implement a stack. A stack is a last in first out")
print(" (LIFO) structure used in some program logic.")
print (dogNames," length = ",len(dogNames))
dName = dogNames.pop()
print(f"Popped {dName:s}")
print (dogNames," length = ",len(dogNames))
dName = dogNames.pop()
print(f"Popped {dName:s}")
print (dogNames," length = ",len(dogNames))
dName = dogNames.pop()
print(f"Popped {dName:s}")
print (dogNames," length = ",len(dogNames))
print("\n\n")
print("There is no push but we can append")
dogNames.append("Colorado")
print (dogNames," length = ",len(dogNames))

View file

@ -0,0 +1,43 @@
#!/usr/bin/python3
# List sorting.
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
firstNames = ["edward","david", "Bob","mary","alice"]
intList = [7, 2, 12, 18, 16, 21]
primeList = [1, 2, 3, 7, 11, 13, 17, 19]
txtList = ["7", "2", "12", "18", "16", "21"]
print (dogNames," length = ",len(dogNames))
print(firstNames," length = ",len(firstNames))
print(intList," length = ",len(intList))
print(primeList," length = ",len(primeList))
print(txtList," length = ",len(txtList))
print("Reverse a list")
dogNames.reverse()
print (dogNames," length = ",len(dogNames))
print("\n")
print("Sort a text list")
dogNames.sort()
print (dogNames," length = ",len(dogNames))
print("\n")
print("Sort a number list")
intList.sort()
print (intList," length = ",len(intList))
print("\n")
print("Sum a number list")
mySum = sum(intList)
print (intList," sum = ",mySum)
print("\n")
print("Sort a text list of number strings")
txtList.sort()
print (txtList," length = ",len(txtList))
print("\n")

View file

@ -0,0 +1,14 @@
#!/usr/bin/python3
# List membership.
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
firstNames = ["edward","david", "Bob","mary","alice"]
print(firstNames)
# A match is found.
print("Is david in firstNames ? ",'david' in firstNames)
# Matches are case sensitive
print("Is David in firstNames ? ",'David' in firstNames)
# harry is not in the list of firstNames
print("Is harry in firstNames ? ",'harry' in firstNames)

View file

@ -0,0 +1,53 @@
#!/usr/bin/python3
# List join and split.
dogNames = [ "harry", "Shenanigans","katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
firstNames = ["edward","david", "Bob","mary","alice"]
# A simple loop to print every element in the list. The print() statement
# automatically generates a newline at the end of each print.
print("Print each name in the list.")
print(firstNames)
for aName in firstNames:
print(aName)
print("\n")
# Join all elements in the list to a single comma separated list
print("Join the elements in the list to a single comma-separated list.")
print(firstNames)
nameString = ','.join(firstNames)
print(nameString,"\n")
# Split all elements in the string into a list
print("Split the elements in the string into a list.")
print(nameString)
myNewList = nameString.split(',')
print(myNewList,"\n")
print("Now check membership in original list")
print("Is david in myNewList ? ",'david' in myNewList)
print("Exact match \n")
# Split all elements in the string into a list
# Notice the effect of extra spaces.
nameString2 = "edward ,david ,Bob ,mary ,alice"
print("Split the elements in the string with extra space into a list.")
print(nameString2)
myNewList = nameString2.split(',')
print(myNewList,"\n")
print("Now check membership in original list")
print("Is david in myNewList ? ",'david' in myNewList)
print("False match because 'david' does not equal 'david ' \n")
# Split a string into a list of characters
sampleString = "This is a sample string of characters."
listOfCharacters = list(sampleString)
print("My new list is \n",listOfCharacters,"\nLength = ", len(listOfCharacters),"\n")
print("What happens if you reverse it, then reassemble the string?")
listOfCharacters.reverse()
print(listOfCharacters)
myNewString = ''.join(listOfCharacters)
print(myNewString)