From a2c535665f87e2d65324a9326e45943a8c27e934 Mon Sep 17 00:00:00 2001 From: Julio E Date: Wed, 8 Jul 2020 19:27:26 -0700 Subject: [PATCH 1/6] Intro to python I half completed --- src/00_hello.py | 3 ++- src/01_bignum.py | 3 ++- src/02_datatypes.py | 5 +++-- src/03_modules.py | 11 ++++++----- src/04_printing.py | 5 +++-- src/05_lists.py | 14 ++++++++------ src/06_tuples.py | 7 ++++++- src/07_slices.py | 14 +++++++------- src/08_comprehensions.py | 8 ++++---- 9 files changed, 41 insertions(+), 29 deletions(-) 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..0347ae3f4b 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 +# 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..1f623414a5 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(int(y)+x) # 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..902cdbbd45 100644 --- a/src/03_modules.py +++ b/src/03_modules.py @@ -10,22 +10,23 @@ # Print out the command line arguments in sys.argv, one per line: # YOUR CODE HERE - +print(sys.argv) # 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.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()) \ No newline at end of file diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..16e413396e 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -11,7 +11,8 @@ # 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, 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 +# 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..2ab4582c9b 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -8,22 +8,24 @@ # 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(-2,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 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..157a1faab4 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -36,9 +36,14 @@ def dist(a, b): # YOUR CODE HERE +def print_tuple(t): + for x in t: + print(x) + + 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..ddde12a6ce 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[3]) # 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[:-2]) # 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..b1cf12a6b8 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -10,14 +10,14 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +y = [x+1 for x in range(5)] 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(9)] print(y) @@ -26,7 +26,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [x.upper() for x in a] print(y) @@ -36,6 +36,6 @@ x = input("Enter comma-separated numbers: ").split(',') # What do you need between the square brackets to make it work? -y = [] +y = [int(a) for a in x if int(a) % 2 == 0] print(y) \ No newline at end of file From e8b986633ecc25dcb36690be88419cfe312ddc05 Mon Sep 17 00:00:00 2001 From: Julio E Date: Fri, 10 Jul 2020 21:39:42 -0700 Subject: [PATCH 2/6] Second Half intro --- .idea/Intro-Python-I.iml | 8 ++++ .idea/inspectionProfiles/Project_Default.xml | 12 +++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ .idea/workspace.xml | 45 +++++++++++++++++++ src/09_dictionaries.py | 15 ++++++- src/10_functions.py | 15 ++++++- src/11_args.py | 34 ++++++++++---- src/12_scopes.py | 5 ++- src/13_file_io.py | 13 +++++- src/14_cal.py | 11 ++++- src/15_classes.py | 20 +++++++++ src/bar.txt | 3 ++ 14 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 .idea/Intro-Python-I.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 src/bar.txt diff --git a/.idea/Intro-Python-I.iml b/.idea/Intro-Python-I.iml new file mode 100644 index 0000000000..d0876a78d0 --- /dev/null +++ b/.idea/Intro-Python-I.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000..920d523a4e --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -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/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/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000..aaa9df8f48 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1594278189492 + + + + \ No newline at end of file diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..b91e4396cc 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -35,13 +35,24 @@ # Add a new waypoint to the list # YOUR CODE HERE - +new_waypoint = { + 'lat': 26, + 'lon': -214, + 'name': 'a fourth place' +} +waypoints.append(new_waypoint) +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 +# YOUR CODE HERE +for x in range(len(waypoints)): + print(waypoints[x].values()) \ No newline at end of file diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..6e087cf168 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,12 +1,23 @@ # Write a function is_even that will return true if the passed-in number is even. # YOUR CODE HERE +def is_true(num): + if num % 2 == 0: + return True + else: + return False + # 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" - +def is_even(num): + if num % 2 == 0: + print("Even!") + else: + print("Odd!") # YOUR CODE HERE - +is_even(num) +print(is_true(num)) \ No newline at end of file diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..9e6451c6fc 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -5,24 +5,35 @@ # the sum. This is what you'd consider to be a regular, normal function. # YOUR CODE HERE +def f1(a, b): + return a + b + print(f1(1, 2)) + # Write a function f2 that takes any number of integer arguments and returns the # sum. # Note: Google for "python arbitrary arguments" and look for "*args" # YOUR CODE HERE +def f2(*numbers): + x = 0 + for num in numbers: + x += num + return x -print(f2(1)) # Should print 1 -print(f2(1, 3)) # Should print 4 -print(f2(1, 4, -12)) # Should print -7 + +print(f2(1)) # Should print 1 +print(f2(1, 3)) # Should print 4 +print(f2(1, 4, -12)) # Should print -7 print(f2(7, 9, 1, 3, 4, 9, 0)) # Should print 33 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(sum(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 @@ -30,10 +41,15 @@ # Note: Google "python default arguments" for a hint. # YOUR CODE HERE +def f3(c, b=0): + if b == 0: + return c + 1 + else: + return c + b -print(f3(1, 2)) # Should print 3 -print(f3(8)) # Should print 9 +print(f3(1, 2)) # Should print 3 +print(f3(8)) # Should print 9 # Write a function f4 that accepts an arbitrary number of keyword arguments and # prints out the keys and values like so: @@ -44,7 +60,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 # key: b, value: 30 @@ -62,4 +80,4 @@ } # How do you have to modify the f4 call below to make this work? -f4(d) +f4(d=dict) diff --git a/src/12_scopes.py b/src/12_scopes.py index bc467fa423..b8d6d4f130 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -6,9 +6,10 @@ def change_x(): x = 99 + print(x) -change_x() +change_x() # This prints 12. What do we have to modify in change_x() to get it to print 99? print(x) @@ -20,7 +21,7 @@ def outer(): def inner(): y = 999 - + print(y) inner() # This prints 120. What do we have to change in inner() to get it to print diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..5c3e3e0fc4 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -10,10 +10,21 @@ # Note: pay close attention to your current directory when trying to open "foo.txt" # YOUR CODE HERE +import os +with open('foo.txt') as f: + read_data = f.read() # 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 +# YOUR CODE HERE +def save_to_file(text): + with open('bar.txt', 'w') as f: + f.write('\n'.join(text)) + +save_to_file("Big") + +f = open('bar.txt','r') +print(f.read()) \ No newline at end of file diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..cc5a1a77d7 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -29,4 +29,13 @@ import sys import calendar -from datetime import datetime \ No newline at end of file +import datetime + +year = datetime.datetime.today().year +month = datetime.datetime.today().month + + +def calendar(month=datetime.datetime.today().month, year=datetime.datetime.today().year): + print("14_cal.py [month] [year]") + month_input = ('Enter month: ') + year_input = ('Enter year: ') diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..6e94de2f11 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -2,21 +2,41 @@ # 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, lon, name): + super().__init__(lon) + self.name = name + # 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(object): + def __init__(self, lat, lon, name, difficulty, size): + super().__init__() + self.difficult = difficulty + self.size = size + # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 # YOUR CODE HERE +waypoint = Waypoint(41.70505, "Catacombs") + +print(waypoint.name) + # 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) diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..188cf96466 --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,3 @@ +B +i +g \ No newline at end of file From 9b8920c60022d4f847c970b52b78980d3a550c39 Mon Sep 17 00:00:00 2001 From: Julio E Date: Tue, 14 Jul 2020 19:12:02 -0700 Subject: [PATCH 3/6] Finishing touches --- .idea/workspace.xml | 47 ++++++++++++++++++++++++++++++++++++++++----- src/14_cal.py | 13 +++++++------ src/15_classes.py | 31 +++++++++++++++++++----------- 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index aaa9df8f48..28dac0222b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,12 +1,26 @@ + + + + + + + + + + + + + + + + + - - - - - + + @@ -70,10 +67,11 @@ - + + diff --git a/src/bar.txt b/src/bar.txt deleted file mode 100644 index 188cf96466..0000000000 --- a/src/bar.txt +++ /dev/null @@ -1,3 +0,0 @@ -B -i -g \ No newline at end of file diff --git a/src/foo.txt b/src/foo.txt deleted file mode 100644 index 9793c085e1..0000000000 --- a/src/foo.txt +++ /dev/null @@ -1,6 +0,0 @@ -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 From 8866441af28d00b29836a1959f67a23330a473de Mon Sep 17 00:00:00 2001 From: Julio Escalera <27julioescalera27@gmail.com> Date: Fri, 4 Sep 2020 19:02:03 -0700 Subject: [PATCH 5/6] Module 2 completed --- .idea/dictionaries/Julio.xml | 3 +++ .idea/workspace.xml | 17 ++++++++++++++++- src/13_file_io.py | 2 ++ src/14_cal.py | 19 ++++++++++++++----- src/bar.txt | 3 +++ src/foo.txt | 6 ++++++ 6 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 .idea/dictionaries/Julio.xml create mode 100644 src/bar.txt create mode 100644 src/foo.txt diff --git a/.idea/dictionaries/Julio.xml b/.idea/dictionaries/Julio.xml new file mode 100644 index 0000000000..9438963272 --- /dev/null +++ b/.idea/dictionaries/Julio.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 19572d253b..0b30812ad7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -19,7 +19,13 @@