diff --git a/src/00_hello.py b/src/00_hello.py index 268998dfc7..e79cee217a 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..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 diff --git a/src/03_modules.py b/src/03_modules.py index 97eba053c7..78cb63e93b 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -9,23 +9,24 @@ # 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 x in range(len(sys.argv)): + print(sys.argv[x]) # 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 -# YOUR CODE HERE +print(os.getppid()) # 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..041f5aeeaa 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -8,10 +8,15 @@ y = 2.24552 z = "I like turtles!" +print('x is ' + str(x), ' y is ' + str(y), ' z is ' + z, sep=',') # 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 % 1d, y is % 2d, z is "% s"' %(x, y, z)) # 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 +print('x is {}, y is {}, z is "{}"'.format(x, y, z)) + +# 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..29f52cdc92 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -7,23 +7,25 @@ # 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 i in y: + x.append(i) 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(-1, 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 x: + print(i * 1000) \ No newline at end of file diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..134c463246 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -34,11 +34,14 @@ def dist(a, b): # Write a function `print_tuple` that prints all the values in a tuple -# YOUR CODE HERE +def print_tuple(data): + for i in range(1, len(data)): + print(data[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? -print_tuple(u) +print_tuple(u) #1 element tuple in python requires a comma diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..93ec19fb76 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[:-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..5f53e305c2 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -11,13 +11,16 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] y = [] - +for i in range(1,6): + y.append(i) 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 i in range(10): + y.append(i) print(y) @@ -27,6 +30,8 @@ a = ["foo", "bar", "baz"] y = [] +for i in a: + y.append(i.upper()) print(y) @@ -34,8 +39,10 @@ # the user entered into list x. x = input("Enter comma-separated numbers: ").split(',') - # What do you need between the square brackets to make it work? y = [] +for i in x: + if i % 2 == 0: + y.append(i) -print(y) \ No newline at end of file +print(y) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..e80121f750 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -34,14 +34,24 @@ ] # Add a new waypoint to the list -# YOUR CODE HERE +waypoints.append({ + "lat": 22, + "lon": -166, + "name": "a different place" +}) + +print(waypoints) # 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" + +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 +for i in waypoints: + print(i) \ No newline at end of file diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..225e2f4cd7 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,6 +1,10 @@ # Write a function is_even that will return true if the passed-in number is even. -# 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: ") @@ -8,5 +12,5 @@ # Print out "Even!" if the number is even. Otherwise print "Odd" -# YOUR CODE HERE +print(is_even(num)) diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..f6104327dc 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -4,7 +4,9 @@ # 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(num1, num2): + return num1 + num2 + print(f1(1, 2)) @@ -12,7 +14,11 @@ # sum. # Note: Google for "python arbitrary arguments" and look for "*args" -# YOUR CODE HERE +def f2(*num): + total = 0 + for i in num: + total += i + return total print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -22,6 +28,12 @@ a = [7, 6, 5, 4] # How do you have to modify the f2 call below to make this work? +def f2(num): + total = 0 + for i in num: + total += i + return total + print(f2(a)) # Should print 22 # Write a function f3 that accepts either one or two arguments. If one argument, @@ -29,7 +41,8 @@ # arguments. # Note: Google "python default arguments" for a hint. -# YOUR CODE HERE +def f3(num1, num2=1): + return num1 + num2 print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -43,7 +56,9 @@ # # Note: Google "python keyword arguments". -# YOUR CODE HERE +def f4(**kwargs): + for key in kwargs: + print(f"key: {key}, value: {kwargs[key]}") # Should print # key: a, value: 12 @@ -62,4 +77,7 @@ } # How do you have to modify the f4 call below to make this work? +def f4(kwargs): + for key in kwargs: + print(f"key: {key}, value: {kwargs[key]}") f4(d)