Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 217 additions & 33 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I',)\n"
]
}
],
"source": [
"# Your code here\n"
"tup = (\"I\", )\n",
"print(tup)"
]
},
{
Expand All @@ -32,11 +41,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n"
]
}
],
"source": [
"# Your code here\n"
"print(type(tup))"
]
},
{
Expand All @@ -54,13 +71,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m tup\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m,)\n",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"# Your code here\n",
"tup.append(\"r\",)\n",
"\n",
"# Your explanation here\n"
"# I can't add any of these elements because tuples are immutable, you cannot change them, thus we cannot aply the method 'append' to a tuple.\n"
]
},
{
Expand All @@ -80,13 +109,87 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 54,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"<class 'tuple'>\n"
]
}
],
"source": [
"# Your code here\n",
"tup = (\"I\", )\n",
"list_t = list(tup)\n",
"\n",
"# Your explanation here\n"
"list_new = [\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"]\n",
"list_t.extend(list_new)\n",
"\n",
"tup2 = tuple(list_t)\n",
"print(tup2)\n",
"print(type(tup2))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"print(tup)\n",
"# I'm not modifying the tuple, but instead creating a new one, that's why it lets me \"re-assign\" the elements to 'tup'."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['I', 'r', 'o', 'n', 'h', 'a', 'c', 'k']\n"
]
}
],
"source": [
"tup_l = list(tup)\n",
"print(tup_l)\n",
"\n",
"# With regard to casting, I can convert a tuple into a list, for example."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'n', 'a', 'c', 'I', 'k', 'r', 'h', 'o'}\n"
]
}
],
"source": [
"tup_2 = set(tup)\n",
"print(tup_2)\n",
"\n",
"# I can also convert a tuple into a set."
]
},
{
Expand All @@ -104,11 +207,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n')\n",
"('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n"
"tup1 = tup[0:4]\n",
"tup2 = tup[-4:]\n",
"print(tup1)\n",
"print(tup2)"
]
},
{
Expand All @@ -122,11 +237,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n"
"tup3 = tup1 + tup2\n",
"print(tup3)"
]
},
{
Expand All @@ -138,11 +262,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"8\n"
]
}
],
"source": [
"# Your code here\n"
"count_tup1 = len(tup1)\n",
"count_tup2 = len(tup2)\n",
"print(count_tup1 + count_tup2)\n",
"print(len(tup3))"
]
},
{
Expand All @@ -154,11 +290,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n"
"tup3.index(\"h\")"
]
},
{
Expand All @@ -178,11 +325,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n",
"False\n",
"False\n"
]
}
],
"source": [
"# Your code here\n"
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"tup3 = ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"\n",
" \n",
"for letter in letters:\n",
" if letter in tup3:\n",
" print(True)\n",
" else:\n",
" print(False)"
]
},
{
Expand All @@ -203,11 +370,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'a': 1\n",
"'b': 0\n",
"'c': 1\n",
"'d': 0\n",
"'e': 0\n"
]
}
],
"source": [
"# Your code here\n"
"for letter in letters:\n",
" if letter in tup3:\n",
" count = tup3.count(letter)\n",
" print(f\"'{letter}': {count}\")\n",
" else:\n",
" print(f\"'{letter}': 0\")"
]
}
],
Expand All @@ -227,7 +411,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.11.4"
},
"toc": {
"base_numbering": 1,
Expand Down
Loading