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
195 changes: 162 additions & 33 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n"
"tup = (\"I\", )\n",
"\n",
"\n",
"\n"
]
},
{
Expand All @@ -32,11 +35,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 56,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n"
]
}
],
"source": [
"# Your code here\n"
"print(type(single_tuple))\n"
]
},
{
Expand All @@ -54,13 +65,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 59,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[59], line 1\u001b[0m\n\u001b[1;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[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"# Your code here\n",
"tup.append(\"r\")\n",
"\n",
"# Your explanation here\n"
"Elements cannot be appended to tuples directly. \n",
"In order to append elements to a tuple, they have to be converted into a list.\n",
"\n"
]
},
{
Expand All @@ -80,13 +105,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 84,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The tuple is: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"\n",
"# Your explanation here\n"
"list_of_a_tuple = list(tup)\n",
"list_of_a_tuple.append(\"r\")\n",
"list_of_a_tuple.append(\"o\")\n",
"list_of_a_tuple.append(\"n\")\n",
"list_of_a_tuple.append(\"h\")\n",
"list_of_a_tuple.append(\"a\")\n",
"list_of_a_tuple.append(\"c\")\n",
"list_of_a_tuple.append(\"k\")\n",
"new_tuple = tuple(list_of_a_tuple)\n",
"print(\"The tuple is:\", new_tuple)\n"
]
},
{
Expand All @@ -104,11 +144,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 86,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tup1 is ('I', 'r', 'o', 'n')\n",
"Tup2 is ('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n"
"tup1 = new_tuple[0:4]\n",
"tup2 = new_tuple[-4:]\n",
"\n",
"print(\"Tup1 is\", tup1)\n",
"print(\"Tup2 is\", tup2)\n"
]
},
{
Expand All @@ -122,11 +175,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 88,
"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 +200,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 95,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of elements of tup1 is 4\n",
"The number of elements of tup2 is 4\n",
"The number of elements of tup3 is 8\n"
]
}
],
"source": [
"# Your code here\n"
"count_tup1 = len(tup1)\n",
"count_tup2 = len(tup2)\n",
"print(\"The number of elements of tup1 is\", count_tup1)\n",
"print(\"The number of elements of tup2 is\", count_tup2)\n",
"count_tup3 = len(tup3)\n",
"print(\"The number of elements of tup3 is\", count_tup3)"
]
},
{
Expand All @@ -154,11 +231,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 103,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The index number of 'h' in tup3 is 4\n"
]
}
],
"source": [
"# Your code here\n"
"x = tup3.index(\"h\")\n",
"print(\"The index number of 'h' in tup3 is\", x)\n"
]
},
{
Expand All @@ -178,11 +264,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 124,
"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",
"for i in letters:\n",
" if i in tup3:\n",
" print(\"True\")\n",
" else:\n",
" print(\"False\")\n",
" "
]
},
{
Expand All @@ -203,12 +307,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 151,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Repeated letter is a , 1 times\n",
" \n",
"Repeated letter is c , 2 times\n",
" \n",
" \n"
]
}
],
"source": [
"# Your code here\n"
"count = 0\n",
"for i in letters:\n",
" if i in tup3:\n",
" count +=1\n",
" print(\"Repeated letter is\", i, \",\", count, \"times\")\n",
" else:\n",
" print(\" \")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -227,7 +356,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.11.5"
},
"toc": {
"base_numbering": 1,
Expand Down
Loading