diff --git a/src/.idea/.gitignore b/src/.idea/.gitignore new file mode 100644 index 0000000000..5c98b42884 --- /dev/null +++ b/src/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/src/.idea/inspectionProfiles/profiles_settings.xml b/src/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000..105ce2da2d --- /dev/null +++ b/src/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml new file mode 100644 index 0000000000..6649a8c6e4 --- /dev/null +++ b/src/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml new file mode 100644 index 0000000000..f669a0e594 --- /dev/null +++ b/src/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/.idea/src.iml b/src/.idea/src.iml new file mode 100644 index 0000000000..8b8c395472 --- /dev/null +++ b/src/.idea/src.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml new file mode 100644 index 0000000000..6c0b863585 --- /dev/null +++ b/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..a2f00d41ec 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1 +1,3 @@ -# 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..7d7a3e1966 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -12,10 +12,10 @@ y = "7" # Write a print statement that combines x + y into the integer value 12 - +print(x+int(y)) # YOUR CODE HERE # Write a print statement that combines x + y into the string value 57 - +print(f"{str(x)}{y}") # YOUR CODE HERE \ No newline at end of file diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..32b22563a7 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -6,26 +6,34 @@ """ import sys +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 +for arg in sys.argv[1:]: + print(arg) # Print out the OS platform you're using: # YOUR CODE HERE +print(f"Operating system: {os.name}") + # Print out the version of Python you're using: # YOUR CODE HERE +print(f"Python Version: {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 +print(f"Process ID: {os.getpid()}") # Print the current working directory (cwd): # YOUR CODE HERE +print(f"Current directory: {os.getcwd()}") # Print out your machine's login name # YOUR CODE HERE +print(f"Machine Login: {os.getlogin()}") diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..990c0b0428 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -11,7 +11,10 @@ # Using the printf operator (%), print the following feeding in the values of x, # y, and z: # x is 10, y is 2.25, z is "I like turtles!" +print("x is %d, y is %f, z is %s" % (10, 2.25, "I like turtles!")) # Use the 'format' string method to print the same thing +print("x is {}, y is {}, z is {}".format(10, 2.25, "I like turtles!")) -# 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 {10}, y is {2.25}, z is I like turtles!") \ No newline at end of file diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..f928832e1e 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -7,23 +7,30 @@ # For the following, DO NOT USE AN ASSIGNMENT (=). # Change x so that it is [1, 2, 3, 4] +x.append(4) # YOUR CODE HERE print(x) # Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10] +x.extend(y) # YOUR CODE HERE print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] +x.remove(8) # YOUR CODE HERE print(x) # Change x so that it is [1, 2, 3, 4, 9, 99, 10] +x.insert(5, 99) # YOUR CODE HERE print(x) # Print the length of list x # YOUR CODE HERE +print(len(x)) # Print all the values in x multiplied by 1000 +for y in x: + print(y*1000) # YOUR CODE HERE \ No newline at end of file diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..2780cac3ca 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -17,28 +17,32 @@ import math + def dist(a, b): """Compute the distance between two x,y points.""" x0, y0 = a # Destructuring assignment x1, y1 = b - return math.sqrt((x1 - x0)**2 + (y1 - y0)**2) + return math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2) + -a = (2, 7) # <-- x,y coordinates stored in tuples +a = (2, 7) # <-- x,y coordinates stored in tuples b = (-14, 72) # Prints "Distance is 66.94" print("Distance is: {:.2f}".format(dist(a, b))) - # Write a function `print_tuple` that prints all the values in a tuple - # YOUR CODE HERE +def print_tuple(x): + for tuplet in x: + print(tuplet) + 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..3e5768644e 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:2]) # Output the second-to-last element: 9 -print() +print(a[-2:-1]) # 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[:-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]) \ No newline at end of file diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..11f3862349 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 = [x for x in range(1,6)] -print (y) +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 (0,10)] print(y) @@ -26,7 +26,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [x.upper() for x 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 = [eve for eve in x if int(eve) % 2 == 0] print(y) \ No newline at end of file diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..16488e788e 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -35,6 +35,13 @@ # Add a new waypoint to the list # YOUR CODE HERE +x = { + "lat": 55, + "lon": -132, + "name": "a fourth place" +} + +waypoints.append(x) # Modify the dictionary with name "a place" such that its longitude # value is -130 and change its name to "not a real place" @@ -42,6 +49,15 @@ # waypoints list. # YOUR CODE HERE +replacement = {'lon': -130, + 'name': 'not a real place' + } + +waypoints[0].update(replacement) + # 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 dicts in waypoints: + for key, value in dicts.items(): + print(f"{key}\'s value: {value}") diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..e50707a62b 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -2,11 +2,28 @@ # YOUR CODE HERE + +def is_even(x): + if x % 2 == 0: + return True + else: + return False + + # Read a number from the keyboard num = input("Enter a number: ") num = int(num) + # Print out "Even!" if the number is even. Otherwise print "Odd" # YOUR CODE HERE +def tester(x): + test_even = is_even(x) + + if test_even: + print("Even!") + else: + print("Odd") +tester(num) \ No newline at end of file diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..28c4948f2a 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -5,24 +5,31 @@ # the sum. This is what you'd consider to be a regular, normal function. # YOUR CODE HERE +def f1(x, y): + return x + y + print(f1(1, 2)) # Write a function f2 that takes any number of integer arguments and returns the + # sum. # Note: Google for "python arbitrary arguments" and look for "*args" - # YOUR CODE HERE +def f2(*argv): + return sum(arg for arg in argv) + -print(f2(1)) # Should print 1 -print(f2(1, 3)) # Should print 4 -print(f2(1, 4, -12)) # Should print -7 +print(f2(1)) # Should print 1 +print(f2(1, 3)) # Should print 4 +print(f2(1, 4, -12)) # Should print -7 print(f2(7, 9, 1, 3, 4, 9, 0)) # Should print 33 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 @@ -30,9 +37,12 @@ # Note: Google "python default arguments" for a hint. # YOUR CODE HERE +def f3(x, y=1): + return x + y + print(f3(1, 2)) # Should print 3 -print(f3(8)) # Should print 9 +print(f3(8)) # Should print 9 # Write a function f4 that accepts an arbitrary number of keyword arguments and @@ -44,6 +54,10 @@ # 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..ab1e84ed51 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -4,13 +4,17 @@ # When you use a variable in a function, it's local in scope to the function. x = 12 + def change_x(): x = 99 + print(x) + change_x() + # This prints 12. What do we have to modify in change_x() to get it to print 99? -print(x) +# print(x) # This nested function has a similar problem. @@ -20,7 +24,7 @@ def outer(): def inner(): y = 999 - + print(y) inner() # This prints 120. What do we have to change in inner() to get it to print diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..32339e0cef 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -11,9 +11,33 @@ # YOUR CODE HERE +with open('foo.txt', 'r') as f: + print(f.read()) + + # 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 +# write three random lines +text = """ +Hi there. +Hi. +Bye.""" + +# open bar.txt (new) +with open('bar.txt', 'w') as f: + + f.write(text) + + # close file + f.close() + + +# open file +with open('bar.txt', 'r') as f: + # read file + print(f.read()) + diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..31a80745a9 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -29,4 +29,18 @@ import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime + + +def program(month=5, year=2020): + calendar.prmonth(year, month) + + +if len(sys.argv) == 1: + program() +elif len(sys.argv) == 2: + program(int(sys.argv[1])) +elif len(sys.argv) == 3: + program(int(sys.argv[1]), int(sys.argv[2])) +else: + print("Unexpected input. Expected input: 14_cal.py [month] [year]") diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..c267af5c7c 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -1,29 +1,52 @@ # 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): + super().__init__(lat, lon) + self.name = name + + def __str__(self): + return f'A Waypoint named {self.name} at latitude {self.lat} and longitude {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'A Geocache named {self.name} with a difficulty of {self.difficulty}, a size of {self.size} at latitude {self.lat} and longitude {self.lon}' + # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 # YOUR CODE HERE +x = 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 -print(waypoint) +print(x) # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 # YOUR CODE HERE +y = Geocache('Newberry Views', 1.5, 2, 44.052137, -121.41556) # Print it--also make this print more nicely -print(geocache) +print(y) diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..bec719689f --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,4 @@ + +Hi there. +Hi. +Bye. \ No newline at end of file