Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active June 23, 2017 00:11
Show Gist options
  • Save mGalarnyk/cf328cc86fbb69c77ffca03b53067788 to your computer and use it in GitHub Desktop.
Save mGalarnyk/cf328cc86fbb69c77ffca03b53067788 to your computer and use it in GitHub Desktop.
Python tuple, tuple manipulation, and fibonacci sequence tutorial for the blog post: https://medium.com/@GalarnykMichael/python-basics-9-tuples-tuple-manipulation-and-the-fibonacci-sequence-2d0da4e2d326
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 align=\"center\"> Tuples </h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed (immutable) unlike lists (mutable). <br> Tuples use parentheses, whereas lists use square brackets."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# empty tuple written as two parentheses containing nothing\n",
"tup1 = (); \n",
"\n",
"# empty list\n",
"list1 = []"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# To write a tuple containing a single value you have to include a comma, even though there is only one value\n",
"tup1 = 50, ;\n",
"type(tup1)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<type 'tuple'>\n"
]
}
],
"source": [
"tup1 = 'Please', 'Subscribe';\n",
"print(type(tup1))\n",
"tup2 = ('pretty please', );\n",
"tup3 = \"a\", \"b\", \"c\", \"d\";"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Accessing Values in Tuples:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example − <br>\n",
"tup3 = \"a\", \"b\", \"c\", \"d\";"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"tup3 | \"a\" | \"b\" | \"c\" | \"d\"\n",
"--- | --- | --- | --- | ---\n",
"index | 0 | 1 | 2 | 3"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('a', 'b', 'c', 'd')"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tup3"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('a', 'b')"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# first index is inclusive (before the :) and last (after the :) is not. \n",
"# not including index 2\n",
"tup3[0:2]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('a', 'b', 'c')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# everything up to index 3\n",
"tup3[:3]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('b', 'c', 'd')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# index 1 to end of tuple\n",
"tup3[1:]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'c'"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Negative: count from the right\n",
"tup3[-2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Updating Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Please', 'Subscribe')"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tup1"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('pretty please',)"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tup2"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Please', 'Subscribe', 'pretty please')\n"
]
}
],
"source": [
"# So let's create a new tuple as follows\n",
"new_tuple = tup1 + tup2;\n",
"print(new_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python Expression | Result | Description\n",
"--- | --- | ---\n",
"tup1 + tup2 | ('Please', 'Subscribe', 'pretty please') | Concatenation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sequence Unpacking"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"tuples are useful for sequence unpacking <br>"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Value of x , y : ', 7, 10)\n"
]
}
],
"source": [
"x, y = (7, 10);\n",
"print(\"Value of x , y : \", x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Emunerate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Returns a tuple containing a count for every iteration (from start which defaults to 0) and the values obtained from iterating over sequence:"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 'steve')\n",
"(1, 'rachel')\n",
"(2, 'michael')\n",
"(3, 'adam')\n",
"(4, 'monica')\n"
]
}
],
"source": [
"friends = ('steve', 'rachel', 'michael', 'adam', 'monica')\n",
"for index, friend in enumerate(friends):\n",
" print(index,friend)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task: Generating Fibonacci Sequence in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fibonacci sequence is an integer sequence characterized by the fact that every number after the first two is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1 (<b>which is how I like to code it</b>), or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 1 2 3 5 8 13 21 34 55\n"
]
}
],
"source": [
"print(1, 1, 2, 3, 5, 8, 13, 21, 34, 55)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Using looping technique, write a Python program which prints out the first 10 Fibonacci numbers"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Fib(a): ', 1, 'b is: ', 1)\n",
"('Fib(a): ', 1, 'b is: ', 2)\n",
"('Fib(a): ', 2, 'b is: ', 3)\n",
"('Fib(a): ', 3, 'b is: ', 5)\n",
"('Fib(a): ', 5, 'b is: ', 8)\n",
"('Fib(a): ', 8, 'b is: ', 13)\n",
"('Fib(a): ', 13, 'b is: ', 21)\n",
"('Fib(a): ', 21, 'b is: ', 34)\n",
"('Fib(a): ', 34, 'b is: ', 55)\n",
"('Fib(a): ', 55, 'b is: ', 89)\n"
]
}
],
"source": [
"# Note, there are better ways to code this which I will go over in later videos\n",
"a,b = 1,1\n",
"for i in range(10):\n",
" print(\"Fib(a): \", a, \"b is: \", b)\n",
" a,b = b,a+b "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://www.youtube.com/watch?v=gUHeaQ0qZaw"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment