Skip to content

Commit 038a88d

Browse files
add some more classes things to functions_and_classes along with misc to other lessons, add solutions.py to write easily
1 parent 7fa45c7 commit 038a88d

File tree

7 files changed

+226
-39
lines changed

7 files changed

+226
-39
lines changed

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Data_Structures.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
"execution_count": null,
162162
"source": [
163163
"my_shopping_list = (\"laptop\", \"mouse\", \"kitten\", \"4 count monster energy\")\n",
164-
"alex_yin_shopping_list = (\"violin\", \"robux for deepwoken\", \"hershey bar\")\n",
164+
"alex_yin_shopping_list = (\"violin\", \"robux for deepwoken\", \"hershey bar\", \"4 count monster energy\")\n",
165165
"\n",
166166
"combined_shopping_list = my_shopping_list + alex_yin_shopping_list\n",
167167
"print(\"Our shopping list:\", my_shopping_list)"
@@ -230,7 +230,7 @@
230230
"execution_count": null,
231231
"source": [
232232
"n=6\n",
233-
"print(n^2) # MAKE SURE NOT TO DO THIS!! this is a bitwise XOR operation, not squaring a number!!\n",
233+
"print(n ^ 2) # MAKE SURE NOT TO DO THIS!! this is a bitwise XOR operation, not squaring a number!!\n",
234234
"print(n ** 2) # this is the correct way to do exponentiation\n",
235235
"\n",
236236
"point_1=(3, 4) # x1 and y1 coordinates\n",

Functions_and_Classes.ipynb

Lines changed: 163 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"cells": [
33
{
4-
"metadata": {},
54
"cell_type": "markdown",
5+
"id": "718e27f5bfe0fec6",
6+
"metadata": {},
67
"source": [
78
"# functions and classes\n",
89
"\n",
@@ -12,16 +13,16 @@
1213
"\n",
1314
"each function can take in parameters, which are inputs that the function uses to perform its task. It can also return a value, which is the output of the function. This allows you to pass data into the function and get results back. you declare a function using the `def` keyword, followed by the function name and parentheses containing any parameters. The body of the function is indented below the definition. when the indentation goes back to its previous level, that means the definition of the function is over.\n",
1415
"take a look at a few examples below :"
15-
],
16-
"id": "718e27f5bfe0fec6"
16+
]
1717
},
1818
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"id": "initial_id",
1922
"metadata": {
2023
"collapsed": true
2124
},
22-
"cell_type": "code",
2325
"outputs": [],
24-
"execution_count": null,
2526
"source": [
2627
"# the most basic function, some code under a header\n",
2728
"def print_hello():\n",
@@ -38,38 +39,27 @@
3839
"print_hello()\n",
3940
"greet() # put your name here!!\n",
4041
"exponentiate(9,3)"
41-
],
42-
"id": "initial_id"
42+
]
4343
},
4444
{
45-
"metadata": {},
4645
"cell_type": "markdown",
46+
"id": "863527bde78d346d",
47+
"metadata": {},
4748
"source": [
4849
"### practice exercise!! - greatest commmon divisor\n",
4950
"write a function that takes in two numbers and returns their greatest common divisor (gcd). the gcd is the largest number that divides both numbers without leaving a remainder. for example, the gcd of 12 and 15 is 3, because 3 is the largest number that will cleanly factor into 12 and 15.\n"
50-
],
51-
"id": "863527bde78d346d"
51+
]
5252
},
5353
{
54+
"cell_type": "code",
55+
"execution_count": 2,
56+
"id": "ff295ae2bf57e0a3",
5457
"metadata": {
5558
"ExecuteTime": {
5659
"end_time": "2025-08-21T16:36:49.522342Z",
5760
"start_time": "2025-08-21T16:36:49.517259Z"
5861
}
5962
},
60-
"cell_type": "code",
61-
"source": [
62-
"## write your code for the gcd function here\n",
63-
"\n",
64-
"# your code here!!\n",
65-
"\n",
66-
"assert gcd(12, 15) == 3\n",
67-
"assert gcd(54, 24) == 6\n",
68-
"assert gcd(101, 10) == 1\n",
69-
"assert gcd(100, 25) == 25\n",
70-
"print(\"all unit tests are passing\")"
71-
],
72-
"id": "ff295ae2bf57e0a3",
7363
"outputs": [
7464
{
7565
"name": "stdout",
@@ -80,34 +70,175 @@
8070
]
8171
}
8272
],
83-
"execution_count": 2
73+
"source": [
74+
"## write your code for the gcd function here\n",
75+
"\n",
76+
"# your code here!!\n",
77+
"\n",
78+
"assert gcd(12, 15) == 3\n",
79+
"assert gcd(54, 24) == 6\n",
80+
"assert gcd(101, 10) == 1\n",
81+
"assert gcd(100, 25) == 25\n",
82+
"print(\"all unit tests are passing\")"
83+
]
8484
},
8585
{
86-
"metadata": {},
8786
"cell_type": "markdown",
87+
"id": "2f14750a7071b0c",
88+
"metadata": {},
8889
"source": [
8990
"## recursive functions\n",
9091
"\n",
9192
"a recursive function is a function that calls itself. this is useful for problems that can be broken down into smaller subproblems, like calculating factorials or traversing trees. the key to a recursive function is to have a base case that stops the recursion, otherwise it will run indefinitely and cause a stack overflow error.\n",
9293
"take a look at the example below, which calculates a factorial, which is the product of each number smaller than itself to 1. for example, 5! = 5 * 4 * 3 * 2 * 1 = 120. the base case is when n is 0 or 1, in which case the factorial is 1. otherwise, the function calls itself with n - 1 until it reaches the base case."
93-
],
94-
"id": "2f14750a7071b0c"
94+
]
9595
},
9696
{
97-
"metadata": {},
9897
"cell_type": "code",
99-
"outputs": [],
10098
"execution_count": null,
99+
"id": "91a29a6bd518edd3",
100+
"metadata": {},
101+
"outputs": [],
101102
"source": [
102-
"def factorial (n):\n",
103+
"def factorial(n):\n",
103104
" if n == 0 or n == 1: # base case\n",
104105
" return 1\n",
105106
" else:\n",
106107
" return n * factorial(n - 1) # recursive case\n",
107108
"\n",
108109
"print(factorial(6))"
109-
],
110-
"id": "91a29a6bd518edd3"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"id": "4c89e89256f37f3c",
115+
"metadata": {},
116+
"source": [
117+
"### practice exercise with recursion - sum of digits\n",
118+
"\n",
119+
"to practice writing recursive functions, write a function that takes in a non-zero positive integer and returns the sum of its digits. for example, if the number is 69420, 6 + 9 + 4 + 2 + 0 = 21, so the function should be returning 21. Here's a hint, try using logarithms or floor division `(a // b)` :3"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"id": "cf1b20cd535d144a",
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"# your code here!"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"id": "4c5802f40a4ceeab",
135+
"metadata": {},
136+
"source": [
137+
"## type hinting\n",
138+
"Sometimes, you will be writing functions for other people to use. Type hinting is a way to specify what kind of paramaters are accepted by a function, and will make a function easier to use and understand. It is not requires, but it is good practice to use type hinting when writing functions exported outside of your code.\n",
139+
"For exampl, if you have a function that adds two integer outputs, you can specify that the function takes in two integers and returns an integer. This helps others understand how to properly use said function, and helps with debugging if the function is incorrectly used."
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"id": "d14af50469ef768a",
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"def add_three_numbers(a: int, b: int, c: int) -> int:\n",
150+
" \"\"\"Adds three integers and returns the result.\"\"\"\n",
151+
" return a + b + c"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"id": "a43ee4a9a4eaed79",
157+
"metadata": {},
158+
"source": [
159+
"You can also see that I added a string with three quotation marks in the first line of the function. This is called a docstring, and it is a way to document what a function does. Some IDEs will show this docstring when you hover over the function name, which is useful for understanding what a function does without having to read the function again every single time.\n",
160+
"\n",
161+
"The syntax for type hinting is to put a colon after the parameter name, followed by the type. The return type is specified after the closing parenthesis of the parameter list, followed by an arrow `->` and the return type. You can think of it like the function is pointing to the return type of the function."
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"id": "8d86ef23ae0ea20",
167+
"metadata": {},
168+
"source": [
169+
"## Classes!\n",
170+
"\n",
171+
"Classes are a type of data structure that allow you to create your own data types. These can bind data and functions together, allowing you to create objects with both properties and methods.\n",
172+
"These are good for organizing your code and making it more modular. You can think of a class as a blueprint for an object, and an object is an instance of a class. Classes are the cornerstone of object-oriented programming (OOP). After using classes, you will find yourself saying \"OOPs, why did i make everything an object lmao\".\n",
173+
"Classes are a good way to make data types in games, like weapons, items, or players. Or, if you're making something like a web app, you can use classes to represent users, posts, or comments. You can also use classes to represent more complex data structures, like trees or graphs.\n",
174+
"\n",
175+
"look at the code below for an example of a class that represents a student."
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": null,
181+
"id": "3514cd46e732de63",
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"class Student:\n",
186+
" def __init__(self, name: str, age: int, major: str):\n",
187+
" self.name = name\n",
188+
" self.age = age\n",
189+
" self.major = major\n",
190+
"\n",
191+
" def introduce(self):\n",
192+
" print(f\"Hello, my name is {self.name}, I'm {self.age} years old and I major in {self.major}.\")\n",
193+
" def birthday(self):\n",
194+
" self.age += 1\n",
195+
" print(f\"Happy birthday {self.name}! You are now {self.age} years old.\")\n",
196+
" def change_major(self, new_major):\n",
197+
" self.major = new_major\n",
198+
" print(f\"{self.name} has changed their major to {self.major}.\")\n",
199+
"jeremy = Student(\"Jeremy\", 20, \"Computer Science\") # create an instance of the Student class\n",
200+
"jeremy.introduce()\n",
201+
"jeremy.birthday()\n",
202+
"jeremy.change_major(\"Mathematics\")\n",
203+
"jeremy.introduce()\n",
204+
"# we can see here that even though the class methods take in the `self` parameter, we do not need to pass it when we call the methods.\n",
205+
"\n",
206+
"# now, try creating your own instance of the student class under your name!"
207+
]
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"id": "b058e3873cc4a21d",
212+
"metadata": {},
213+
"source": [
214+
"In this example, we have a Student class that represents a student. The `__init__` method is a special method, called the constructor. This gets called every time you create an object, hence its name. It initializes the object's properties, which are name, age, and major in this case. The `self` parameter refers to the instance of the class itself, and is used to access the object's properties and methods.\n",
215+
"\n",
216+
"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",
217+
"\n",
218+
"### special class methods\n",
219+
"\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"
221+
]
222+
},
223+
{
224+
"cell_type": "markdown",
225+
"id": "83e689a7",
226+
"metadata": {},
227+
"source": [
228+
"## exercise - writing your own Car class\n",
229+
"\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",
231+
"\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. "
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": null,
238+
"id": "d4331927360a7115",
239+
"metadata": {},
240+
"outputs": [],
241+
"source": []
111242
}
112243
],
113244
"metadata": {

Intro_To_Python.ipynb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"\n",
1414
"## Your First Python Script\n",
1515
"\n",
16-
"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",
17-
"\n"
16+
"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"
1817
],
1918
"id": "5cfc0bb57c4e4fb9"
2019
},
@@ -46,7 +45,7 @@
4645
"metadata": {},
4746
"cell_type": "markdown",
4847
"source": [
49-
"## Python syntax\n",
48+
"## Python Syntax\n",
5049
"\n",
5150
"As mentioned earlier, Python relies strongly on indentation and whitespace to define code blocks. For an example, we will be using `if` statements, which we will be discussing in more detail later. An `if` statement is a conditional statement that allows you to execute a block of code only if a certain condition is met. Here is an example of an `if` statement in Python:"
5251
],

TODO.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TODO:
2+
3+
- dictionaries about different terms
4+
- add more "fix this code" examples
5+
- step daily challenges
6+
- _write_ daily challenges
7+
- seriousness, serious topics
8+
- make sure that its light

solutions.ipynb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,25 @@
135135
],
136136
"id": "d76d86b3a3a9271f"
137137
},
138+
{
139+
"metadata": {},
140+
"cell_type": "markdown",
141+
"source": "## sum of digits using recursion",
142+
"id": "3b269a1989dbb936"
143+
},
138144
{
139145
"metadata": {},
140146
"cell_type": "code",
141147
"outputs": [],
142148
"execution_count": null,
143-
"source": "",
144-
"id": "134824d4f7570878"
149+
"source": [
150+
"def sum_digits(n):\n",
151+
" if n == 0:\n",
152+
" return 0\n",
153+
" else:\n",
154+
" return n % 10 + sum_digits(n // 10)"
155+
],
156+
"id": "fa1fbdb2a45856ed"
145157
}
146158
],
147159
"metadata": {

solutions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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)