diff --git a/bigos/lecture20250225/for01/for01.py b/bigos/lecture20250225/for01/for01.py new file mode 100644 index 0000000..1bd448b --- /dev/null +++ b/bigos/lecture20250225/for01/for01.py @@ -0,0 +1,8 @@ + + +#https://beginnersbook.com/2018/01/python-for-loop/ + +for val in range(5): + print(val) +else: + print("The loop has completed execution") diff --git a/bigos/lecture20250225/for01/for02.py b/bigos/lecture20250225/for01/for02.py new file mode 100644 index 0000000..b6a7153 --- /dev/null +++ b/bigos/lecture20250225/for01/for02.py @@ -0,0 +1,15 @@ +# Program to print squares of all numbers present in a list +# https://beginnersbook.com/2018/01/python-for-loop/ + +# List of integer numbers +numbers = [1, 2, 4, 6, 11, 20] + +# variable to store the square of each num temporary +sq = 0 + +# iterating over the given list +for val in numbers: + # calculating square of each number + sq = val * val + # displaying the squares + print(sq) diff --git a/bigos/lecture20250225/for01/for03.py b/bigos/lecture20250225/for01/for03.py new file mode 100644 index 0000000..55be419 --- /dev/null +++ b/bigos/lecture20250225/for01/for03.py @@ -0,0 +1,24 @@ + +print("Print a range(6) ") +for x in range(6): + print(x) +else: + print("Finally finished!") + +print("\n\nPrint a range(0, 3) ") +for x in range(0, 3): + print("Value of x = {:d}".format(x) ) + + +print("\n\nPrint a range(45, 55) ") +for x in range(45, 55): + print("Value of x = {:d}".format(x) ) + + +print("\n\nPrint a range(2, 30, 3) ") +for x in range(2, 30, 4): + print(x) + +print("\n\nPrint a range(1, 11, 2) ") +for x in range(1, 11, 2): + print(x) diff --git a/bigos/lecture20250225/for01/for04.py b/bigos/lecture20250225/for01/for04.py new file mode 100644 index 0000000..2853ea8 --- /dev/null +++ b/bigos/lecture20250225/for01/for04.py @@ -0,0 +1,5 @@ + +print("Break a string into characters.") +string = "Hello World" +for x in string: + print(x) diff --git a/bigos/lecture20250225/for01/for05.py b/bigos/lecture20250225/for01/for05.py new file mode 100644 index 0000000..3d95e8f --- /dev/null +++ b/bigos/lecture20250225/for01/for05.py @@ -0,0 +1,7 @@ + +# https://beginnersbook.com/2018/01/python-for-loop/ + +for num1 in range(0,3): + for num2 in range(0, 3): + print(num1, ",", num2) + diff --git a/bigos/lecture20250225/for01/for06.py b/bigos/lecture20250225/for01/for06.py new file mode 100644 index 0000000..5310393 --- /dev/null +++ b/bigos/lecture20250225/for01/for06.py @@ -0,0 +1,10 @@ + +# https://www.programiz.com/python-programming/for-loop +digits = [70, 91, 85] + +scoreSum = 0 +for i in digits: + print(i) + scoreSum += i +else: + print("The average is ",scoreSum/len(digits)) diff --git a/bigos/lecture20250225/lists01/list02.py b/bigos/lecture20250225/lists01/list02.py new file mode 100644 index 0000000..d64f6e6 --- /dev/null +++ b/bigos/lecture20250225/lists01/list02.py @@ -0,0 +1,18 @@ + + +myList = [ 7, 5, 3, 9,11, 9999, "bob"] + + +print("The list is ",myList) +print("The type is ",type(myList)) +print("The length is ", len(myList) ) +print("\n") + +counter = 0 +while(counter < len(myList)) : + print(f"{counter} {myList[counter]} ") + counter = counter + 1 + + + + diff --git a/bigos/lecture20250225/lists01/list03.py b/bigos/lecture20250225/lists01/list03.py new file mode 100644 index 0000000..29f8955 --- /dev/null +++ b/bigos/lecture20250225/lists01/list03.py @@ -0,0 +1,27 @@ + + +myList = [ 7, 5, 3, 9,11] + + +print("The list is ",myList) +print("The type is ",type(myList)) +print("The length is ", len(myList) ) +print("\n") + +for dummy in myList: + print(f"{dummy:3d} {dummy**2:4d} {dummy**3:5d}") + +print("Done!") +for listElement in myList: + print(f"{listElement:3d} {listElement**2:4d} {listElement**3:5d}") + +print("Done!") + +for x in myList: + print(f"{x:3d} {x**2:4d} {x**3:5d}") + +print("Done!") + + + + diff --git a/bigos/lecture20250225/lists01/lists01.py b/bigos/lecture20250225/lists01/lists01.py new file mode 100644 index 0000000..43ce084 --- /dev/null +++ b/bigos/lecture20250225/lists01/lists01.py @@ -0,0 +1,17 @@ + + +myList = [ 7, 5, 3, 9] + + +print(myList) +print(type(myList)) + +print( len(myList) ) + +print(myList[0]) +print(myList[1]) +print(myList[2]) +print(myList[3]) + +# This generates an error when you uncomment it +#print(myList[4]) diff --git a/bigos/lecture20250225/lists01/names.py b/bigos/lecture20250225/lists01/names.py new file mode 100644 index 0000000..e49823f --- /dev/null +++ b/bigos/lecture20250225/lists01/names.py @@ -0,0 +1,8 @@ + +firstNames = [ "Harry", "Rusty", "Katrina", "Jack", "Gimli","Shenanigans", "Mandy"] + +print(firstNames) +print(len(firstNames)) + +for firstName in firstNames: + print(firstName) diff --git a/bigos/lecture20250225/lists01/names2.py b/bigos/lecture20250225/lists01/names2.py new file mode 100644 index 0000000..d3c225d --- /dev/null +++ b/bigos/lecture20250225/lists01/names2.py @@ -0,0 +1,14 @@ + +firstNames = [ "Harry", "Rusty", "Katrina", "Jack", "Gimli","Shenanigans", "Mandy"] + +print(firstNames) +print(len(firstNames)) + +for firstName in firstNames: + print(firstName) + +firstNames.sort() +print(firstNames) +for firstName in firstNames: + print(firstName) + diff --git a/bigos/lecture20250225/lists01/powers.py b/bigos/lecture20250225/lists01/powers.py new file mode 100644 index 0000000..747d005 --- /dev/null +++ b/bigos/lecture20250225/lists01/powers.py @@ -0,0 +1,6 @@ + +powersList = [0,1,2,3,4,5,6,7,8,9,10] + +for n in powersList: + powerOfTwo = 2**n + print(f"{n:2d} {powerOfTwo:8d}") diff --git a/bigos/lecture20250225/lists02/dogs-lists08.py b/bigos/lecture20250225/lists02/dogs-lists08.py new file mode 100644 index 0000000..bb2be27 --- /dev/null +++ b/bigos/lecture20250225/lists02/dogs-lists08.py @@ -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") + diff --git a/bigos/lecture20250225/lists02/dogs-lists09.py b/bigos/lecture20250225/lists02/dogs-lists09.py new file mode 100644 index 0000000..b4b197e --- /dev/null +++ b/bigos/lecture20250225/lists02/dogs-lists09.py @@ -0,0 +1,50 @@ +# dogs-lists09 + +#Dog Description List format = [,,, ] + +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") + diff --git a/bigos/lecture20250225/lists02/dogs-lists10.py b/bigos/lecture20250225/lists02/dogs-lists10.py new file mode 100644 index 0000000..99d21df --- /dev/null +++ b/bigos/lecture20250225/lists02/dogs-lists10.py @@ -0,0 +1,55 @@ +# dogs-lists10 + +#Example using lambda + +#Dog Description List format = [,,, ] + +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) diff --git a/bigos/lecture20250225/lists02/dogs-lists10a.py b/bigos/lecture20250225/lists02/dogs-lists10a.py new file mode 100644 index 0000000..abd1c51 --- /dev/null +++ b/bigos/lecture20250225/lists02/dogs-lists10a.py @@ -0,0 +1,57 @@ +# dogs-lists10a + +#Example using itemgetter + +from operator import itemgetter + +#Dog Description List format = [,,, ] + +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) diff --git a/bigos/lecture20250225/lists02/forloopprimer.py b/bigos/lecture20250225/lists02/forloopprimer.py new file mode 100644 index 0000000..e7cbfe8 --- /dev/null +++ b/bigos/lecture20250225/lists02/forloopprimer.py @@ -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") diff --git a/bigos/lecture20250225/lists02/fstringprimer.py b/bigos/lecture20250225/lists02/fstringprimer.py new file mode 100644 index 0000000..bd40fbd --- /dev/null +++ b/bigos/lecture20250225/lists02/fstringprimer.py @@ -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}") + diff --git a/bigos/lecture20250225/lists02/lists01-define-and-print.py b/bigos/lecture20250225/lists02/lists01-define-and-print.py new file mode 100644 index 0000000..1a0d918 --- /dev/null +++ b/bigos/lecture20250225/lists02/lists01-define-and-print.py @@ -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)) + diff --git a/bigos/lecture20250225/lists02/lists02-access-elements-simple.py b/bigos/lecture20250225/lists02/lists02-access-elements-simple.py new file mode 100644 index 0000000..0090bf1 --- /dev/null +++ b/bigos/lecture20250225/lists02/lists02-access-elements-simple.py @@ -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]) + + diff --git a/bigos/lecture20250225/lists02/lists02-access-elements.py b/bigos/lecture20250225/lists02/lists02-access-elements.py new file mode 100644 index 0000000..a77965d --- /dev/null +++ b/bigos/lecture20250225/lists02/lists02-access-elements.py @@ -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]) + diff --git a/bigos/lecture20250225/lists02/lists03-append-insert-extend-remove.py b/bigos/lecture20250225/lists02/lists03-append-insert-extend-remove.py new file mode 100644 index 0000000..564c10f --- /dev/null +++ b/bigos/lecture20250225/lists02/lists03-append-insert-extend-remove.py @@ -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") + + + + diff --git a/bigos/lecture20250225/lists02/lists04-pop.py b/bigos/lecture20250225/lists02/lists04-pop.py new file mode 100644 index 0000000..cde767f --- /dev/null +++ b/bigos/lecture20250225/lists02/lists04-pop.py @@ -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)) + + diff --git a/bigos/lecture20250225/lists02/lists05-sort.py b/bigos/lecture20250225/lists02/lists05-sort.py new file mode 100644 index 0000000..61fe67f --- /dev/null +++ b/bigos/lecture20250225/lists02/lists05-sort.py @@ -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") + + + diff --git a/bigos/lecture20250225/lists02/lists06-membership.py b/bigos/lecture20250225/lists02/lists06-membership.py new file mode 100644 index 0000000..0e6a7fb --- /dev/null +++ b/bigos/lecture20250225/lists02/lists06-membership.py @@ -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) diff --git a/bigos/lecture20250225/lists02/lists07-join-split.py b/bigos/lecture20250225/lists02/lists07-join-split.py new file mode 100644 index 0000000..e3f8fa2 --- /dev/null +++ b/bigos/lecture20250225/lists02/lists07-join-split.py @@ -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)