-
Notifications
You must be signed in to change notification settings - Fork 0
Kyle teeter #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Kyle teeter #1
Changes from all commits
e5a8634
643bcd4
822e370
dc978d0
cb088fa
470613f
5612bfc
af5d04a
745ab9c
3bc774d
114848c
8252184
4140ec2
55a0235
2a1fd9c
f30d7c4
845b5ee
f792f00
595a316
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| # Print "Hello, world!" to your terminal | ||
| # Print "Hello, world!" to your terminal | ||
|
|
||
| message = "Hello, world!" | ||
| print(message) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Print out 2 to the 65536 power | ||
| # (try doing the same thing in the JS console and see what it outputs) | ||
|
|
||
| # YOUR CODE HERE | ||
| print(2**65536) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,13 @@ | |
| # 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!" | ||
| yRound = round(y, 2) | ||
| print("x is %s, y is %s, z is %s" % (x, yRound, z)) | ||
|
|
||
| # Use the 'format' string method to print the same thing | ||
|
|
||
| print ("x is {}, y is {}, z is {}".format(x, yRound, z)) | ||
|
|
||
| # Finally, print the same thing using an f-string | ||
| # Finally, print the same thing using an f-string | ||
|
|
||
| print (f"x is {x}, y is {yRound}, z is {z}") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in this module. The last printing exercise should be like the following: |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,22 +8,28 @@ | |
|
|
||
| # 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 | ||
| del x[4] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also use: |
||
| 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 | ||
| # YOUR CODE HERE | ||
| for val in x : | ||
| print(val*1000) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,8 +37,13 @@ def dist(a, b): | |
| # YOUR CODE HERE | ||
|
|
||
| t = (1, 2, 5, 7, 99) | ||
| def print_tuple(t) : | ||
| for item in t: | ||
| print(item) | ||
| 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| def prints_tuple(u): | ||
| print(u) # What needs to be added to make this work? | ||
| prints_tuple(u) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,26 +12,33 @@ | |
| a = [2, 4, 1, 7, 9, 6] | ||
|
|
||
| # Output the second element: 4: | ||
| print() | ||
| y= a[1:2] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| print(y) | ||
|
|
||
| # Output the second-to-last element: 9 | ||
| print() | ||
| x= a[4:5] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| print(x) | ||
|
|
||
| # Output the last three elements in the array: [7, 9, 6] | ||
| print() | ||
| z = a[3:] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| print(z) | ||
|
|
||
| # Output the two middle elements in the array: [1, 7] | ||
| print() | ||
| w = a[2:4] | ||
| print(w) | ||
|
|
||
| # Output every element except the first one: [4, 1, 7, 9, 6] | ||
| print() | ||
| r = a[1:] | ||
| print(r) | ||
|
|
||
| # Output every element except the last one: [2, 4, 1, 7, 9] | ||
| print() | ||
| t = a[:5] | ||
| print(t) | ||
|
|
||
| # For string s... | ||
|
|
||
| s = "Hello, world!" | ||
|
|
||
| # Output just the 8th-12th characters: "world" | ||
| print() | ||
| v = s[7:12] | ||
| print(v) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,21 +3,21 @@ | |
| They essentially act as a terse and concise way of initializing | ||
| and populating a list given some expression that specifies how | ||
| the list should be populated. | ||
|
|
||
| Take a look at https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions | ||
| for more info regarding list comprehensions. | ||
| """ | ||
|
|
||
| # Write a list comprehension to produce the array [1, 2, 3, 4, 5] | ||
|
|
||
| y = [] | ||
| y = [x+1 for x in range(5)] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| 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 +26,7 @@ | |
|
|
||
| a = ["foo", "bar", "baz"] | ||
|
|
||
| y = [] | ||
| y = [str(a[x]).upper() for x in range(len(a))] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| print(y) | ||
|
|
||
|
|
@@ -36,6 +36,10 @@ | |
| x = input("Enter comma-separated numbers: ").split(',') | ||
|
|
||
| # What do you need between the square brackets to make it work? | ||
| y = [] | ||
| #long way | ||
| #for z in x: | ||
| # if int(z) % 2 == 0: | ||
| # y.append(z) | ||
| y = [z for z in x if int(z) % 2 == 0] | ||
|
|
||
| print(y) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| # Write a function is_even that will return true if the passed-in number is even. | ||
|
|
||
| # YOUR CODE HERE | ||
| def is_even(num): | ||
| if num % 2 == 0: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
| # Read a number from the keyboard | ||
| num = input("Enter a number: ") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -9,4 +14,7 @@ | |
| # Print out "Even!" if the number is even. Otherwise print "Odd" | ||
|
|
||
| # YOUR CODE HERE | ||
|
|
||
| if is_even(num): | ||
| print("Even!") | ||
| else: | ||
| print("Odd") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,14 +5,24 @@ | |
| # 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 prints the | ||
| # sum. Google for "python arbitrary arguments" and look for "*args" | ||
|
|
||
| # YOUR CODE HERE | ||
|
|
||
| def f2(*argv): | ||
| temp = 0 | ||
| if isinstance(argv[0], list): | ||
| return sum(*argv) | ||
| else: | ||
| for arg in argv: | ||
| temp += int(arg) | ||
| return temp | ||
|
|
||
| print(f2(1)) # Should print 1 | ||
| print(f2(1, 3)) # Should print 4 | ||
| print(f2(1, 4, -12)) # Should print -7 | ||
|
|
@@ -21,13 +31,16 @@ | |
| a = [7, 6, 5, 4] | ||
|
|
||
| # What thing do you have to add to make this work? | ||
| # check to see if the argument is a list item or add * in front of a | ||
| print(f2(a)) # Should print 22 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your f2 function and print statement can be defined like the following: |
||
|
|
||
| # 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. 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 | ||
|
|
@@ -42,6 +55,10 @@ | |
| # Google "python keyword arguments". | ||
|
|
||
| # YOUR CODE HERE | ||
| def f4(**kwargs): | ||
| for key in kwargs: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your for loop can also be defined like the following for simplicity: |
||
| print(f"key: {key}, value: {kwargs[key]}") | ||
|
|
||
|
|
||
| # Should print | ||
| # key: a, value: 12 | ||
|
|
@@ -60,4 +77,5 @@ | |
| } | ||
|
|
||
| # What thing do you have to add to make this work? | ||
| f4(d) | ||
| # add ** in front of d | ||
| f4(**d) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,12 @@ | |
|
|
||
| def changeX(): | ||
| x = 99 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| print(x) | ||
|
|
||
| changeX() | ||
|
|
||
| # This prints 12. What do we have to modify in changeX() to get it to print 99? | ||
| #add print statement for x in changeX() scope | ||
| print(x) | ||
|
|
||
|
|
||
|
|
@@ -20,11 +22,12 @@ def outer(): | |
|
|
||
| def inner(): | ||
| y = 999 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| print(y) | ||
| inner() | ||
|
|
||
| # This prints 120. What do we have to change in inner() to get it to print | ||
| # 999? Google "python nested function scope". | ||
| # add print statement inside inner scope | ||
| print(y) | ||
|
|
||
| outer() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,26 @@ | ||
| """ | ||
| Python makes performing file I/O simple. Take a look | ||
| at how to read and write to files here: | ||
|
|
||
| https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files | ||
| """ | ||
|
|
||
| # Open up the "foo.txt" file (which already exists) for reading | ||
| # Print all the contents of the file, then close the file | ||
|
|
||
| # YOUR CODE HERE | ||
| with open("foo.txt") as foo: | ||
| print(foo.read()) | ||
| foo.closed | ||
|
|
||
| # 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 | ||
| # YOUR CODE HERE | ||
| bar = open("bar.txt", "w+") | ||
| bar.write("I opened a new file\nThis file contains some text\nThree lines to be exact") | ||
| bar.closed | ||
| with open("bar.txt") as bar: | ||
| print(bar.read()) | ||
| bar.closed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.