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
392 changes: 392 additions & 0 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,392 @@
{
"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": [],
"source": [
"tup=('I',)\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))"
]
},
{
"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": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'a', 'c', 'k')\n"
]
}
],
"source": [
"ti=['r','o','n','a','c','k']\n",
"\n",
"lista_1=list(tup)\n",
"\n",
"lista_2=tuple(lista_1+ti)\n",
"\n",
"print(lista_2)\n",
"\n"
]
},
{
"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": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"parte_1=list(lista_2[0:4])\n",
"\n",
"parte_2=list(lista_2[4:])\n",
"\n",
"parte_3=[\"h\"]\n",
"\n",
"lista_4=tuple(parte_1+parte_3+parte_2)\n",
"\n",
"print(lista_4)"
]
},
{
"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": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n')\n",
"('h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"tup1=lista_4[0:4]\n",
"\n",
"tup2=lista_4[4:]\n",
"\n",
"print(tup1)\n",
"print(tup2)"
]
},
{
"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": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n"
]
}
],
"source": [
"tup3=tup1+tup2\n",
"print(tup3)"
]
},
{
"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": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"x=len(tup1)\n",
"y=len(tup2)\n",
"\n",
"if (x + y ==len(tup3)):\n",
" \n",
" print(True)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### What is the index number of `\"h\"` in `tup3`?"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"for i in range(len(tup3)):\n",
" \n",
" if tup3[i] == 'h':\n",
" \n",
" print(i)"
]
},
{
"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": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True a esa letra si esta\n",
"False b esa letra no esta\n",
"True c esa letra si esta\n",
"False d esa letra no esta\n",
"False e esa letra no esta\n"
]
}
],
"source": [
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"\n",
"\n",
"for i in letters:\n",
" \n",
" x=0\n",
" \n",
" for k in tup3:\n",
" \n",
" if k == i:\n",
" \n",
" x+=1 \n",
" \n",
" break\n",
" \n",
" else:\n",
" \n",
" x=x\n",
" if x>0:\n",
" \n",
" print(True,i,'esa letra si esta')\n",
" \n",
" else:\n",
" \n",
" print(False,i, 'esa letra no esta')\n",
" \n",
" \n",
" \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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a esta 1 veces\n",
"b esta 0 veces\n",
"c esta 1 veces\n",
"d esta 0 veces\n",
"e esta 0 veces\n"
]
}
],
"source": [
"letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
"\n",
"\n",
"\n",
"for i in letters:\n",
" \n",
" x=0\n",
" \n",
" for k in tup3:\n",
" \n",
" if k == i:\n",
" \n",
" x+=1 \n",
" \n",
" break\n",
" \n",
" else:\n",
" \n",
" x=x\n",
" if x>0:\n",
" \n",
" print(i, 'esta',x, 'veces')\n",
" \n",
" else:\n",
" \n",
" print(i, 'esta',x, 'veces')\n",
" \n",
" \n",
" \n"
]
}
],
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading