From 458588bd0a8a96208d208df75865b990a9d64333 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 12:17:26 -0500 Subject: [PATCH 01/13] 1-3 modules done Everything seems to work, so far so good --- src/01_bignum.py | 3 ++- src/02_datatypes.py | 5 +++-- src/03_modules.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/01_bignum.py b/src/01_bignum.py index c020928d63..0347ae3f4b 100644 --- a/src/01_bignum.py +++ b/src/01_bignum.py @@ -1,4 +1,5 @@ # Print out 2 to the 65536 power # (try doing the same thing in the JS console and see what it outputs) -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +print(2**65536) \ No newline at end of file diff --git a/src/02_datatypes.py b/src/02_datatypes.py index 245193da34..309be63848 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -14,8 +14,9 @@ # Write a print statement that combines x + y into the integer value 12 # YOUR CODE HERE - +print(int(y) + x) # Write a print statement that combines x + y into the string value 57 -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +print(f'{x}{y}') \ No newline at end of file diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..931f6215b2 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -5,27 +5,56 @@ level operating system functionality. """ +import getpass import sys +import argparse +import platform +import os + # See docs for the sys module: https://docs.python.org/3.7/library/sys.html # Print out the command line arguments in sys.argv, one per line: # YOUR CODE HERE +# Total arguments +n = len(sys.argv) +print("Total arguments passed: " + str(n)) + +# Arguments passed: +print("\nName of python script:", sys.argv[0]) + +print("\nArguments passed:", end = " ") +for i in range(1, n): + print(sys.argv[i], end = " ") + # Print out the OS platform you're using: # YOUR CODE HERE +print("My OS is", os.name) + # Print out the version of Python you're using: # YOUR CODE HERE +print("My python Version is:", sys.version) +print("MY python version infi is:", sys.version_info) -import os # See the docs for the OS module: https://docs.python.org/3.7/library/os.html # Print the current process ID # YOUR CODE HERE +print("This is my current process ID:", os.getpid()) + # Print the current working directory (cwd): # YOUR CODE HERE +print("My current working directory is", os.getcwd()) + # Print out your machine's login name # YOUR CODE HERE + +username = os.getlogin() +username2 = getpass.getuser + +print("My computers username is:", username) +print("MY compuers username2 is", username2) \ No newline at end of file From f5c035a67766afbaf4930db8a72034330c5d7230 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 12:51:55 -0500 Subject: [PATCH 02/13] Finished next 2 modules in here, everything works so far so good --- src/04_printing.py | 9 ++++++++- src/05_lists.py | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..9fac177baa 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -12,6 +12,13 @@ # y, and z: # x is 10, y is 2.25, z is "I like turtles!" +print("x: %2d, y: %2.2f, z: \"%s\"" % (x, y, z)) + +print("=================================") + + # Use the 'format' string method to print the same thing -# Finally, print the same thing using an f-string \ No newline at end of file +# Finally, print the same thing using an f- + +print(f"x = {x}, \ny = {y:.2f}, \nz = \"{z}\"") diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..239429044e 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -8,22 +8,30 @@ # Change x so that it is [1, 2, 3, 4] # YOUR CODE HERE +x.append(4) print(x) # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] # YOUR CODE HERE +x += y print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] # YOUR CODE HERE +x.remove(8) print(x) # Change x so that it is [1, 2, 3, 4, 9, 99, 10] # YOUR CODE HERE +x.insert(5, 99) print(x) # Print the length of list x # YOUR CODE HERE +print(len(x)) +print(x.count) # Print all the values in x multiplied by 1000 -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE + +print(list(map(lambda a: a * 1000, x))) \ No newline at end of file From 25f93bdc83aeba4850e6120eac5ed6a1693f296a Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 13:13:40 -0500 Subject: [PATCH 03/13] Added print tuple function and corrected syntax error, also didnt realize i had unsaved files --- src/06_tuples.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..49613de60a 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -34,11 +34,13 @@ def dist(a, b): # Write a function `print_tuple` that prints all the values in a tuple -# YOUR CODE HERE +def print_tuple(tuple): + for element in tuple: + print(element) t = (1, 2, 5, 7, 99) print_tuple(t) # Prints 1 2 5 7 99, one per line # Declare a tuple of 1 element then print it -u = (1) # What needs to be added to make this work? +u = (1,) # What needs to be added to make this work? print_tuple(u) From deb261c09f6ce0dc20c92136e549068c08651325 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 13:14:32 -0500 Subject: [PATCH 04/13] Added unsaved changes --- src/00_hello.py | 5 ++++- src/03_modules.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..1dff9b41cb 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1 +1,4 @@ -# Print "Hello, world!" to your terminal \ No newline at end of file +# Print "Hello, world!" to your terminal + +hello = "Hello, world" +print(hello) \ No newline at end of file diff --git a/src/03_modules.py b/src/03_modules.py index 931f6215b2..fef8ee97e1 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -57,4 +57,4 @@ username2 = getpass.getuser print("My computers username is:", username) -print("MY compuers username2 is", username2) \ No newline at end of file +print("MY computers username2 is", username2) \ No newline at end of file From d6cd0cbabe13e82197923831f3c0ef2b55c01896 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 13:39:57 -0500 Subject: [PATCH 05/13] Completed slices. --- src/07_slices.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..c3775c693d 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -12,26 +12,27 @@ a = [2, 4, 1, 7, 9, 6] # Output the second element: 4: -print() +print(a[1]) # Output the second-to-last element: 9 -print() +print(a[-2]) # Output the last three elements in the array: [7, 9, 6] -print() +print(a[-3:]) # Output the two middle elements in the array: [1, 7] -print() +print(a[2:4]) # Output every element except the first one: [4, 1, 7, 9, 6] -print() +print(a[1:]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +# When using minus one and the colon to the left the compiler then goes all the way from the last number to the left (-1 to -6) +print(a[:-1]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +print(s[7:12]) From 78760f3f9288acc9a1dc815bea188c098509934f Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 14:16:23 -0500 Subject: [PATCH 06/13] Finished problems on comprehensions --- src/08_comprehensions.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..366017ffb4 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -10,15 +10,18 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +y = [num for num in range(1, 6)] +print(y) -print (y) +y1 = [] +for num in range(1, 6): + y1.append(num) + print(y1) # Write a list comprehension to produce the cubes of the numbers 0-9: # [0, 1, 8, 27, 64, 125, 216, 343, 512, 729] -y = [] - +y = [num**3 for num in range(10)] print(y) # Write a list comprehension to produce the uppercase version of all the @@ -26,16 +29,15 @@ a = ["foo", "bar", "baz"] -y = [] +y = [word.upper() for word in a] print(y) # Use a list comprehension to create a list containing only the _even_ elements # the user entered into list x. - x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] +y = [num for num in x if int(num) % 2 == 0] print(y) \ No newline at end of file From 3d1291a5507c5a508a486a31c88d6353fa6e4a5f Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 15:46:05 -0500 Subject: [PATCH 07/13] Finished dictionaries --- src/09_dictionaries.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..4a8ca13f40 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -14,6 +14,7 @@ - lon: a signed integer representing a longitude value - name: a name string for this location """ +import json waypoints = [ { @@ -36,12 +37,48 @@ # Add a new waypoint to the list # YOUR CODE HERE +waypoints.append( { + "lat": 120, + "lon": -11, + "name": "Some random place" +}) + +# Another way +# anotherPlace = { "lat": 42, "lon": -120, "name": "a fourth place"} +# waypoints.append(anotherPlace) +# print(waypoints) + +# *************How to print json data ************** +# json_data = '[{"ID":10,"Name":"Pankaj","Role":"CEO"},' \ +# '{"ID":20,"Name":"David Lee","Role":"Editor"}]' + +# json_object = json.loads(json_data) +# json_formatted_str = json.dumps(json_object, indent=2) +# print(json_formatted_str) +# dont forget to import json + +# with open('EmployeeData.json', 'r') as json_file: +# json_object = json.load(json_file) + +# print(json_object) +# print(json.dumps(json_object)) +# print(json.dumps(json_object, indent=1)) +# ********************************************************** + + # Modify the dictionary with name "a place" such that its longitude # value is -130 and change its name to "not a real place" # Note: It's okay to access the dictionary using bracket notation on the # waypoints list. # YOUR CODE HERE +waypoints[0]["lon"] = -130 +waypoints[0]["name"] = "not a real place" # Write a loop that prints out all the field values for all the waypoints -# YOUR CODE HERE \ No newline at end of file + +# YOUR CODE HERE +for i, waypoint in enumerate(waypoints): + print(f"Waypoint #{i + 1}") + for key, value in waypoint.items(): + print(f" { key }: {value}") \ No newline at end of file From c99e73610a3e65d0ef22a622fdf45e485c9d0329 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 15:58:54 -0500 Subject: [PATCH 08/13] Finished number 10, odd even function seems to work --- src/10_functions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..5a3033a7a1 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,6 +1,8 @@ # Write a function is_even that will return true if the passed-in number is even. # YOUR CODE HERE +def is_even(num): + return num % 2 == 0 # Read a number from the keyboard num = input("Enter a number: ") @@ -9,4 +11,7 @@ # Print out "Even!" if the number is even. Otherwise print "Odd" # YOUR CODE HERE - +if is_even(num): + print("Even") +else: + print("Odd") From ce5c163ec6c6326954deb40edba387a5e558a918 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 16:14:16 -0500 Subject: [PATCH 09/13] Completed 11 --- src/11_args.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..1a3782e872 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -5,6 +5,8 @@ # the sum. This is what you'd consider to be a regular, normal function. # YOUR CODE HERE +def f1(num1, num2): + return num1 + num2 print(f1(1, 2)) @@ -14,6 +16,12 @@ # YOUR CODE HERE +def f2(*nums): + sum = 0 + for num in nums: + sum += num + return sum + print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 print(f2(1, 4, -12)) # Should print -7 @@ -22,7 +30,7 @@ a = [7, 6, 5, 4] # How do you have to modify the f2 call below to make this work? -print(f2(a)) # Should print 22 +print(f2(*a)) # Should print 22 # Write a function f3 that accepts either one or two arguments. If one argument, # it returns that value plus 1. If two arguments, it returns the sum of the @@ -31,6 +39,9 @@ # YOUR CODE HERE +def f3(num1, num2 = 1): + return num1 + num2 + print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -45,6 +56,10 @@ # YOUR CODE HERE +def f4(**keyValues): + for key, value in keyValues.items(): + print("Key: %s, Value: %s" % (key, value)) + # Should print # key: a, value: 12 # key: b, value: 30 @@ -62,4 +77,4 @@ } # How do you have to modify the f4 call below to make this work? -f4(d) +f4(**d) From cb67eead8ca3a15cf59414fb6ddc4f47bbe11fcc Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 5 Sep 2020 16:48:31 -0500 Subject: [PATCH 10/13] Finished scopes --- src/12_scopes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/12_scopes.py b/src/12_scopes.py index bc467fa423..fe00b2f45b 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -5,9 +5,11 @@ x = 12 def change_x(): + global x x = 99 -change_x() + change_x() + # This prints 12. What do we have to modify in change_x() to get it to print 99? print(x) @@ -19,6 +21,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() From 23ac605c15b1cba5013cb6135e4da116d18180b2 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Mon, 7 Sep 2020 18:33:59 -0500 Subject: [PATCH 11/13] Opened foo.text in code, had the compiler read and close, then created new text file in code --- src/13_file_io.py | 12 +++++++++++- src/bar.txt | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/bar.txt diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..40b81ce9fd 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -11,9 +11,19 @@ # YOUR CODE HERE +f = open('foo.txt', 'r') +print(f.read()) +f.close() + # Open up a file called "bar.txt" (which doesn't exist yet) for # writing. Write three lines of arbitrary content to that file, # then close the file. Open up "bar.txt" and inspect it to make # sure that it contains what you expect it to contain -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE + +f = open('bar.txt', 'w') +f.write('Some random text \n') +f.write('This is some new line of code \n') +f.write('Some more random stuff to write \n') +f.close() \ No newline at end of file diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..6f9c558079 --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,3 @@ +Some random text +This is some new line of code +Some more random stuff to write From a48e46b1fd9d5fc4b29f9042c4ae6b70b89c3568 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Mon, 7 Sep 2020 18:43:19 -0500 Subject: [PATCH 12/13] Added calander functionality in the command line --- src/14_cal.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..76ea0c3690 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -29,4 +29,17 @@ import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime + +def cal(month = datetime.now().month, year = datetime.now().year): + print(calendar.month(theyear = int(year), themonth = int(month))) + +arg = sys.argv +if len(arg) == 2: + cal(month = arg[1]) +elif len(arg) == 3: + cal(arg[1], arg[2]) +elif len(arg) > 3: + print('Please provide a month and a year') +else: + cal() From 21c168891a2a88ec22d7454ef205d4b971dbfbc6 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Mon, 7 Sep 2020 18:58:06 -0500 Subject: [PATCH 13/13] Completed all assignments --- src/15_classes.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..71fe2903e6 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -3,20 +3,47 @@ # YOUR CODE HERE +class LatLon(): + def __init__(self, lat, lon): + self.lat = lat + self.lon = lon # Make a class Waypoint that can be passed parameters `name`, `lat`, and `lon` to the # constructor. It should inherit from LatLon. Look up the `super` method. # YOUR CODE HERE +class Waypoint(LatLon): + def __init__(self, name, lat, lon): + self.name = name + super().__init__(lat, lon) + + def __str__(self): + return f'Name: {self.name}, Lat: {self.lat}, Lon: {self.lon}' # Make a class Geocache that can be passed parameters `name`, `difficulty`, # `size`, `lat`, and `lon` to the constructor. What should it inherit from? # YOUR CODE HERE +class Geocache(Waypoint): + def __init__(self, name, difficulty, size, lat, lon): + super().__init__(name, lat, lon) + self.difficulty = difficulty + self.size = size + + def __str__(self): + return (f'Name: {self.name}, '\ + f'Difficulty: {self.difficulty}, '\ + f'Size: {self.size}, '\ + f'Lat: {self.lat}, '\ + f'Lon: {self.lon}') + # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 # YOUR CODE HERE +waypoint = Waypoint('Catacombs', 41.70505, -121.51521) +print(waypoint) + # Without changing the following line, how can you make it print into something # more human-readable? Hint: Look up the `object.__str__` method print(waypoint) @@ -25,5 +52,7 @@ # YOUR CODE HERE +geocache = Geocache('Newberry Views', 1.5, 2, 44.052137, -121.41556) + # Print it--also make this print more nicely print(geocache)