Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tuple,Set,Dict]Fernando Carvalho #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
186 changes: 103 additions & 83 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n",
"tup = (\"I\",)"
"tup = ('I',)"
]
},
{
Expand All @@ -34,23 +34,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n"
]
}
],
"source": [
"# Your code here\n",
"type(tup)"
"print(type(tup))"
]
},
{
Expand All @@ -68,26 +65,14 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"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'"
]
}
],
"outputs": [],
"source": [
"# Your code here\n",
"tup.append(\"r\")\n",
"tup.append()\n",
"# Your explanation here\n",
"# tuple is immutable"
"#we cannot change the content of a tuple so, because of it, the atribute doesn't exist."
]
},
{
Expand All @@ -105,14 +90,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n",
"tup[0] = \"r\"\n",
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
"# Your explanation here\n",
"# tuple is immutable"
"#What is happening here is that we are giving the var \"tup\" an all new value. Like we would with any other variable."
]
},
{
Expand All @@ -130,13 +115,27 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 33,
"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\")"
"tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n",
" \n",
"tup1 = tup[:4]\n",
"print(tup1)\n",
"tup2 = tup[-4:]\n",
"print(tup2)\n",
"\n"
]
},
{
Expand All @@ -150,26 +149,25 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 40,
"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"
"Tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"Tup : ('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)"
"tup3 = tup1+tup2\n",
"\n",
"if (tup3 == tup):\n",
" print(\"Tup3:\",tup3)\n",
" print(\"Tup :\",tup)"
]
},
{
Expand All @@ -181,26 +179,28 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"8\n",
"8\n",
"8\n"
"The sum of 'tup1'and'tup2'is the same as'tup3'.\n"
]
}
],
"source": [
"# Your code here\n",
"print(len(tup1))\n",
"print(len(tup3))\n",
"print(len(tup1) + len(tup2))\n",
"print(len(tup3))"
"total_tup3 = len(tup3)\n",
"total_el1 = len(tup1)\n",
"total_el2 = len(tup2)\n",
"\n",
"total_sum = total_el1 + total_el2\n",
"\n",
"if total_tup3 == total_sum:\n",
" print(\"The sum of 'tup1'and'tup2'is the same as'tup3'.\")\n",
" "
]
},
{
Expand All @@ -212,24 +212,20 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"h\n"
]
}
],
"source": [
"# Your code here\n",
"tup3.index(\"h\")\n",
"##It's number 4"
"print(tup3[4])"
]
},
{
Expand All @@ -249,33 +245,34 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[True, False, True, False, False]\n"
"a : True\n",
"b : False\n",
"c : True\n",
"d : False\n",
"e : 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",
"#Tup3: ('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n",
"\n",
"#if element in tup3 print(True) else print(False) \n",
"print(existing_letters)\n"
"for l in letters:\n",
" if l in tup3:\n",
" print(l,\":\",True)\n",
" else:\n",
" print(l,\":\",False)\n",
" \n",
" "
]
},
{
Expand All @@ -289,18 +286,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 71,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a appears 1 time.\n",
"b appears 0 time.\n",
"c appears 1 time.\n",
"d appears 0 time.\n",
"e appears 0 time.\n"
]
}
],
"source": [
"# Your code here\n",
"\n"
"for l in letters:\n",
" cont=0\n",
" if l in tup3:\n",
" cont=cont+1\n",
" print(l,\"appears\",cont,\"time.\")\n"
]
},
{
"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,7 +334,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
Loading