From 4007f0e63a9ca0bd8dbcd0455bf92c44713c6c50 Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 2 Sep 2020 21:48:52 -0500 Subject: [PATCH 1/3] finished first 4 --- .idea/.gitignore | 8 ++++++++ .idea/Intro-Python-I.iml | 12 ++++++++++++ .idea/inspectionProfiles/profiles_settings.xml | 6 ++++++ .idea/misc.xml | 4 ++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ src/00_hello.py | 2 +- src/01_bignum.py | 5 ++++- src/02_datatypes.py | 6 ++++-- src/03_modules.py | 11 ++++++----- 10 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Intro-Python-I.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..73f69e0958 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ 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..8c854ae3f0 100644 --- a/src/00_hello.py +++ b/src/00_hello.py @@ -1 +1 @@ -# Print "Hello, world!" to your terminal \ No newline at end of file +print('Hello, world!') \ No newline at end of file diff --git a/src/01_bignum.py b/src/01_bignum.py index c020928d63..c59aa41298 100644 --- a/src/01_bignum.py +++ b/src/01_bignum.py @@ -1,4 +1,7 @@ # 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..866a6ceaad 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..038cb5bf79 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 - +for arg in sys.argv: + print(arg) # 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 - 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 From 61eb91744bc27316e33055a18cc221cfc183f374 Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 3 Sep 2020 10:20:20 -0500 Subject: [PATCH 2/3] finished upto comprehensions --- src/04_printing.py | 8 +++++--- src/05_lists.py | 10 ++++++++-- src/06_tuples.py | 8 ++++++-- src/07_slices.py | 14 +++++++------- src/08_comprehensions.py | 21 ++++++++------------- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/04_printing.py b/src/04_printing.py index 06aaa7ff16..2d774cc742 100644 --- a/src/04_printing.py +++ b/src/04_printing.py @@ -11,7 +11,9 @@ # 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("%i, %.2f, %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 +txt = "{}, {}, {}" +print(txt.format(x,y,z)) +# Finally, print the same thing using an f-string +print(f"{x}, {y}, {z}") \ No newline at end of file diff --git a/src/05_lists.py b/src/05_lists.py index cfccc4e945..38abac6a4a 100644 --- a/src/05_lists.py +++ b/src/05_lists.py @@ -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 +x.pop(4) 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 nums in x: + print(nums * 1000) \ No newline at end of file diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..5626f25276 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -36,9 +36,13 @@ def dist(a, b): # YOUR CODE HERE +def print_tuple(tuple): + for v in tuple: + print(v) + 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? +print_tuple(u,) diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..2d08d531c9 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:2]) # 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..2fcd23fd02 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -2,40 +2,35 @@ List comprehensions are one cool and unique feature of Python. 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. - +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 = [] - -print (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 = [] - -print(y) +y = [x**3 for x in range(10)] +# print(y) # Write a list comprehension to produce the uppercase version of all the # elements in array a. Hint: "foo".upper() is "FOO". a = ["foo", "bar", "baz"] -y = [] - -print(y) - +y = [word.upper() for word in a] +# # Use a list comprehension to create a list containing only the _even_ elements # 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 = [] +y = [int(num) for num in x if int(num) % 2 == 0] print(y) \ No newline at end of file From 3ef15a8075bae002d67a253301680184b1d3481e Mon Sep 17 00:00:00 2001 From: Anthony Date: Tue, 8 Sep 2020 21:25:48 -0500 Subject: [PATCH 3/3] Finished and met MVP --- src/09_dictionaries.py | 9 +++++---- src/10_functions.py | 7 +++++++ src/11_args.py | 17 +++++++++++++++-- src/12_scopes.py | 5 ++++- src/13_file_io.py | 13 +++++++++++-- src/14_cal.py | 19 +++++++++++++++++-- src/15_classes.py | 24 ++++++++++++++++++++++-- 7 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..7f1cd09c05 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -35,13 +35,14 @@ # Add a new waypoint to the list # YOUR CODE HERE - -# Modify the dictionary with name "a place" such that its longitude +waypoints.append({"lat": 19, "lon": -96, "name": "a fourth place"})# 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].update(lon= -26, name= "not a real place") # 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 wp in waypoints: + print(wp.values()) \ No newline at end of file diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..498fbd0acb 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -10,3 +10,10 @@ # YOUR CODE HERE +def is_even(): + if num % 2 == 0: + return("Even!") + else: + return("Odd!") + +print(is_even()) \ No newline at end of file diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..499d086d11 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -5,6 +5,8 @@ # 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)) @@ -13,6 +15,9 @@ # Note: Google for "python arbitrary arguments" and look for "*args" # YOUR CODE HERE +def f2(*num): + for n in num: + return sum(num) print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 @@ -22,7 +27,7 @@ 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 @@ -30,6 +35,11 @@ # Note: Google "python default arguments" for a hint. # YOUR CODE HERE +def f3(a, b=0): + if a and b: + return a + b + elif a: + return a + 1 print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -44,6 +54,9 @@ # Note: Google "python keyword arguments". # YOUR CODE HERE +def f4(**kwargs): + for k,v in kwargs.items(): + print(str(k) + ": " + str(v)) # Should print # key: a, value: 12 @@ -62,4 +75,4 @@ } # How do you have to modify the f4 call below to make this work? -f4(d) +f4(**d) \ No newline at end of file diff --git a/src/12_scopes.py b/src/12_scopes.py index bc467fa423..67cd20425d 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -5,6 +5,7 @@ x = 12 def change_x(): + global x x = 99 change_x() @@ -19,7 +20,9 @@ def outer(): y = 120 def inner(): + nonlocal y #To Learn How to Access Inside y = 999 + """ print(y) """ #Wrong Way inner() @@ -29,4 +32,4 @@ def inner(): print(y) -outer() +outer() \ No newline at end of file diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..ab3f41db5b 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -1,7 +1,6 @@ """ 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 """ @@ -10,10 +9,20 @@ # Note: pay close attention to your current directory when trying to open "foo.txt" # YOUR CODE HERE +with open("src/foo.txt") as f: + read_data = f.read() + print(read_data) # 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 +with open("src/bar.txt", "r+") as f: + lines = "First line\n" + "Second line \n" + "Third line \n" + write_data = f.write(lines) + +with open("src/bar.txt", "r") as f: + read_data = f.read() + print(read_data) \ No newline at end of file diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..6839fe748f 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -26,7 +26,22 @@ print out a calendar for April in 2015, but if you omit either the year or both values, it should use today’s date to get the month and year. """ - import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime + +today = datetime.today() +month, year = today.month, today.year + +if len(sys.argv) > 1: + month = sys.argv[1] +if len(sys.argv) > 2: + year = sys.argv[2] + +if not month or int(month) < 1 or int(month) > 12: + print('USAGE: 14_cal.py [month] [year]') + print('-- [month] -> eg. 12') + print('-- [year] -> eg. 1999') + sys.exit() + +print(calendar.month(int(year), int(month))) \ No newline at end of file diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..98702d24bc 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -3,19 +3,39 @@ # 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(LatLon): + def __init__(self, name, lat, lon): + super().__init__(lat, lon) + self.name = name + def __str__(self): + return f'{self.name}, {self.lat}, {self.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(Waypoint): + def __init__(self,name, difficulty, size, lat, lon): + super().__init__(name, lat, lon) + self.difficulty = difficulty + self.size = size + def __str__(self): + return f'{self.name}, {"diff: " + str(self.difficulty)}, {"size: " + str(self.size)}, {self.lat}, {self.lon}' # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 # YOUR CODE HERE +waypoint = Waypoint("Catacombs", 41.70505, -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 @@ -24,6 +44,6 @@ # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 # YOUR CODE HERE - +geocache = Geocache("Newberry Views", 1.5, 2, 44.052137, -121.41556) # Print it--also make this print more nicely -print(geocache) +print(geocache) \ No newline at end of file