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
380 changes: 380 additions & 0 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,380 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Challenge 1: Tuples\n",
"\n",
"#### Do you know you can create tuples with only one element?\n",
"\n",
"**In the cell below, define a variable `tup` with a single element `\"I\"`.**\n",
"\n",
"*Hint: you need to add a comma (`,`) after the single element.*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('I',)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tup=(\"I\",)\n",
"tup\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Print the type of `tup`. \n",
"\n",
"Make sure its type is correct (i.e. *tuple* instead of *str*)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n"
]
}
],
"source": [
"print(type(tup))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Now try to append the following elements to `tup`. \n",
"\n",
"Are you able to do it? Explain.\n",
"\n",
"```\n",
"\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k',\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"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)",
"Input \u001b[1;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mtup\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mo\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mn\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mh\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mc\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk\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\",\"o\",\"n\",\"h\",\"a\",\"c\",\"k\",)\n",
"\n",
"# Your explanation here\n",
"# A diferencia de las listas y los diccionarios las tuplas son inmutables."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### How about re-assign a new value to an existing tuple?\n",
"\n",
"Re-assign the following elements to `tup`. Are you able to do it? Explain.\n",
"\n",
"```\n",
"\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [7]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m tup[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mR\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 3\u001b[0m tup\n",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"# Your code here\n",
"tup[1]=\"R\"\n",
"tup\n",
"# Your explanation here\n",
"#no podemos asignar nuevos valores a una tupla porque es inmutable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Split `tup` into `tup1` and `tup2` with 4 elements in each. \n",
"\n",
"`tup1` should be `(\"I\", \"r\", \"o\", \"n\")` and `tup2` should be `(\"h\", \"a\", \"c\", \"k\")`.\n",
"\n",
"*Hint: use positive index numbers for `tup1` assignment and use negative index numbers for `tup2` assignment. Positive index numbers count from the beginning whereas negative index numbers count from the end of the sequence.*\n",
"\n",
"Also print `tup1` and `tup2`."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n')\n",
"('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"# Your code here\n",
"tup=(\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\",)\n",
"tup1=tup[0:4]\n",
"print(tup1)\n",
"tup2=tup[-4:]\n",
"print(tup2)\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Add `tup1` and `tup2` into `tup3` using the `+` operator.\n",
"\n",
"Then print `tup3` and check if `tup3` equals to `tup`."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tup3= tup1+tup2\n",
"tup3\n",
"tup3 == tup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Count the number of elements in `tup1` and `tup2`. Then add the two counts together and check if the sum is the same as the number of elements in `tup3`"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(tup1) + len(tup2) == len(tup)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### What is the index number of `\"h\"` in `tup3`?"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"index=tup3.index(\"h\")\n",
"print(index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n",
"\n",
"```\n",
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"```\n",
"\n",
"For each letter you check, print `True` if it is present in `tup3` otherwise print `False`.\n",
"\n",
"*Hint: you only need to loop `letters`. You don't need to loop `tup3` because there is a Python operator `in` you can use. See [reference](https://stackoverflow.com/questions/17920147/how-to-check-if-a-tuple-contains-an-element-in-python).*"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TRUE\n",
"FALSE\n",
"TRUE\n",
"FALSE\n",
"FALSE\n"
]
}
],
"source": [
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"x=0\n",
"for i in letters:\n",
" if letters[x] in tup3:\n",
" print(\"TRUE\")\n",
" else:\n",
" print(\"FALSE\")\n",
" x=x+1\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### How many times does each letter in `letters` appear in `tup3`?\n",
"\n",
"Print out the number of occurrence of each letter."
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 1\n",
"b = 0\n",
"c = 1\n",
"d = 0\n",
"e = 0\n"
]
}
],
"source": [
"# Your code here\n",
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"x=0\n",
"y=0\n",
"count=0\n",
"for i in letters:\n",
" if letters[x] in tup3:\n",
" for j in tup3:\n",
" if letters[x] == tup3[y]:\n",
" count=count+1\n",
" y=y+1\n",
" print(letters[x],\"=\",count)\n",
" y=0\n",
" else:\n",
" print(letters[x],\"= 0\")\n",
" x=x+1\n",
" count=0\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading