Skip to content

Commit 6bd7ec6

Browse files
finish writing up rough draft of the functions and classes lesson
1 parent 038a88d commit 6bd7ec6

File tree

5 files changed

+116
-38
lines changed

5 files changed

+116
-38
lines changed

Functions_and_Classes.ipynb

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,48 @@
215215
"\n",
216216
"Due to the peculiarities of Python, the `self` parameter is required in every method of a class, and is always the first parameter. However, when you call an object's method, you do not need to pass in the `self` parameter, as Python will do this automatically.\n",
217217
"\n",
218-
"### special class methods\n",
218+
"### special or \"magic\" class methods\n",
219219
"\n",
220-
"There are a few other special class methods that we can use to do things for us automatically. All of these special methods are denoted by a double underscore in their method name, like `__init__`. \n"
220+
"There are a few other special class methods that we can use to do things for us automatically. Some people call these _magic_ methods, but there's no magic behind these, just 1s and 0s. if you look deeper all of computers is just funny electrons jumping around on different excitement states on metal and plastic. So much of my life is dedicated to funny electrons...If you think about it, internet influencers and crypto moguls make a living off making electrons move slightly differently on someone's magic handheld box to display images and sound...Kind of a sad life...OKAY IM GOING TO STOP BEING EXISTENTIAL THIS IS JUST A PROGRAMMING CLUB LESSON.\n",
221+
"guys i think writing these at 9pm was bad decision i woke up at 6am today and had to drive and volunteer and ugh im so stressed out rn its the summer and somehow im swamped in work\n",
222+
"ANYWAYS, All of these special methods are denoted by a double underscore in their method name, like `__init__`.\n",
223+
"\n",
224+
"- `__str__` : this method is called when you call the `str()` function on an object, or when you print an object. It should return a string representation of the object. This is useful for debugging and logging, as it allows you to see the object's properties in a readable format. This is similar to the `toString()` method that you'll `@Override` in Java.\n",
225+
"- theres actually a whole bunch more than i thought there were, i only knew `__init__` and `__str__` before looking it up lol. theres a whole bunch more that define behavior when you use add, divide, multiply, and other operators on objects. you can check out the full list [here](https://docs.python.org/3/reference/datamodel.html#special-method-names) and [here](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types) if you're interested.\n"
221226
]
222227
},
228+
{
229+
"metadata": {},
230+
"cell_type": "code",
231+
"outputs": [],
232+
"execution_count": null,
233+
"source": [
234+
"class Apple:\n",
235+
" def __init__(self, variety, color, weight):\n",
236+
" self.variety = variety\n",
237+
" self.color = color\n",
238+
" self.weight = weight\n",
239+
" def __str__(self):\n",
240+
" return f\"{self.color} {self.variety} apple weighing {self.weight} grams\"\n",
241+
"my_apple = Apple(\"Granny Smith\", \"Green\", 150)\n",
242+
"print(my_apple) # this will call the __str__ method"
243+
],
244+
"id": "fba9dcd9945e5d69"
245+
},
223246
{
224247
"cell_type": "markdown",
225248
"id": "83e689a7",
226249
"metadata": {},
227250
"source": [
228251
"## exercise - writing your own Car class\n",
229252
"\n",
230-
"yeah yeah you might say, making a car class is soooooo basic. but hey, it's a tried and true method of learning how to use classes. \n",
253+
"yeah yeah you might say, making a car class is soooooo basic. but hey, it's a tried and true method of learning how to use classes. c.f. my ap comp sci class\n",
254+
"\n",
255+
"-- Specifications --\n",
231256
"\n",
232-
"For your car, it's going to want to have a few properties. We're going to want to describe the `make` (brand, e.g. Honda), `model` (e.g. Corolla), `year`, `color`, and `mpg` (miles per gallon). These should all go in the constructor. Make sure to initialize an extra property called `gallons`, which is the gallons left in tank, to zero. "
257+
"For your car, it's going to want to have a few properties. We're going to want to describe the `make` (brand, e.g. Honda), `model` (e.g. Corolla), `year`, `color`, and `mpg` (miles per gallon). These should all go in the constructor. Make sure to initialize an extra property called `gallons`, which is the gallons left in tank, to zero.\n",
258+
"\n",
259+
"Make sure to add a `__str__` method that returns a string representation of the car (bonus points for ASCII art!!) which includes all the information about the car. Also, add a method called `add_fuel` that takes in a number of gallons and adds it to the tank, a method called `repaint` which changes the color, and, a method called `check_tank` that prints out how many gallons are left in the tank. Finally, add a method called `drive` that takes in a number of miles and reduces the gallons in the tank based on the mpg. If there is not enough fuel to drive the requested number of miles, drive as far as possible and set the gallons to zero. Then, call a tow truck (just kidding!). The method should return the number of miles driven.\n"
233260
]
234261
},
235262
{
@@ -238,7 +265,21 @@
238265
"id": "d4331927360a7115",
239266
"metadata": {},
240267
"outputs": [],
241-
"source": []
268+
"source": [
269+
"# write your code for the car class here VVV\n",
270+
"#\n",
271+
"#\n",
272+
"#\n",
273+
"#\n",
274+
"# vroom vroom\n",
275+
"# ______\n",
276+
"# /|_||_\\`.__\n",
277+
"#( _ _ _\\\n",
278+
"#=`-(_)--(_)-'\n",
279+
"# man do you remember the cars movies ...that was so long ago...peak cinema...\n",
280+
"# i feel old now\n",
281+
"# lightning mcqueen was such a mood tho ngl...i used to play with lightning mcqueen toys all the time in my childhood"
282+
]
242283
}
243284
],
244285
"metadata": {

Intro_To_Python.ipynb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@
1111
"\n",
1212
"Python is designed to be easy to read and write, which makes it a great language for beginners. It does away with curly braces and semicolons, and instead uses indentation and whitespace to define code blocks. This makes it easier to read and understand, especially for those who are new to programming. For this set of lessons, we will be using Python 3, which is the latest version of the language. Although Python 2 is still in use in some legacy systems, it only receives security patches.\n",
1313
"\n",
14+
"### A few words before we dive in\n",
15+
"\n",
16+
"sooo its almost 9pm as i right these words. my brain is slowly melting. i have spent the better part of an hour debugging my wifi drivers on my twinkpad. i have been grinding out these lessons over the past week and a half, and its been taking a toll. ive gone back to write this as an apology for any mistakes or lack of clarity. i write this as someone who has been programming for a handful of years, and i understand that some of the concept that i take for granted, you may have never heard of. i apologize if i do not explain something, please dont hesitate to ask me about it. ill do my best to explain.\n",
17+
"\n",
18+
"The other thing is that im not exactly the best writer. i barely passed english 2 sophomore year. my writing quality is proportional to the amount of sleep i got, and i didnt get much sleep last night. as the weeks go by, youll probably be able to tell that it starts out very professional like and slowly degrades into a casual conversation, please forgive me. i tend to write how i would speak to y'all, which explains the casual tone and constant comma splices lmao. i tried to write inside jokes and memes into the lessons to make them more fun so if you think its dry im sorry :sob: ill try to write more skibidi brainrot into the lessons or whatever yall like these days i dont keep up with the trends :pensive-wobble: my fyp is like not representative of anything thats actually popular on tiktok or insta anymore its just things i laugh at :p\n"
19+
],
20+
"id": "5cfc0bb57c4e4fb9"
21+
},
22+
{
23+
"metadata": {},
24+
"cell_type": "markdown",
25+
"source": [
1426
"## Your First Python Script\n",
1527
"\n",
1628
"For now, let's start with the basics. The first \"tool\" under your toolbelt will be printing output to the console. While you will not find a lot of command-line applications for popular use written in Python (except tools for other developers!), printing is still an essential tool for debugging your programs and in the first stages of development. To print to the console, type (or run the following code cell):\n"
1729
],
18-
"id": "5cfc0bb57c4e4fb9"
30+
"id": "ea5ae82e4fda4520"
1931
},
2032
{
2133
"metadata": {

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ TODO:
55
- step daily challenges
66
- _write_ daily challenges
77
- seriousness, serious topics
8-
- make sure that its light
8+
- make sure that its light
9+
- ADD DICTIONARIES AT THE START OF LESSONS LPLEASE

solutions.ipynb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,61 @@
154154
" return n % 10 + sum_digits(n // 10)"
155155
],
156156
"id": "fa1fbdb2a45856ed"
157+
},
158+
{
159+
"metadata": {},
160+
"cell_type": "markdown",
161+
"source": "",
162+
"id": "8ed2c5960799334a"
163+
},
164+
{
165+
"metadata": {},
166+
"cell_type": "markdown",
167+
"source": [
168+
"## car class, functions and classes\n",
169+
"this is just an example for the car class, it can be done in many different ways. added below are a few unit tests / example uses of a simple car class"
170+
],
171+
"id": "5882d4ab6c21982"
172+
},
173+
{
174+
"metadata": {},
175+
"cell_type": "code",
176+
"outputs": [],
177+
"execution_count": null,
178+
"source": [
179+
"class Car:\n",
180+
" def __init__(self, make, model, year, color, mpg):\n",
181+
" self.make = make\n",
182+
" self.model = model\n",
183+
" self.year = year\n",
184+
" self.mpg = mpg\n",
185+
" self.color = color\n",
186+
" self.fuel_level = 0.0 # in gallons\n",
187+
"\n",
188+
" def __str__(self):\n",
189+
" return f\"{self.year} {self.make} {self.model}. Fuel level: {self.fuel_level} gallons. Gets {self.mpg} miles per gallon.\"\n",
190+
" def add_fuel(self, gallons):\n",
191+
" self.fuel_level += gallons\n",
192+
" def check_tank():\n",
193+
" return gallons\n",
194+
" def drive(self, miles) -> float:\n",
195+
" possible_miles = self.fuel_level * self.mpg\n",
196+
" if miles > possible_miles:\n",
197+
" miles = possible_miles\n",
198+
" self.fuel_level -= miles / self.mpg\n",
199+
" return miles\n",
200+
" def repaint(self, color):\n",
201+
" self.color = color\n",
202+
"\n",
203+
"\n",
204+
"ol_reliable = Car(\"Toyota\", \"Sienna\", 2017, \"Silver\", 30.3)\n",
205+
"ol_reliable.add_fuel(12.3)\n",
206+
"print(f\"There are {ol_reliable.check_tank()} gallons left in the {ol_reliable}\")\n",
207+
"print(f\"Let's try to drive 100 miles! We drove {drive(1000)} miles, and now there is {ol_reliable.check_tank()} gallons left in the tank.\")\n",
208+
"ol_reliable.repaint(\"Lime Green\")\n",
209+
"print(f\"We've repainted our car! {ol_reliable}\")"
210+
],
211+
"id": "9367f77632cbe6c6"
157212
}
158213
],
159214
"metadata": {

solutions.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +0,0 @@
1-
class Car:
2-
def __init__(self, make, model, year, color, mpg):
3-
self.make = make
4-
self.model = model
5-
self.year = year
6-
self.mpg = mpg
7-
self.color = color
8-
self.fuel_level = 0.0 # in gallons
9-
10-
def __str__(self):
11-
return f"{self.year} {self.make} {self.model}. Fuel level: {self.fuel_level} gallons. Gets {self.mpg} miles per gallon."
12-
def add_fuel(self, gallons):
13-
self.fuel_level += gallons
14-
def check_tank():
15-
return gallons
16-
def drive(self, miles) -> float:
17-
possible_miles = self.fuel_level * self.mpg
18-
if miles > possible_miles:
19-
miles = possible_miles
20-
self.fuel_level -= miles / self.mpg
21-
return miles
22-
def repaint(self, color):
23-
self.color = color
24-
25-
26-
ol_reliable = Car("Toyota", "Sienna", 2017, "Silver", 30.3)
27-
ol_reliable.add_fuel(12.3)
28-
print(f"There are {ol_reliable.check_tank()} gallons left in the {ol_reliable}")
29-
print(f"Let's try to drive 100 miles! We drove {drive(1000)} miles, and now there is {ol_reliable.check_tank()} gallons left in the tank.")
30-
ol_reliable.repaint("Lime Green")
31-
print(f"We've repainted our car! {ol_reliable}")

0 commit comments

Comments
 (0)