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
187 changes: 115 additions & 72 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"('I',)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup = (\"I\",)"
"tup = (\"I\",)\n",
"tup"
]
},
{
Expand All @@ -34,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -43,14 +55,14 @@
"tuple"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"type(tup)"
"type(tup)\n"
]
},
{
Expand All @@ -68,26 +80,25 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"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)",
"\u001b[0;32m<ipython-input-3-79d5a2bcc9c1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your code here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;31m# Your explanation here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# tuple is immutable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Your code here\n",
"tup.append(\"r\")\n",
"# Your explanation here\n",
"# tuple is immutable"
"\"\"\"\n",
"Trying to append an element to a tuple with the code above will result in an AttributeError.\n",
"This is because tuples in Python are inmutable, , meaning once they are created, their contents cannot be changed. \n",
"Since tuples are immutable, you can also no add, or remove elements from them after creation.\n",
"\n",
"\"\"\"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
Expand All @@ -110,9 +121,14 @@
"outputs": [],
"source": [
"# Your code here\n",
"tup[0] = \"r\"\n",
"#tup[1] = \"r\" - this would give me an error\n",
"\n",
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\") #instead I reassign a new tuple to tup.\n",
"# Your explanation here\n",
"# tuple is immutable"
"\"\"\"\n",
"Because tuples are immutable, I can't directly reassign individual elements of a tuple\n",
"I can, however, as I did above, create a new tuple and assign it to tup, as an alternative\n",
"\"\"\"\n"
]
},
{
Expand All @@ -132,11 +148,21 @@
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n') ('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup1 = (\"I\", \"r\", \"o\", \"n\")\n",
"tup2 = (\"h\", \"a\", \"c\", \"k\")"
"tup1 = tup[:4]\n",
"tup2 = tup[-4:]\n",
"\n",
"print(tup1, tup2)"
]
},
{
Expand All @@ -154,22 +180,20 @@
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"True\n"
]
"data": {
"text/plain": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"tup3 = tup1 + tup2\n",
"print(tup3)\n",
"print(tup)\n",
"print(tup == tup3)"
"tup3"
]
},
{
Expand All @@ -181,26 +205,35 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"8\n",
"8\n",
"8\n"
"4\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"print(len(tup1))\n",
"print(len(tup3))\n",
"print(len(tup1) + len(tup2))\n",
"print(len(tup3))"
"print(len(tup1)) #print lenght of tup1\n",
"print(len(tup2)) #print lenght of tup1\n",
"sum_len = len(tup1) + len(tup2) #sum the lengths of tup1 ans tup2 and assign it to sum_len\n",
"\n",
"sum_len == len(tup3) #check if sum_len is equal to the length of tup3"
]
},
{
Expand All @@ -212,7 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -221,15 +254,14 @@
"4"
]
},
"execution_count": 32,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup3.index(\"h\")\n",
"##It's number 4"
"tup.index(\"h\")"
]
},
{
Expand All @@ -249,33 +281,26 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[True, False, True, False, False]\n"
"True\n",
"False\n",
"True\n",
"False\n",
"False\n"
]
}
],
"source": [
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"# Your code here\n",
"# with a for loop\n",
"#for element in letters:\n",
" # if element in tup3:\n",
" # print(True)\n",
" # else:\n",
" # print(False)\n",
"#using a list comprehension\n",
"\n",
"existing_letters = [True if (element in tup3) else False for element in letters ]\n",
"\n",
"#if element in tup3 print(True) else print(False) \n",
"print(existing_letters)\n"
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"for letter in letters:\n",
" print(letter in tup3)\n"
]
},
{
Expand All @@ -289,18 +314,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Letter a appears 1 time(s) in tup3.\n",
"Letter c appears 1 time(s) in tup3.\n"
]
}
],
"source": [
"# Your code here\n",
"\n"
"for letter in letters:\n",
" if letter in tup3:\n",
" print(f\"Letter {letter} appears {tup3.count(letter)} time(s) in tup3.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -314,9 +357,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading