diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..26d33521af --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Intro-Python-I.iml b/.idea/Intro-Python-I.iml new file mode 100644 index 0000000000..8b8c395472 --- /dev/null +++ b/.idea/Intro-Python-I.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000..105ce2da2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..d1e22ecb89 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..8e6b599928 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..94a25f7f4c --- /dev/null +++ b/.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..7fad840f26 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..89598afaa6 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 + +print(pow(65536, 2)) diff --git a/src/02_datatypes.py b/src/02_datatypes.py index 245193da34..570d419793 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -13,9 +13,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 +print(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..b6f0dcf5ac 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -19,13 +19,13 @@ import os -# See the docs for the OS module: https://docs.python.org/3.7/library/os.html +# See the docs for the OS module: # Print the current process ID -# YOUR CODE HERE +print(os.getgid()) # Print the current working directory (cwd): -# YOUR CODE HERE +print(os.getcwd()) # Print out your machine's login name -# YOUR CODE HERE +print(os.getlogin()) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..70a7f6bc75 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -11,7 +11,7 @@ # 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!" - +printf x # 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 diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..7385f34664 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -7,23 +7,26 @@ # 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 +#for number in y: +# x.append(number) +x.extend(y) print(x) # Change x so that it is [1, 2, 3, 4, 9, 10] -# YOUR CODE HERE +x.sort() print(x) # Change x so that it is [1, 2, 3, 4, 9, 99, 10] -# YOUR CODE HERE +x.insert(6, 99) print(x) # Print the length of list x -# YOUR CODE HERE +print(x.__len__()) + # Print all the values in x multiplied by 1000 -# YOUR CODE HERE \ No newline at end of file +print() diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..d1ce903ef1 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -34,11 +34,15 @@ 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 number in tuple: + print(number) + + 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? -print_tuple(u) +u = (1) # What needs to be added to make this work? / Answer: int needs to be cast as str +print_tuple(str(u)) diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..995d0bc693 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -12,26 +12,30 @@ 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[4]) # Output the last three elements in the array: [7, 9, 6] -print() +print(a[3], a[4], a[5]) # Output the two middle elements in the array: [1, 7] -print() +mid = slice(2, 4) +print(a[mid]) # Output every element except the first one: [4, 1, 7, 9, 6] -print() +x = slice(1, 5) +print(a[x]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +pop = slice(0, 4) +print(a[pop]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +world = slice(7, 12) +print(s[world]) \ No newline at end of file diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..7760f604bd 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -11,14 +11,14 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] y = [] - -print (y) +for x in range(5): + y.append(x+1) +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) # Write a list comprehension to produce the uppercase version of all the @@ -27,7 +27,8 @@ a = ["foo", "bar", "baz"] y = [] - +for x in a: + y.append(x.upper()) print(y) # Use a list comprehension to create a list containing only the _even_ elements diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..22e9012005 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -34,14 +34,18 @@ ] # Add a new waypoint to the list -# YOUR CODE HERE +theenterprise = {"lat": 12, "lon": 99, "name": "the last place"} +waypoints.append(theenterprise) # 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 +waypoint = waypoints[0] +waypoint["lon"] = -130 +waypoint["name"] = "not a real place" +waypoints[0] = waypoint # Write a loop that prints out all the field values for all the waypoints -# YOUR CODE HERE \ No newline at end of file +for coordinates in waypoints: + print(coordinates) \ No newline at end of file diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..d749d94237 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,12 +1,19 @@ # Write a function is_even that will return true if the passed-in number is even. -# YOUR CODE HERE +def is_even(val): + if val % 2: + return True + else: + return False + +blah = is_even(5) +print(blah) # 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 + diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..7888e1fb67 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(arg1, arg2): + return 1 + 2 print(f1(1, 2)) @@ -12,7 +13,11 @@ # sum. # Note: Google for "python arbitrary arguments" and look for "*args" -# YOUR CODE HERE +def f2(*arg): + result = 0 + for args in arg: + result = result + args + return result print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -22,14 +27,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(arg1, arg2=1): + return arg1 + arg2 + print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -43,18 +50,22 @@ # # Note: Google "python keyword arguments". -# YOUR CODE HERE +def f4(a, b="Null"): + + return "key: \"a\", value: \"b\"" + + # Should print # key: a, value: 12 # key: b, value: 30 -f4(a=12, b=30) +print(f4(a=12, b=30)) # Should print # key: city, value: Berkeley # key: population, value: 121240 # key: founded, value: "March 23, 1868" -f4(city="Berkeley", population=121240, founded="March 23, 1868") +#print(f4(city="Berkeley", population=121240, founded="March 23, 1868")) d = { "monster": "goblin", diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..527536d843 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -9,11 +9,16 @@ # 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+') +data = foo.read() +print(data) +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') +bar.write("This is a test.\n") +bar.close() \ No newline at end of file diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..ac3932b710 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -1,29 +1,45 @@ # 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: + def __init__(self, name, lat, lon): + self.name = name + self.lat = lat + self.lon = 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: + def __init__(self, name, difficulty, size, lat, lon): + self.name = name + self.difficulty = difficulty + self.size = size + self.lat = lat + self.lon = lon # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 -# YOUR CODE HERE +waypoint = Waypoint(name="Catacombs", lat=41.70505, lon=-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(waypoint.name) # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 -# YOUR CODE HERE +geocache = Geocache(name="Newberry Views", difficulty=1.5, size=2, lat=44.052137, lon=-121.41556) # Print it--also make this print more nicely -print(geocache) +print(geocache.name) diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..484ba93ef5 --- /dev/null +++ b/src/bar.txt @@ -0,0 +1 @@ +This is a test. diff --git a/src/foo.txt b/src/foo.txt index 9793c085e1..484ba93ef5 100644 --- a/src/foo.txt +++ b/src/foo.txt @@ -1,6 +1 @@ -Do you bite your thumb at us, sir? -I do bite my thumb, sir. -Do you bite your thumb at us, sir? -No, sir. I do not bite my thumb at you, sir, but I bite my thumb, sir. -Do you quarrel, sir? -Quarrel, sir? No, sir. \ No newline at end of file +This is a test.