diff --git a/lecture20261110/contacts_directory/listvsdictionary.py b/lecture20261110/contacts_directory/listvsdictionary.py index 3020cef..8b6b161 100644 --- a/lecture20261110/contacts_directory/listvsdictionary.py +++ b/lecture20261110/contacts_directory/listvsdictionary.py @@ -18,4 +18,6 @@ print(addressDictionary.values()) for Item in addressDictionary.keys(): print(f"{Item:10s} {addressDictionary[Item]}") -pprint(addressDictionary) \ No newline at end of file +pprint(addressDictionary) + + diff --git a/lecture20261110/lists/grades02.py b/lecture20261110/lists/grades02.py index 996e014..7d493b4 100644 --- a/lecture20261110/lists/grades02.py +++ b/lecture20261110/lists/grades02.py @@ -3,6 +3,7 @@ # List element manipulation. scores = [75, 95, 90, 87, 99, 100, 97, 100] +#scores = [100,100,100,100,100,100, 100, 100] numberOfScores = len(scores) diff --git a/lecture20261110/lists/list04.py b/lecture20261110/lists/list04.py index 5394f4a..b443fd8 100644 --- a/lecture20261110/lists/list04.py +++ b/lecture20261110/lists/list04.py @@ -10,8 +10,8 @@ print (dogNames," length = ",len(dogNames),"Type =",type(dogNames)) print("\n") for item in dogNames: -# print(f"{item:14s} {type(item)}") - print(f"{item} {type(item)}") + print(f"{item:14s} {type(item)}") +# print(f"{item} {type(item)}") print("\nDone!") diff --git a/lecture20261117/for01/.idea/.gitignore b/lecture20261117/for01/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/lecture20261117/for01/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/lecture20261117/for01/.idea/for01.iml b/lecture20261117/for01/.idea/for01.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/lecture20261117/for01/.idea/for01.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/lecture20261117/for01/.idea/inspectionProfiles/Project_Default.xml b/lecture20261117/for01/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..7518cc8 --- /dev/null +++ b/lecture20261117/for01/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/lecture20261117/for01/.idea/inspectionProfiles/profiles_settings.xml b/lecture20261117/for01/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/lecture20261117/for01/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/lecture20261117/for01/.idea/misc.xml b/lecture20261117/for01/.idea/misc.xml new file mode 100644 index 0000000..ccf5d5d --- /dev/null +++ b/lecture20261117/for01/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/lecture20261117/for01/.idea/modules.xml b/lecture20261117/for01/.idea/modules.xml new file mode 100644 index 0000000..afedefb --- /dev/null +++ b/lecture20261117/for01/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/lecture20261117/for01/for01.py b/lecture20261117/for01/for01.py new file mode 100644 index 0000000..b6a7153 --- /dev/null +++ b/lecture20261117/for01/for01.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/lecture20261117/for01/for02.py b/lecture20261117/for01/for02.py new file mode 100644 index 0000000..8f8e189 --- /dev/null +++ b/lecture20261117/for01/for02.py @@ -0,0 +1,12 @@ +# Program to print the sum of first 5 natural numbers +# https://beginnersbook.com/2018/01/python-for-loop/ +# variable to store the sum +sum = 0 + +# iterating over natural numbers using range() +for val in range(1, 6): + # calculating sum + sum = sum + val + +# displaying sum of first 5 natural numbers +print(sum) diff --git a/lecture20261117/for01/for03.py b/lecture20261117/for01/for03.py new file mode 100644 index 0000000..1bd448b --- /dev/null +++ b/lecture20261117/for01/for03.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/lecture20261117/for01/for04.py b/lecture20261117/for01/for04.py new file mode 100644 index 0000000..3d95e8f --- /dev/null +++ b/lecture20261117/for01/for04.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/lecture20261117/for01/for05.py b/lecture20261117/for01/for05.py new file mode 100644 index 0000000..646abc4 --- /dev/null +++ b/lecture20261117/for01/for05.py @@ -0,0 +1,9 @@ + + +# https://www.programiz.com/python-programming/for-loop +digits = [0, 1, 5] + +for i in digits: + print(i) +else: + print("No items left.") diff --git a/lecture20261117/for01/for06.py b/lecture20261117/for01/for06.py new file mode 100644 index 0000000..5310393 --- /dev/null +++ b/lecture20261117/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/lecture20261117/for01/for07.py b/lecture20261117/for01/for07.py new file mode 100644 index 0000000..17bf01b --- /dev/null +++ b/lecture20261117/for01/for07.py @@ -0,0 +1,14 @@ + +#https://www.programiz.com/python-programming/for-loop +# program to display student's marks from record +student_name = 'Soyuj' +student_name = 'James' + +marks = {'James': 90, 'Jules': 55, 'Arthur': 77} + +for student in marks: + if student == student_name: + print(marks[student]) + break +else: + print('No entry with that name found.') diff --git a/lecture20261117/for01/main.py b/lecture20261117/for01/main.py new file mode 100644 index 0000000..94e3a87 --- /dev/null +++ b/lecture20261117/for01/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press ⌃R to execute it or replace it with your code. +# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/lecture20261117/for01/range01.py b/lecture20261117/for01/range01.py new file mode 100644 index 0000000..31ed085 --- /dev/null +++ b/lecture20261117/for01/range01.py @@ -0,0 +1,10 @@ + +# https://www.programiz.com/python-programming/for-loop + +print(range(10)) + +print(list(range(10))) + +print(list(range(2, 8))) + +print(list(range(2, 20, 3))) diff --git a/lecture20261117/if/if01.py b/lecture20261117/if/if01.py new file mode 100644 index 0000000..f3d834f --- /dev/null +++ b/lecture20261117/if/if01.py @@ -0,0 +1,32 @@ + +program01 = ''' + +Program: if01 +This program demonstrates the conditional tests used in Python. + +''' + +print(program01) + +a = 4 +b = 7 +c = 7 + +print(f"a = {a}\nb = {b}\nc = {c}\n") + +print(f"a == b {a==b}") +print(f"a != b {a!=b}") +print(f"a < b {a b {a>b}") +print(f"a >= b {a>=b}") + +print("\n") + +print(f"c == b {c==b}") +print(f"c != b {c!=b}") +print(f"c < b {c b {c>b}") +print(f"c >= b {c>=b}") + diff --git a/lecture20261117/if/if02.py b/lecture20261117/if/if02.py new file mode 100644 index 0000000..37d13de --- /dev/null +++ b/lecture20261117/if/if02.py @@ -0,0 +1,27 @@ + +# if statement example +if 22 > 7: + print("22 is greater than 7") + +print("A better method to define the code ...") + +# Use variables in the if statement and print +data1 = 22 + + +if data1 > 7: + print(f"{data1} is greater than 7") + + +# Or this form +print("And another method to define the code ...") + + +data1 = 22 +data2 = 7 + +if data1 > data2: + print(f"{data1} is greater than {data2}") + +print("Program ended") + diff --git a/lecture20261117/if/if03.py b/lecture20261117/if/if03.py new file mode 100644 index 0000000..577511b --- /dev/null +++ b/lecture20261117/if/if03.py @@ -0,0 +1,16 @@ + +# if..else statement example +x = 7 +print(f"{x} is equal to 7?") +if x == 7: + print("Yes") +else: + print("No") + +# if..else statement example +x = 5 +print(f"{x} is equal to 7?") +if x == 7: + print("Yes") +else: + print("No") diff --git a/lecture20261117/if/if04.py b/lecture20261117/if/if04.py new file mode 100644 index 0000000..8956a55 --- /dev/null +++ b/lecture20261117/if/if04.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 +# Program: if02 + +# Lists of numbers. These could be integers or floats. +intList = [7, 2, 16, 18, 12, 21] + +# Print the list and the length +print(intList," length = ",len(intList)) + +print(f"Print each value in the list") +counter = 0 +for item in intList: + print(f"intList[{counter}] = {item:2d}") + counter += 1 + +cutoff = 13 +print(f"Print each value in the list >= {cutoff}") +counter = 0 +for item in intList: + if(item >= cutoff): + print(f"intList[{counter}] = {item:2d}") + counter += 1 + diff --git a/lecture20261117/if/if05.py b/lecture20261117/if/if05.py new file mode 100644 index 0000000..fc31f8a --- /dev/null +++ b/lecture20261117/if/if05.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 +# Program: if03 + +# Lists of numbers. These could be integers or floats. +intList = [7, 2, 16, 18, 12, 21] + +# Print the list and the length +print(intList," length = ",len(intList)) + +print(f"Print each value in the list") +counter = 0 +for item in intList: + print(f"intList[{counter}] = {item:2d}") + counter += 1 + +cutoff = 13 +print(f"Print each value in the list >= {cutoff}") +counter = 0 +for item in intList: + if(item >= cutoff): + print(f"intList[{counter}] = {item:2d}") + else: + print(f"========> intList[{counter}] = {item:2d} is less than {cutoff}") + counter += 1 + diff --git a/lecture20261117/if/if06.py b/lecture20261117/if/if06.py new file mode 100644 index 0000000..bf9b1f5 --- /dev/null +++ b/lecture20261117/if/if06.py @@ -0,0 +1,7 @@ + +# consider the following line from the autoloan program: + +principal = float(input("Enter a value for the principal: ")) + + +print(f"You entered the value {principal}") \ No newline at end of file diff --git a/lecture20261117/if/if07.py b/lecture20261117/if/if07.py new file mode 100644 index 0000000..8ce43f5 --- /dev/null +++ b/lecture20261117/if/if07.py @@ -0,0 +1,16 @@ + +# consider the following line from the autoloan program: + +principal = float(input("Enter a value for the principal: ")) +if(principal < 5000.) or (principal >120000.00): + print(f"The loan value {principal} is outside the allowed limits.") + exit(-1) + +rate = float(input("Enter a value for the interest rate (7 for 7%): ")) + +numberOfYears = float(input("Enter the number of years for the loan: ")) + +print(f"You entered the principal value {principal}") +print(f"You entered the rate value {rate}%") +print(f"You entered the loan term value {numberOfYears} years") + diff --git a/lecture20261117/try/try01.py b/lecture20261117/try/try01.py new file mode 100644 index 0000000..02ed292 --- /dev/null +++ b/lecture20261117/try/try01.py @@ -0,0 +1,8 @@ + +data1 = float(input("Enter the dividend ")) + +data2 = float(input("Enter the divisor ")) + +quotient = data1/data2 + +print(f"{data1}/{data2} = {quotient}") diff --git a/lecture20261117/try/try02.py b/lecture20261117/try/try02.py new file mode 100644 index 0000000..7ee0d33 --- /dev/null +++ b/lecture20261117/try/try02.py @@ -0,0 +1,13 @@ +import sys + +data1 = float(input("Enter the dividend ")) + +data2 = float(input("Enter the divisor ")) + +try: + quotient = data1/data2 + print(f"{data1}/{data2} = {quotient}") +except: + print(f"Invalid value. Division by zero is not allowed.") + sys.exit(-1) + diff --git a/lecture20261117/try/try03.py b/lecture20261117/try/try03.py new file mode 100644 index 0000000..7c2057c --- /dev/null +++ b/lecture20261117/try/try03.py @@ -0,0 +1,20 @@ +import sys + +try: + data1 = float(input("Enter the dividend ")) +except: + print(f"Invalid value for the dividend ") + sys.exit(-2) +try: + data2 = float(input("Enter the divisor ")) +except: + print(f"Invalid value for the divisor ") + sys.exit(-3) + +try: + quotient = data1/data2 + print(f"{data1}/{data2} = {quotient}") +except: + print(f"Invalid value. Division by zero is not allowed.") + sys.exit(-1) + diff --git a/lecture20261117/try/try04.py b/lecture20261117/try/try04.py new file mode 100644 index 0000000..fc9ca62 --- /dev/null +++ b/lecture20261117/try/try04.py @@ -0,0 +1,19 @@ +import sys + +def getFloatValue(message): + try: + data1 = float(input(message)) + except: + print(f"Invalid value for a floating point number. ") + sys.exit(-2) + return data1 + +data1 = getFloatValue("Enter value for the dividend ") +data2 = getFloatValue("Enter value for the divisor ") + +try: + quotient = data1/data2 + print(f"{data1}/{data2} = {quotient}") +except: + print(f"Invalid value. Division by zero is not allowed.") + sys.exit(-1) diff --git a/lecture20261117/while/.idea/encodings.xml b/lecture20261117/while/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/lecture20261117/while/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lecture20261117/while/.idea/inspectionProfiles/Project_Default.xml b/lecture20261117/while/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..579ee6c --- /dev/null +++ b/lecture20261117/while/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/lecture20261117/while/.idea/inspectionProfiles/profiles_settings.xml b/lecture20261117/while/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/lecture20261117/while/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/lecture20261117/while/.idea/misc.xml b/lecture20261117/while/.idea/misc.xml new file mode 100644 index 0000000..5a946c5 --- /dev/null +++ b/lecture20261117/while/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lecture20261117/while/.idea/modules.xml b/lecture20261117/while/.idea/modules.xml new file mode 100644 index 0000000..60df014 --- /dev/null +++ b/lecture20261117/while/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/lecture20261117/while/.idea/while.iml b/lecture20261117/while/.idea/while.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/lecture20261117/while/.idea/while.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/lecture20261117/while/.idea/workspace.xml b/lecture20261117/while/.idea/workspace.xml new file mode 100644 index 0000000..a0ff75e --- /dev/null +++ b/lecture20261117/while/.idea/workspace.xml @@ -0,0 +1,605 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lecture20261117/while/names1.py b/lecture20261117/while/names1.py new file mode 100644 index 0000000..540aaa8 --- /dev/null +++ b/lecture20261117/while/names1.py @@ -0,0 +1,32 @@ + +print("Process fNames") + +fNames = [ "Harry", "Moe", "Katrina", "Colorado", "Shenanigans", "Tracey"] +lNames = [ "Bigos", "Howard", "Smith", "Jones", "Davis", "Carter"] +counter = 0 + + +print("\nThe length of the list is {}\n".format(len(fNames))) + +while(counter < len(fNames) ): + print("Counter = ",counter) + print("fNames[{}] = {}".format(counter,fNames[counter])) + print("lNames[{}] = {}\n".format(counter,lNames[counter])) + counter = counter + 1 + +print("Process list - sorted - but doesn't work right") + +fNames.sort() +lNames.sort() +counter = 0 ; + + +print("\nThe length of the list is {}\n".format(len(fNames))) + +while(counter < len(fNames) ): + print("Counter = ",counter) + print("fNames[{}] = {}".format(counter,fNames[counter])) + print("lNames[{}] = {}\n".format(counter,lNames[counter])) + counter = counter + 1 + +print("Problem: How do we sort both lists and keep the first and last names together.") diff --git a/lecture20261117/while/names2.py b/lecture20261117/while/names2.py new file mode 100644 index 0000000..bd9ee04 --- /dev/null +++ b/lecture20261117/while/names2.py @@ -0,0 +1,29 @@ + +print("Process fNames") + +fNames = [ "Harry Bigos", "Moe Howard", "Katrina Smith", "Colorado Jones", "Shenanigans Davis", "Tracey Carter"] +counter = 0 + + +print("\nThe length of the list is {}\n".format(len(fNames))) + +while(counter < len(fNames) ): + print("Counter = ",counter) + print("fNames[{}] = {}".format(counter,fNames[counter])) + counter = counter + 1 + +print("Process list - sorted - but doesn't work right") + +fNames.sort() +counter = 0 ; + + +print("\nThe length of the list is {}\n".format(len(fNames))) + +while(counter < len(fNames) ): + print("Counter = ",counter) + print("fNames[{}] = {}".format(counter,fNames[counter])) + counter = counter + 1 + +print("Problem: How do we sort both lists and keep the first and last names together.") +print("One solution: Redefine the data.") diff --git a/lecture20261117/while/rectangle2.py b/lecture20261117/while/rectangle2.py new file mode 100644 index 0000000..9f27b83 --- /dev/null +++ b/lecture20261117/while/rectangle2.py @@ -0,0 +1,30 @@ +print("Calculate the area of a rectangle") + +counter = 3 +length = float(input("Enter the length of one side: ")) +while(length <= 0): + print("ERROR: The length must be greater than zero") + print("You entered {}".format(length)) + counter = counter - 1 + if(counter <= 0): + print("Too many tries. Program exiting.") + exit(-1) + length = float(input("Enter the length of one side: ")) + +runFlag = True +counter = 3 +while(runFlag): + width = float(input("Enter the width of the other side: ")) + if(width <= 0): + print("ERROR: The width must be greater than zero") + print("You entered {}".format(width)) + counter = counter - 1 + if(counter <= 0): + print("Too many tries. Program exiting.") + exit(-1) + else: + runFlag = False + +area = length * width + +print("The area is {}".format(area)) diff --git a/lecture20261117/while/while01.py b/lecture20261117/while/while01.py new file mode 100644 index 0000000..38b252a --- /dev/null +++ b/lecture20261117/while/while01.py @@ -0,0 +1,15 @@ + +print("Down counter") +counter = 5 ; + +while(counter > 0 ): + print("Counter = ",counter) + counter = counter - 1 + +counter = 0 ; + +print("Up counter") +while(counter < 5 ): + print("Counter = ",counter) + counter = counter + 1 + diff --git a/lecture20261117/while/while02.py b/lecture20261117/while/while02.py new file mode 100644 index 0000000..5d37af7 --- /dev/null +++ b/lecture20261117/while/while02.py @@ -0,0 +1,9 @@ + +print("Down counter") +counter = 5 ; +index = 0 + +while(counter > 0 ): + print("Counter = ",counter, " Index = ", index) + index = index + 1 + counter = counter - 1 diff --git a/lecture20261117/while/while03.py b/lecture20261117/while/while03.py new file mode 100644 index 0000000..d8daaba --- /dev/null +++ b/lecture20261117/while/while03.py @@ -0,0 +1,11 @@ + +print("Process list") +counter = 5 ; +index = 0 +names = [ "Harry", "Larry", "Moe", "Katrina", "Colorado", "Shenanigans"] + +while(counter > 0 ): + print("Counter = ",counter, " Index = ", index) + print("names[{}] = {}\n".format(index,names[index])) + index = index + 1 + counter = counter - 1 diff --git a/lecture20261117/while/while04.py b/lecture20261117/while/while04.py new file mode 100644 index 0000000..dba4a9f --- /dev/null +++ b/lecture20261117/while/while04.py @@ -0,0 +1,14 @@ + +print("Process list") + +names = [ "Harry", "Larry", "Moe", "Katrina", "Colorado", "Shenanigans", "Tracey"] +counter = len(names) ; +index = 0 + +print("\nThe length of the list is {}\n".format(counter)) + +while(counter > 0 ): + print("Counter = ",counter, " Index = ", index) + print("names[{}] = {}\n".format(index,names[index])) + index = index + 1 + counter = counter - 1 diff --git a/lecture20261117/while/while05.py b/lecture20261117/while/while05.py new file mode 100644 index 0000000..5d127a2 --- /dev/null +++ b/lecture20261117/while/while05.py @@ -0,0 +1,26 @@ + +print("Process list - minimal code") + +names = [ "Harry", "Moe", "Katrina", "Colorado", "Shenanigans", "Tracey"] +counter = 0 + + +print("\nThe length of the list is {}\n".format(len(names))) + +while(counter < len(names) ): + print("Counter = ",counter) + print("names[{}] = {}\n".format(counter,names[counter])) + counter = counter + 1 + +print("Process list - sorted") + +names.sort() +counter = 0 ; + + +print("\nThe length of the list is {}\n".format(len(names))) + +while(counter < len(names) ): + print("Counter = ",counter) + print("names[{}] = {}\n".format(counter,names[counter])) + counter = counter + 1