From 6ca3adf05b3469008cdeda8873756ebc747a4617 Mon Sep 17 00:00:00 2001 From: Ash827 Date: Tue, 1 Sep 2020 23:53:08 -0400 Subject: [PATCH 1/6] getting started --- src/00_hello.py | 3 ++- src/01_bignum.py | 3 ++- src/02_datatypes.py | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..d4f86bc874 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1 +1,2 @@ -# Print "Hello, world!" to your terminal \ No newline at end of file +# Print "Hello, world!" to your terminal +print("Hello, world!") \ No newline at end of file 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..7f3296215c 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(x + int(y)) # 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(str(x) + y) \ No newline at end of file From 5178d21b37104c31223a379981df619d1d683c1a Mon Sep 17 00:00:00 2001 From: Ash827 Date: Wed, 2 Sep 2020 22:09:53 -0400 Subject: [PATCH 2/6] first half complete! --- src/03_modules.py | 19 ++++++++++++------- src/04_printing.py | 4 +++- src/05_lists.py | 13 +++++++------ src/06_tuples.py | 6 ++++-- src/07_slices.py | 14 +++++++------- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..ede052698a 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -9,23 +9,28 @@ # 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 + +print(sys.argv[0]) # Print out the OS platform you're using: -# YOUR CODE HERE +import platform +print(platform.system()) # Print out the version of Python you're using: -# YOUR CODE HERE - +print(sys.version) 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 +pid = os.getpid() +print(pid) # Print the current working directory (cwd): -# YOUR CODE HERE +path = os.getcwd() + +print(path) # Print out your machine's login name -# YOUR CODE HERE +import getpass +print(getpass.getuser()) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..2c8af16462 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -13,5 +13,7 @@ # x is 10, y is 2.25, z is "I like turtles!" # Use the 'format' string method to print the same thing +print("x is {}, y is {}, z is {}". format(x, y, z)) -# Finally, print the same thing using an f-string \ No newline at end of file +# Finally, print the same thing using an f-string +print(f"x is {x}, y is {y}, z is {z}") \ No newline at end of file diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..2a823fee17 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -7,23 +7,24 @@ # For the following, DO NOT USE AN ASSIGNMENT (=). # 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 = x + y print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] -# YOUR CODE HERE +x.pop(4) 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 all the values in x multiplied by 1000 -# YOUR CODE HERE \ No newline at end of file +for i in range(len(x)): + print(x[i]*1000) \ No newline at end of file diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..5c565921bf 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(tup): + for i in tup: + print(i) 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) diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..cf48116a62 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -12,26 +12,26 @@ 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() +print(a[:5]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +print(s[7:12]) \ No newline at end of file From d557a7c7eda71815fe75509ef47d18d27a3002da Mon Sep 17 00:00:00 2001 From: Ash827 Date: Sun, 6 Sep 2020 22:23:33 -0400 Subject: [PATCH 3/6] almost done --- src/08_comprehensions.py | 8 ++++---- src/09_dictionaries.py | 12 +++++++++--- src/10_functions.py | 9 +++++++-- src/11_args.py | 26 ++++++++++++++++++++------ src/12_scopes.py | 2 ++ 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..744c7cb49e 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -10,14 +10,14 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +y = [1, 2, 3, 4, 5] print (y) # 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 = [x**3 for x in range(10)] print(y) @@ -26,7 +26,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [str.upper() for str in a] print(y) @@ -36,6 +36,6 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] +y = [int(n) for n in x if int(n) % 2 == 0] print(y) \ No newline at end of file diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..374f181cb9 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -34,14 +34,20 @@ ] # Add a new waypoint to the list -# YOUR CODE HERE + +waypoints.append({"lat": 44, "lon": -133, "name": "my place"}) # 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 +for dict in waypoints: + for key, value in zip(dict.keys(), dict.values()): + print(f'{key}: {value}') + +print(waypoints) \ No newline at end of file diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..ac9d4efb1d 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(n): + if n % 2 == 0: + return True # Read a number from the keyboard num = input("Enter a number: ") @@ -8,5 +10,8 @@ # Print out "Even!" if the number is even. Otherwise print "Odd" -# YOUR CODE HERE +if is_even(num) == True: + print("Even!") +else: + print("Odd") diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..ae0926e424 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -4,7 +4,8 @@ # Write a function f1 that takes two integer positional arguments and returns # the sum. This is what you'd consider to be a regular, normal function. -# YOUR CODE HERE +def f1(x, n): + return x + n print(f1(1, 2)) @@ -12,7 +13,11 @@ # sum. # Note: Google for "python arbitrary arguments" and look for "*args" -# YOUR CODE HERE +def f2(*argv): + x = 0 + for n in argv: + x += n + return x print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -22,14 +27,21 @@ 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 # arguments. # Note: Google "python default arguments" for a hint. -# YOUR CODE HERE +def f3(*argv): + x = 0 + for i in argv: + x += i + if x == argv[0]: + return argv[0] + 1 + else: + return x print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -43,7 +55,9 @@ # # Note: Google "python keyword arguments". -# YOUR CODE HERE +def f4(**kwargs): + for key, value in kwargs.items(): + print(f'key: {key}, value: {value}') # Should print # key: a, value: 12 @@ -62,4 +76,4 @@ } # How do you have to modify the f4 call below to make this work? -f4(d) +f4(**d) diff --git a/src/12_scopes.py b/src/12_scopes.py index bc467fa423..adc77a4549 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -5,6 +5,7 @@ x = 12 def change_x(): + global x x = 99 change_x() @@ -19,6 +20,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() From 78d524fdabceeb41ef8bbba74b6ec1bc6ac47b1e Mon Sep 17 00:00:00 2001 From: Ash827 Date: Sun, 6 Sep 2020 22:35:09 -0400 Subject: [PATCH 4/6] almost done --- src/08_comprehensions.py | 2 ++ src/09_dictionaries.py | 1 + src/10_functions.py | 1 + src/11_args.py | 2 +- src/12_scopes.py | 1 + src/13_file_io.py | 2 +- 6 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 744c7cb49e..2b53299c99 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -17,6 +17,8 @@ # Write a list comprehension to produce the cubes of the numbers 0-9: # [0, 1, 8, 27, 64, 125, 216, 343, 512, 729] +#It worked :) Woot! + y = [x**3 for x in range(10)] print(y) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index 374f181cb9..8c1ebfb5c0 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -34,6 +34,7 @@ ] # Add a new waypoint to the list +# Append! Append! waypoints.append({"lat": 44, "lon": -133, "name": "my place"}) diff --git a/src/10_functions.py b/src/10_functions.py index ac9d4efb1d..6f89246d10 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,4 +1,5 @@ # Write a function is_even that will return true if the passed-in number is even. +# n % 2 ==0 checks for an even number. If it is divided by 2 and doesn't have a remainer, print True! def is_even(n): if n % 2 == 0: diff --git a/src/11_args.py b/src/11_args.py index ae0926e424..5fcf06e904 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -49,7 +49,7 @@ def f3(*argv): # Write a function f4 that accepts an arbitrary number of keyword arguments and # prints out the keys and values like so: -# +## # key: foo, value: bar # key: baz, value: 12 # diff --git a/src/12_scopes.py b/src/12_scopes.py index adc77a4549..45c268307b 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -15,6 +15,7 @@ def change_x(): # This nested function has a similar problem. +# def outer(): y = 120 diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..ca161c9e43 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -9,7 +9,7 @@ # Print all the contents of the file, then close the file # Note: pay close attention to your current directory when trying to open "foo.txt" -# YOUR CODE HERE + # Open up a file called "bar.txt" (which doesn't exist yet) for # writing. Write three lines of arbitrary content to that file, From 9b99bd32772c7cb0af4c36f6ea0bc95e93eba362 Mon Sep 17 00:00:00 2001 From: Ash827 Date: Sun, 6 Sep 2020 22:56:01 -0400 Subject: [PATCH 5/6] finished assignment --- src/13_file_io.py | 18 +++++++++++++++++- src/14_cal.py | 17 ++++++++++++++++- src/15_classes.py | 38 +++++++++++++++++++++++++++++++++----- src/bar.txt | 5 +++++ 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 src/bar.txt diff --git a/src/13_file_io.py b/src/13_file_io.py index ca161c9e43..c7b5c5659f 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -9,11 +9,27 @@ # Print all the contents of the file, then close the file # Note: pay close attention to your current directory when trying to open "foo.txt" +with open('foo.txt') as f: + foo = f.read() +print(foo) # 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 +with open('bar.txt', 'w+') as f: + f.write( + ''' + This is a line + So is this + And away we go! + ''' + ) + f.close() + +with open('bar.txt') as f: + bar = f.read() + +print(bar) \ No newline at end of file diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..2a86abcd57 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -29,4 +29,19 @@ import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime + +args = [sys.argv[i] for i in list(range(len(sys.argv)))][1:] + +today = datetime.today() +year = today.year +month = today.month + +if len(args) == 0: + print(calendar.month(year, month)) +elif (len(args) == 1) and (len(args[0]) <= 2): + print(calendar.month(year, int(args[0]))) +elif len(args[1]) != 4: + print("Error: Arguements must be in form of 'python 14_cal.py () ()'") +else: + print(calendar.month(int(args[1]), int(args[0]))) \ No newline at end of file diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..bf5b2786a9 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -1,21 +1,49 @@ # Make a class LatLon that can be passed parameters `lat` and `lon` to the # constructor -# 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 __repr__(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(LatLon): + + def __init__(self, name, difficulty, size, lat, lon): + self.difficulty = difficulty + self.size = size + self.name = name + super().__init__(lat, lon) + + def __repr__(self): + return f''' + Name: {self.name} + Difficulty: {self.difficulty} + Size: {self.size} + Lat: {self.lat} + 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) # Without changing the following line, how can you make it print into something # more human-readable? Hint: Look up the `object.__str__` method @@ -23,7 +51,7 @@ # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 -# YOUR CODE HERE +geocache = Geocache("Newberry Views", 1.5, 2, 44.052137, -121.41556) # Print it--also make this print more nicely print(geocache) diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..03b55d5a9b --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,5 @@ + + This is a line + So is this + And away we go! + \ No newline at end of file From 83dd03ff0c53e660510a7429e04e86b6a1a146a5 Mon Sep 17 00:00:00 2001 From: Ashley Gaskins Date: Tue, 8 Sep 2020 21:56:27 -0400 Subject: [PATCH 6/6] Update 14_cal.py --- src/14_cal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/14_cal.py b/src/14_cal.py index 2a86abcd57..d834593308 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -42,6 +42,6 @@ elif (len(args) == 1) and (len(args[0]) <= 2): print(calendar.month(year, int(args[0]))) elif len(args[1]) != 4: - print("Error: Arguements must be in form of 'python 14_cal.py () ()'") + print("Error: Arguments must be in form of 'python 14_cal.py () ()'") else: - print(calendar.month(int(args[1]), int(args[0]))) \ No newline at end of file + print(calendar.month(int(args[1]), int(args[0])))