From 92884f0df4d3886cd4bdcfcb77a6aa5f51b9a076 Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 12:48:56 +0000 Subject: [PATCH 1/9] hello, bignum and datatypes completed --- src/00_hello.py | 4 +++- src/01_bignum.py | 4 +++- src/02_datatypes.py | 4 +++- src/03_modules.py | 7 ++++++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..85a0e88a62 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..bb21784ef4 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 +# 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..6f039abcd2 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -14,8 +14,10 @@ # 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 diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..1663019fe3 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -5,20 +5,25 @@ level operating system functionality. """ +import os 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 a in sys.argv: + print(a) # Print out the OS platform you're using: # YOUR CODE HERE +print(sys.platform) # 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 From 5c608711ff5c7b114dd71220000cb36ca766a32a Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 13:03:04 +0000 Subject: [PATCH 2/9] modules completed --- src/03_modules.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/03_modules.py b/src/03_modules.py index 1663019fe3..afcc452fb8 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -5,6 +5,7 @@ level operating system functionality. """ + import os import sys @@ -28,9 +29,13 @@ # Print the current process ID # YOUR CODE HERE +print(os.getpid()) # 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()) + From d3c4768f3cd7c10ea592e2e94b61c7cb4026845e Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 13:21:11 +0000 Subject: [PATCH 3/9] printing completed --- src/04_printing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..f91da85520 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 and z is "%s"' % (x, y, z)) # Use the 'format' string method to print the same thing +print('x is {:d}, y is {:f} and z is "{:s}"'.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} and z is "{z}"') \ No newline at end of file From 3d031c0168e8f1a67be9fba564d0937b27d3232e Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 13:34:00 +0000 Subject: [PATCH 4/9] lists completed --- src/05_lists.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..5a05ff7d37 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 +for n in y: + x.append(n) 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 all the values in x multiplied by 1000 -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +for a in x: + print(a*1000) From fb11849a77ec59f253f75831eb0e1615ab46612d Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 14:00:41 +0000 Subject: [PATCH 5/9] tuples completed --- src/06_tuples.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..ae6ebf1397 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -35,10 +35,13 @@ def dist(a, b): # Write a function `print_tuple` that prints all the values in a tuple # YOUR CODE HERE +def print_tuple(v): + for n in v: + print(n) 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 64b570178119657c0bb31d0fe86ebe17f9052bce Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 14:31:16 +0000 Subject: [PATCH 6/9] slices completed --- src/07_slices.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..14dd6a4056 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -1,7 +1,7 @@ """ -Python exposes a terse and intuitive syntax for performing +Python exposes a terse and intuitive syntax for performing slicing on lists and strings. This makes it easy to reference -only a portion of a list or string. +only a portion of a list or string. This Stack Overflow answer provides a brief but thorough overview: https://stackoverflow.com/a/509295 @@ -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() +l = int(len(a)/2) +print(a[l-1:l+1]) # Output every element except the first one: [4, 1, 7, 9, 6] -print() +print(a[1:len(a)]) # 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]) From 5c3c4504e970818b05d01ffa84dd32b7c6dd3090 Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 14:50:12 +0000 Subject: [PATCH 7/9] comprehensions completed --- src/08_comprehensions.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..94dde98bea 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -11,14 +11,16 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] y = [] - +for x in range(1, 6): + y.append(x) 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 = [] - +for x in range(10): + y.append(x**3) print(y) # Write a list comprehension to produce the uppercase version of all the @@ -27,7 +29,8 @@ a = ["foo", "bar", "baz"] y = [] - +for w in a: + y.append(w.upper()) print(y) # Use a list comprehension to create a list containing only the _even_ elements @@ -37,5 +40,7 @@ # What do you need between the square brackets to make it work? y = [] - +for n in x: + if int(n) % 2 == 0: + y.append(int(n)) print(y) \ No newline at end of file From 5fb44f21bd3b97b7f2488903f6c853edc0978e8d Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 15:39:18 +0000 Subject: [PATCH 8/9] dictionaries completed --- src/09_dictionaries.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..101b08a46a 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -35,6 +35,11 @@ # Add a new waypoint to the list # YOUR CODE HERE +waypoints.append({ + "lat": 55, + "lon": -120, + "name": "some random place" +}) # 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 +47,11 @@ # waypoints list. # YOUR CODE HERE +waypoints[0]['name'] = 'not a real place' +waypoints[0]['lon'] = -130 # 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 w in waypoints: + for v in w.values(): + print(v) \ No newline at end of file From f8f80949c93d3b7fc841f4965d91307fa4743ad2 Mon Sep 17 00:00:00 2001 From: EmilyAbrahart Date: Mon, 10 Feb 2020 15:45:34 +0000 Subject: [PATCH 9/9] functions completed and general file formatting --- src/00_hello.py | 2 +- src/01_bignum.py | 2 +- src/02_datatypes.py | 2 +- src/03_modules.py | 1 - src/04_printing.py | 2 +- src/06_tuples.py | 4 +++- src/08_comprehensions.py | 4 ++-- src/09_dictionaries.py | 2 +- src/10_functions.py | 11 ++++++++++- 9 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/00_hello.py b/src/00_hello.py index 85a0e88a62..8913f3d0f8 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1,3 +1,3 @@ # Print "Hello, world!" to your terminal -print("Hello, world!") \ No newline at end of file +print("Hello, world!") diff --git a/src/01_bignum.py b/src/01_bignum.py index bb21784ef4..a3f6f18716 100644 --- a/src/01_bignum.py +++ b/src/01_bignum.py @@ -3,4 +3,4 @@ # YOUR CODE HERE -print(2**65536) \ No newline at end of file +print(2**65536) diff --git a/src/02_datatypes.py b/src/02_datatypes.py index 6f039abcd2..216316f64a 100644 --- a/src/02_datatypes.py +++ b/src/02_datatypes.py @@ -20,4 +20,4 @@ # Write a print statement that combines x + y into the string value 57 # YOUR CODE HERE -print(str(x) + y) \ No newline at end of file +print(str(x) + y) diff --git a/src/03_modules.py b/src/03_modules.py index afcc452fb8..9d54e39e7e 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -38,4 +38,3 @@ # 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 f91da85520..27fa3af170 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -17,4 +17,4 @@ print('x is {:d}, y is {:f} and z is "{:s}"'.format(x, y, z)) # Finally, print the same thing using an f-string -print(f'x is {x}, y is {y} and z is "{z}"') \ No newline at end of file +print(f'x is {x}, y is {y} and z is "{z}"') diff --git a/src/06_tuples.py b/src/06_tuples.py index ae6ebf1397..53ef629aa3 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -17,6 +17,7 @@ import math + def dist(a, b): """Compute the distance between two x,y points.""" x0, y0 = a # Destructuring assignment @@ -24,6 +25,7 @@ def dist(a, b): return math.sqrt((x1 - x0)**2 + (y1 - y0)**2) + a = (2, 7) # <-- x,y coordinates stored in tuples b = (-14, 72) @@ -31,7 +33,6 @@ def dist(a, b): print("Distance is: {:.2f}".format(dist(a, b))) - # Write a function `print_tuple` that prints all the values in a tuple # YOUR CODE HERE @@ -39,6 +40,7 @@ def print_tuple(v): for n in v: print(n) + t = (1, 2, 5, 7, 99) print_tuple(t) # Prints 1 2 5 7 99, one per line diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 94dde98bea..1736b96eb2 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -13,7 +13,7 @@ y = [] for x in range(1, 6): y.append(x) -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] @@ -43,4 +43,4 @@ for n in x: if int(n) % 2 == 0: y.append(int(n)) -print(y) \ No newline at end of file +print(y) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index 101b08a46a..72108f454f 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -54,4 +54,4 @@ # YOUR CODE HERE for w in waypoints: for v in w.values(): - print(v) \ No newline at end of file + print(v) diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..eb8798f662 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -2,6 +2,12 @@ # YOUR CODE HERE + +def is_even(n): + if n % 2 == 0: + return True + + # Read a number from the keyboard num = input("Enter a number: ") num = int(num) @@ -9,4 +15,7 @@ # Print out "Even!" if the number is even. Otherwise print "Odd" # YOUR CODE HERE - +if is_even(num): + print("Even!") +else: + print("Odd!")