Skip to content

Commit 6bdd11b

Browse files
committed
PYTHON 3 UPDATES
1 parent 5dea292 commit 6bdd11b

File tree

163 files changed

+54669
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+54669
-0
lines changed

00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-String Formatting-checkpoint.ipynb

+713
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb

+528
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/.ipynb_checkpoints/10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb

+951
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/01-Numbers.ipynb

+545
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/02-Strings.ipynb

+1,003
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/03-String Formatting.ipynb

+713
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/04-Lists.ipynb

+758
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/05-Dictionaries.ipynb

+440
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Tuples\n",
8+
"\n",
9+
"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",
10+
"\n",
11+
"In this section, we will get a brief overview of the following:\n",
12+
"\n",
13+
" 1.) Constructing Tuples\n",
14+
" 2.) Basic Tuple Methods\n",
15+
" 3.) Immutability\n",
16+
" 4.) When to Use Tuples\n",
17+
"\n",
18+
"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",
19+
"\n",
20+
"## Constructing Tuples\n",
21+
"\n",
22+
"The construction of a tuples use () with elements separated by commas. For example:"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 1,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"# Create a tuple\n",
32+
"t = (1,2,3)"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"data": {
42+
"text/plain": [
43+
"3"
44+
]
45+
},
46+
"execution_count": 2,
47+
"metadata": {},
48+
"output_type": "execute_result"
49+
}
50+
],
51+
"source": [
52+
"# Check len just like a list\n",
53+
"len(t)"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 3,
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"data": {
63+
"text/plain": [
64+
"('one', 2)"
65+
]
66+
},
67+
"execution_count": 3,
68+
"metadata": {},
69+
"output_type": "execute_result"
70+
}
71+
],
72+
"source": [
73+
"# Can also mix object types\n",
74+
"t = ('one',2)\n",
75+
"\n",
76+
"# Show\n",
77+
"t"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 4,
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"data": {
87+
"text/plain": [
88+
"'one'"
89+
]
90+
},
91+
"execution_count": 4,
92+
"metadata": {},
93+
"output_type": "execute_result"
94+
}
95+
],
96+
"source": [
97+
"# Use indexing just like we did in lists\n",
98+
"t[0]"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 5,
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"data": {
108+
"text/plain": [
109+
"2"
110+
]
111+
},
112+
"execution_count": 5,
113+
"metadata": {},
114+
"output_type": "execute_result"
115+
}
116+
],
117+
"source": [
118+
"# Slicing just like a list\n",
119+
"t[-1]"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"## Basic Tuple Methods\n",
127+
"\n",
128+
"Tuples have built-in methods, but not as many as lists do. Let's look at two of them:"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 6,
134+
"metadata": {},
135+
"outputs": [
136+
{
137+
"data": {
138+
"text/plain": [
139+
"0"
140+
]
141+
},
142+
"execution_count": 6,
143+
"metadata": {},
144+
"output_type": "execute_result"
145+
}
146+
],
147+
"source": [
148+
"# Use .index to enter a value and return the index\n",
149+
"t.index('one')"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 7,
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"1"
161+
]
162+
},
163+
"execution_count": 7,
164+
"metadata": {},
165+
"output_type": "execute_result"
166+
}
167+
],
168+
"source": [
169+
"# Use .count to count the number of times a value appears\n",
170+
"t.count('one')"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"metadata": {},
176+
"source": [
177+
"## Immutability\n",
178+
"\n",
179+
"It can't be stressed enough that tuples are immutable. To drive that point home:"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 8,
185+
"metadata": {},
186+
"outputs": [
187+
{
188+
"ename": "TypeError",
189+
"evalue": "'tuple' object does not support item assignment",
190+
"output_type": "error",
191+
"traceback": [
192+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
193+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
194+
"\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",
195+
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
196+
]
197+
}
198+
],
199+
"source": [
200+
"t[0]= 'change'"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it."
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 9,
213+
"metadata": {},
214+
"outputs": [
215+
{
216+
"ename": "AttributeError",
217+
"evalue": "'tuple' object has no attribute 'append'",
218+
"output_type": "error",
219+
"traceback": [
220+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
221+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
222+
"\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",
223+
"\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
224+
]
225+
}
226+
],
227+
"source": [
228+
"t.append('nope')"
229+
]
230+
},
231+
{
232+
"cell_type": "markdown",
233+
"metadata": {},
234+
"source": [
235+
"## When to use Tuples\n",
236+
"\n",
237+
"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",
238+
"\n",
239+
"You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n",
240+
"\n",
241+
"Up next Files!"
242+
]
243+
}
244+
],
245+
"metadata": {
246+
"kernelspec": {
247+
"display_name": "Python 3",
248+
"language": "python",
249+
"name": "python3"
250+
},
251+
"language_info": {
252+
"codemirror_mode": {
253+
"name": "ipython",
254+
"version": 3
255+
},
256+
"file_extension": ".py",
257+
"mimetype": "text/x-python",
258+
"name": "python",
259+
"nbconvert_exporter": "python",
260+
"pygments_lexer": "ipython3",
261+
"version": "3.6.2"
262+
}
263+
},
264+
"nbformat": 4,
265+
"nbformat_minor": 1
266+
}

0 commit comments

Comments
 (0)