Skip to content

Commit

Permalink
PYTHON 3 UPDATES
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierian-Data committed Feb 12, 2018
1 parent 5dea292 commit 6bdd11b
Show file tree
Hide file tree
Showing 163 changed files with 54,669 additions and 0 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions 00-Python Object and Data Structure Basics/01-Numbers.ipynb

Large diffs are not rendered by default.

1,003 changes: 1,003 additions & 0 deletions 00-Python Object and Data Structure Basics/02-Strings.ipynb

Large diffs are not rendered by default.

713 changes: 713 additions & 0 deletions 00-Python Object and Data Structure Basics/03-String Formatting.ipynb

Large diffs are not rendered by default.

758 changes: 758 additions & 0 deletions 00-Python Object and Data Structure Basics/04-Lists.ipynb

Large diffs are not rendered by default.

440 changes: 440 additions & 0 deletions 00-Python Object and Data Structure Basics/05-Dictionaries.ipynb

Large diffs are not rendered by default.

266 changes: 266 additions & 0 deletions 00-Python Object and Data Structure Basics/06-Tuples.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tuples\n",
"\n",
"In Python tuples are very similar to lists, however, unlike lists they are *immutable* meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar. \n",
"\n",
"In this section, we will get a brief overview of the following:\n",
"\n",
" 1.) Constructing Tuples\n",
" 2.) Basic Tuple Methods\n",
" 3.) Immutability\n",
" 4.) When to Use Tuples\n",
"\n",
"You'll have an intuition of how to use tuples based on what you've learned about lists. We can treat them very similarly with the major distinction being that tuples are immutable.\n",
"\n",
"## Constructing Tuples\n",
"\n",
"The construction of a tuples use () with elements separated by commas. For example:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Create a tuple\n",
"t = (1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check len just like a list\n",
"len(t)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('one', 2)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can also mix object types\n",
"t = ('one',2)\n",
"\n",
"# Show\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'one'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use indexing just like we did in lists\n",
"t[0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Slicing just like a list\n",
"t[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Tuple Methods\n",
"\n",
"Tuples have built-in methods, but not as many as lists do. Let's look at two of them:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use .index to enter a value and return the index\n",
"t.index('one')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use .count to count the number of times a value appears\n",
"t.count('one')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Immutability\n",
"\n",
"It can't be stressed enough that tuples are immutable. To drive that point home:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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)",
"\u001b[1;32m<ipython-input-8-1257c0aa9edd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;34m'change'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"t[0]= 'change'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it."
]
},
{
"cell_type": "code",
"execution_count": 9,
"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)",
"\u001b[1;32m<ipython-input-9-b75f5b09ac19>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'nope'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"t.append('nope')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## When to use Tuples\n",
"\n",
"You may be wondering, \"Why bother using tuples when they have fewer available methods?\" To be honest, tuples are not used as often as lists in programming, but are used when immutability is necessary. If in your program you are passing around an object and need to make sure it does not get changed, then a tuple becomes your solution. It provides a convenient source of data integrity.\n",
"\n",
"You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n",
"\n",
"Up next Files!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading

0 comments on commit 6bdd11b

Please sign in to comment.