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
166 changes: 111 additions & 55 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
}
],
"source": [
"# Your code here\n",
"type(tup)"
]
},
Expand All @@ -59,11 +58,13 @@
"source": [
"#### Now try to append the following elements to `tup`. \n",
"\n",
"\n",
"\n",
"Are you able to do it? Explain.\n",
"\n",
"```\n",
"\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k',\n",
"```"
"``"
]
},
{
Expand All @@ -76,18 +77,19 @@
"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'"
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_6036\\1578196230.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Your code here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"o\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"h\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"a\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"c\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"k\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;31m# Your explanation here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"# Your code here\n",
"tup.append(\"r\")\n",
"\n",
"tup.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"# Your explanation here\n",
"# tuple is immutable"
"Tuples are immutable, we cannot append elements"
]
},
{
Expand All @@ -105,14 +107,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['I', 'r', 'o', 'n', 'h', 'a', 'r', 'k']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup[0] = \"r\"\n",
"# Appending to a tuple with list conversion\n",
"tup = (\"I\",)\n",
"tup_list =list(tup)\n",
"type(tup_list)\n",
"\n",
"tup_list.append( \"r\")\n",
"tup_list.append( \"o\")\n",
"tup_list.append( \"n\")\n",
"tup_list.append( \"h\")\n",
"tup_list.append( \"a\")\n",
"tup_list.append( \"r\")\n",
"tup_list.append( \"k\")\n",
"\n",
"tup_list\n",
"\n",
"# Your explanation here\n",
"# tuple is immutable"
"# I can use list.append() to append a single value"
]
},
{
Expand All @@ -130,13 +157,23 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['I', 'r', 'o', 'n'] ['h', 'a', 'r', 'k']\n"
]
}
],
"source": [
"# Your code here\n",
"tup1 = (\"I\", \"r\", \"o\", \"n\")\n",
"tup2 = (\"h\", \"a\", \"c\", \"k\")"
"tup_list\n",
"tup1 = tup_list[:4]\n",
"tup2 = tup_list[-4:]\n",
"print(tup1,tup2)"
]
},
{
Expand All @@ -150,26 +187,27 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 24,
"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"
"['I', 'r', 'o', 'n', 'h', 'a', 'r', 'k']\n",
"tup are equal\n"
]
}
],
"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)"
"\n",
"if tup3 == tup1 + tup2:\n",
" print(\"tup are equal\")\n",
"else:\n",
" print(\"tup not equal\")\n"
]
},
{
Expand All @@ -181,26 +219,29 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"8\n",
"8\n",
"8\n"
"4\n",
"are equal\n"
]
}
],
"source": [
"# Your code here\n",
"print(len(tup1))\n",
"print(len(tup3))\n",
"print(len(tup1) + len(tup2))\n",
"print(len(tup3))"
"print(len(tup2))\n",
"if len(tup1) + len(tup2) == len(tup3):\n",
" print(\"are equal\")\n",
" \n",
"else:\n",
" print(\"not equal\")\n",
"\n"
]
},
{
Expand All @@ -212,24 +253,25 @@
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"execution_count": 31,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 32,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup3.index(\"h\")\n",
"##It's number 4"
"tup3.index(\"h\")\n"
]
},
{
Expand All @@ -249,33 +291,30 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[True, False, True, False, False]\n"
"True\n",
"False\n",
"False\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",
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"#if element in tup3 print(True) else print(False) \n",
"print(existing_letters)\n"
"for letter in letters:\n",
" if letter in tup3:\n",
" print(\"True\")\n",
" else:\n",
" print(\"False\")"
]
},
{
Expand All @@ -289,18 +328,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 44,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The letter 'a' appears in 'tup3' 1 times.\n",
"The letter 'b' appears in 'tup3' 0 times.\n",
"The letter 'c' appears in 'tup3' 1 times.\n",
"The letter 'd' appears in 'tup3' 0 times.\n",
"The letter 'e' appears in 'tup3' 0 times.\n"
]
}
],
"source": [
"# Your code here\n",
"\n"
"\n",
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"for letter in letters:\n",
" count = tup3.count(letter)\n",
" print(f\"The letter '{letter}' appears in 'tup3' {count} times.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -314,7 +370,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
Loading