Skip to content

Commit

Permalink
more syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
zingale committed May 13, 2024
1 parent 5fdc24f commit 2400452
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 231 deletions.
232 changes: 95 additions & 137 deletions content/02-python-functions-classes/python-classes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -82,9 +82,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mike\n",
"None\n"
]
}
],
"source": [
"a = Student(\"Mike\")\n",
"print(a.name)\n",
Expand All @@ -100,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -124,14 +133,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
" \n",
"<span class=\"fa fa-flash\"></span> Quick Exercise:\n",
"\n",
"````{admonition} Quick Exercise\n",
"\n",
"Loop over the students in the `students` list and print out the name and grade of each student, one per line.\n",
"\n",
"</div>"
"````"
]
},
{
Expand All @@ -150,9 +156,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['leela', 'amy', 'hermes', 'hypnotoad']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"As = [q.name for q in students if q.grade.startswith(\"A\")]\n",
"As"
Expand All @@ -174,7 +191,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -230,7 +247,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -246,7 +263,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -262,18 +279,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c2.value()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"invalid suit, setting to 1\n"
]
}
],
"source": [
"c3 = Card(suit=0, rank=4)"
]
Expand All @@ -287,9 +323,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2:♠\n",
"2:♥\n"
]
}
],
"source": [
"print(c1)\n",
"print(c2)"
Expand All @@ -304,9 +349,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"print(c1 > c2)\n",
"print(c1 < c2)"
Expand All @@ -321,9 +375,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'Card' and 'Card'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mc1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mc2\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Card' and 'Card'"
]
}
],
"source": [
"c1 + c2"
]
Expand All @@ -332,15 +398,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
" \n",
"<span class=\"fa fa-flash\"></span> Quick Exercise:\n",
"````{admonition} Quick Exercise\n",
"\n",
" * Create a \"hand\" corresponding to a straight (5 cards of any suite, but in sequence of rank)\n",
" * Create another hand corresponding to a flush (5 cards all of the same suit, of any rank)\n",
" * Finally create a hand with one of the cards duplicated&mdash;this should not be allowed in a standard deck of cards. How would you check for this?\n",
"\n",
"</div>"
"````"
]
},
{
Expand All @@ -352,98 +416,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deck of Cards"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Classes can use other include other classes as data objects&mdash;here's a deck of cards. Note that we are using the python random module here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"class Deck:\n",
" \"\"\" the deck is a collection of cards \"\"\"\n",
"\n",
" def __init__(self):\n",
"\n",
" self.nsuits = 4\n",
" self.nranks = 13\n",
" self.minrank = 2\n",
" self.maxrank = self.minrank + self.nranks - 1\n",
"\n",
" self.cards = []\n",
"\n",
" for rank in range(self.minrank,self.maxrank+1):\n",
" for suit in range(1, self.nsuits+1):\n",
" self.cards.append(Card(rank=rank, suit=suit))\n",
"\n",
" def shuffle(self):\n",
" random.shuffle(self.cards)\n",
"\n",
" def get_cards(self, num=1):\n",
" hand = []\n",
" for n in range(num):\n",
" hand.append(self.cards.pop())\n",
"\n",
" return hand\n",
" \n",
" def __str__(self):\n",
" string = \"\"\n",
" for c in self.cards:\n",
" string += str(c) + \" \"\n",
" return string"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"let's create a deck, shuffle, and deal a hand (for a poker game)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mydeck = Deck()\n",
"print(mydeck)\n",
"print(len(mydeck.cards))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"notice that there is no error handling in this class. The get_cards() will deal cards from the deck, removing them in the process. Eventually we'll run out of cards."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mydeck.shuffle()\n",
"\n",
"hand = mydeck.get_cards(5)\n",
"for c in sorted(hand): print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -472,8 +444,6 @@
" self.country = country\n",
" \n",
" def __add__(self, other):\n",
" if self.country != other.country:\n",
" return None\n",
" return Currency(self.amount + other.amount, country=self.country)\n",
"\n",
" def __sub__(self, other):\n",
Expand Down Expand Up @@ -505,14 +475,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
" \n",
"<span class=\"fa fa-flash\"></span> Quick Exercise:\n",
" \n",
"````{admonition} Quick Exercise \n",
"\n",
"As written, our Currency class has a bug&mdash;it does not check whether the amounts are in the same country before adding. Modify the `__add__` method to first check if the countries are the same. If they are, return the new `Currency` object with the sum, otherwise, return `None`.\n",
"\n",
"</div>"
"````"
]
},
{
Expand Down Expand Up @@ -840,15 +807,6 @@
"source": [
"-u"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -867,7 +825,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 2400452

Please sign in to comment.