diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..02f1b8da04 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1 +1,5 @@ -# Print "Hello, world!" to your terminal \ No newline at end of file +# Print "Hello, world!" to your terminal + +# * SOLUTION: +# * OPEN INTERPRETER && RUN: python3 00_hello.py +print("Hello, world!") \ No newline at end of file diff --git a/src/01_bignum.py b/src/01_bignum.py index c020928d63..2af4b9f57f 100644 --- a/src/01_bignum.py +++ b/src/01_bignum.py @@ -1,4 +1,6 @@ # 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 +# * SOLUTION: +# * OPEN INTERPRETER && RUN: python3 01_bignum.py +print(2**65536) \ No newline at end of file diff --git a/src/02_datatypes.py b/src/02_datatypes.py index 245193da34..dcfb6141d5 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -8,14 +8,27 @@ on a string and an integer. """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 02_datatypes.py + +# ? TYPE() - RETURNS THE TYPE OF VALUE YOU ARE DEALING WITH. +# ? EX: || + x = 5 y = "7" # Write a print statement that combines x + y into the integer value 12 -# YOUR CODE HERE - +# ? INT() - CONVERTS A VALUE TO INTEGERS +print( + x + int(y), + type(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 +# ? STR() - CONVERTS A VALUE TO STRING +print( + str(x + int(y)), + type(str(x + int(y))) +) diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..19ecbc64c0 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -4,28 +4,30 @@ methods, and the os module, which gives you access to lower- level operating system functionality. """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 03_modules.py import sys # 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 argument in dir(sys.argv): print("Argument: ", argument) # Print out the OS platform you're using: -# YOUR CODE HERE +print('PLATFORM :', sys.platform) # Print out the version of Python you're using: -# YOUR CODE HERE - +print('IMPLEMENTATION VERSION INFORMATION', sys.implementation) +print('VERSIONS INFORMATION: ', 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('CURRENT PROCESS ID: ', os.getpid()) # Print the current working directory (cwd): -# YOUR CODE HERE +print('CURRENT WORKING DIRECTORY (CWD): ', os.getcwd()) # Print out your machine's login name -# YOUR CODE HERE +print('CURRENT LOCAL MACHINE LOGIN NAME: ', os.getlogin()) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..5d47f15cca 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -4,6 +4,9 @@ method, and by using f-strings. """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 04_printing.py + x = 10 y = 2.24552 z = "I like turtles!" @@ -11,7 +14,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 %s, y is %s, z is %s ' % (x, y, z)) # Use the 'format' string method to print the same thing +print('x is {0}, y is {1}, z is {2}'.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..04791f66c9 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -6,24 +6,27 @@ # For the following, DO NOT USE AN ASSIGNMENT (=). +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 05_lists.py + # 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.extend(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('LENGTH OF LIST: ', len(x)) # Print all the values in x multiplied by 1000 -# YOUR CODE HERE \ No newline at end of file +for item in x: print(str(item * 1000)) \ No newline at end of file diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..8aa67d5d97 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -27,18 +27,21 @@ def dist(a, b): a = (2, 7) # <-- x,y coordinates stored in tuples b = (-14, 72) +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 06_tuples.py + # 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(param): + if (isinstance(param, tuple)) : + for item in param: print(item) 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..be6d21d397 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -9,29 +9,32 @@ Use Python's slice syntax to achieve the following: """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 07_slices.py + 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[-5::1]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +print(a[0:-1]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +print(s[-6:-1]) \ No newline at end of file diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..6f257f0784 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -10,14 +10,17 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 08_comprehensions.py + +y = [x for x in range(1,6)] 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 +29,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [x.upper() for x in a] print(y) @@ -36,6 +39,6 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] +y = [int(item) for item in x if (int(item) % 2) == 0] print(y) \ No newline at end of file diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..2348c7b539 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -15,6 +15,9 @@ - name: a name string for this location """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 09_dictionaries.py + waypoints = [ { "lat": 43, @@ -34,14 +37,27 @@ ] # Add a new waypoint to the list -# YOUR CODE HERE +waypoints.append({ + "lat": 9999, + "lon": -10000, + "name": "DEEPEST HOLE ON EARTH" +}) # 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 +ref = waypoints[0] + +ref["name"] = "not a real place" +ref["lon"] = -130 + +print(waypoints) # 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 value in range(len(waypoints)): + for key in waypoints[value]: + print(F'Obecjet Number: {value} ({key} : {waypoints[value][key]})') diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..26c66ff594 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,6 +1,17 @@ # Write a function is_even that will return true if the passed-in number is even. -# YOUR CODE HERE +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 10_functions.py + + +def is_even(number): + if(int(number) % 2) == 0: + print('Even number') + return True + elif(int(number) % 2) != 0: + print('Odd number') + return False + # Read a number from the keyboard num = input("Enter a number: ") @@ -8,5 +19,4 @@ # Print out "Even!" if the number is even. Otherwise print "Odd" -# YOUR CODE HERE - +is_even(num) diff --git a/src/11_args.py b/src/11_args.py index 39a694ec94..d842150563 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -4,7 +4,11 @@ # 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 +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 11_args.py + +def f1(n1, n2): + if(n1 or n2) != 0: return (n1 + n2) print(f1(1, 2)) @@ -12,7 +16,7 @@ # sum. # Note: Google for "python arbitrary arguments" and look for "*args" -# YOUR CODE HERE +def f2(*n): print(sum(n)) print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -22,14 +26,16 @@ 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(param1, param2 = False): + if (param1 and param2): return param1 + param2 + elif (param2) != True: return param1 + 1 print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -43,7 +49,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 +70,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..348debffbb 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -2,9 +2,14 @@ # Good reading: https://www.programiz.com/python-programming/global-local-nonlocal-variables # When you use a variable in a function, it's local in scope to the function. + +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 12_scopes.py + x = 12 def change_x(): + global x x = 99 change_x() @@ -19,6 +24,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..5ff7edd0b2 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -5,15 +5,27 @@ https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 13_file_io.py + # Open up the "foo.txt" file (which already exists) for reading # 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 +foo = open('foo.txt', 'r+') + +for line in foo: print(line) + +foo.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 +bar = open('bar.txt', 'w+') + +for line in range(3): + bar.write(F'Line content for line number: {line}. \n') + +bar.close() \ No newline at end of file diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..32655ee60c 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -27,6 +27,23 @@ it should use today’s date to get the month and year. """ +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 14_cal.py + import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime + +arg = sys.argv + +def cal( + month=datetime.now().month, + year=datetime.now().year +): print( + calendar.month(int(year), (int(month))) +) + +if (len(arg)) > 3: print('Enter valid information') +elif len(arg) == 2: cal(month=arg[1]) +elif len(arg) == 3: cal(arg[1], arg[2]) +else: cal() \ No newline at end of file diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..643021a306 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -1,21 +1,50 @@ # Make a class LatLon that can be passed parameters `lat` and `lon` to the # constructor -# YOUR CODE HERE +# * SOLUTIONS BELOW: +# * OPEN INTERPRETER && RUN: python3 15_classes.py + + +class LatLon(): + def __init__(self, lat, lon): + self.lon = lon + self.lat = lat # 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'Waypoint 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'{self.name}, diff {self.difficulty}, size {self.size}, {self.lat}, {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 +52,10 @@ # 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..aed31555f1 --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,3 @@ +Line content for line number: 0. +Line content for line number: 1. +Line content for line number: 2.