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
159 changes: 98 additions & 61 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I',)\n"
]
},
{
"data": {
"text/plain": [
Expand All @@ -50,6 +57,7 @@
],
"source": [
"# Your code here\n",
"print(tup)\n",
"type(tup)"
]
},
Expand All @@ -72,22 +80,22 @@
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"ename": "IndentationError",
"evalue": "unexpected indent (3258976191.py, line 2)",
"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[0;36m Cell \u001b[0;32mIn[3], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m tup.append(\"r\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n"
]
}
],
"source": [
"# Your code here\n",
"tup.append(\"r\")\n",
" tup.append(\"r\")\n",
"\n",
"# Your explanation here\n",
"# tuple is immutable"
" # will raise an error\n",
" # not possible as tuples are inmmutable objects\n",
" # not .append attribute for tuples"
]
},
{
Expand All @@ -105,14 +113,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup[0] = \"r\"\n",
"\n",
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"\n",
"print(tup)\n",
"\n",
"# Your explanation here\n",
"# tuple is immutable"
" # it is not possible to change a a tuple, but you can reassign it completely\n",
"\n"
]
},
{
Expand All @@ -130,13 +151,25 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 5,
"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 = (\"I\", \"r\", \"o\", \"n\")\n",
"tup2 = (\"h\", \"a\", \"c\", \"k\")"
"tup1 = tuple(tup[:4])\n",
"tup2 = tuple(tup[-4:])\n",
"\n",
"print(tup1)\n",
"print(tup2)"
]
},
{
Expand All @@ -150,26 +183,22 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 6,
"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', 'c', 'k')\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",
"print(tup3)"
]
},
{
Expand All @@ -181,26 +210,24 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"8\n",
"8\n",
"8\n"
]
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"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))"
"len(tup3) == len(tup1) + len(tup2)\n",
"\n"
]
},
{
Expand All @@ -212,7 +239,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -221,15 +248,14 @@
"4"
]
},
"execution_count": 32,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"tup3.index(\"h\")\n",
"##It's number 4"
"tup3.index('h')"
]
},
{
Expand All @@ -249,33 +275,30 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 9,
"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",
"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')\n"
]
},
{
Expand All @@ -289,18 +312,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 1, 'b': 0, 'c': 1, 'd': 0, 'e': 0}\n"
]
}
],
"source": [
"# Your code here\n",
"\n"
"letter_occurence = {}\n",
"\n",
"for letter in letters:\n",
" letter_count = tup3.count(letter)\n",
" letter_occurence[letter] = letter_count\n",
"\n",
"print(letter_occurence)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -314,7 +351,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down
Loading