forked from Pierian-Data/Complete-Python-3-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dea292
commit 6bdd11b
Showing
163 changed files
with
54,669 additions
and
0 deletions.
There are no files selected for viewing
713 changes: 713 additions & 0 deletions
713
...Object and Data Structure Basics/.ipynb_checkpoints/03-String Formatting-checkpoint.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
528 changes: 528 additions & 0 deletions
528
...Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
951 changes: 951 additions & 0 deletions
951
...pynb_checkpoints/10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
545 changes: 545 additions & 0 deletions
545
00-Python Object and Data Structure Basics/01-Numbers.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
1,003 changes: 1,003 additions & 0 deletions
1,003
00-Python Object and Data Structure Basics/02-Strings.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
713 changes: 713 additions & 0 deletions
713
00-Python Object and Data Structure Basics/03-String Formatting.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
758 changes: 758 additions & 0 deletions
758
00-Python Object and Data Structure Basics/04-Lists.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
440 changes: 440 additions & 0 deletions
440
00-Python Object and Data Structure Basics/05-Dictionaries.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
266 changes: 266 additions & 0 deletions
266
00-Python Object and Data Structure Basics/06-Tuples.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.