Skip to content

Commit a8d0ba8

Browse files
committed
Tuple
1 parent d4e6c8d commit a8d0ba8

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

Diff for: 06.Tuple.ipynb

+46-21
Original file line numberDiff line numberDiff line change
@@ -222,37 +222,62 @@
222222
},
223223
{
224224
"cell_type": "code",
225-
"execution_count": 15,
225+
"execution_count": 17,
226226
"metadata": {},
227227
"outputs": [
228228
{
229-
"ename": "NameError",
230-
"evalue": "name 'cmp' is not defined",
229+
"name": "stdout",
230+
"output_type": "stream",
231+
"text": [
232+
"Yes, 'apple' is in the fruits tuple\n"
233+
]
234+
}
235+
],
236+
"source": [
237+
"thistuple = (\"apple\", \"banana\", \"cherry\")\n",
238+
"if \"apple\" in thistuple:\n",
239+
" print(\"Yes, 'apple' is in the fruits tuple\") "
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": 18,
245+
"metadata": {},
246+
"outputs": [
247+
{
248+
"name": "stdout",
249+
"output_type": "stream",
250+
"text": [
251+
"3\n"
252+
]
253+
}
254+
],
255+
"source": [
256+
" thistuple = (\"apple\", \"banana\", \"cherry\")\n",
257+
"print(len(thistuple))"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 20,
263+
"metadata": {},
264+
"outputs": [
265+
{
266+
"ename": "TypeError",
267+
"evalue": "'tuple' object does not support item assignment",
231268
"output_type": "error",
232269
"traceback": [
233270
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
234-
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
235-
"\u001b[0;32m<ipython-input-15-a9bcf8d03232>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mtuple2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'coder'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mcmp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtuple1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# cmp() returns 0 if matched, 1 when not tuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
236-
"\u001b[0;31mNameError\u001b[0m: name 'cmp' is not defined"
271+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
272+
"\u001b[0;32m<ipython-input-20-fe88f2910dbf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mthistuple\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"apple\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"banana\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"cherry\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mthistuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"orange\"\u001b[0m \u001b[0;31m# This will raise an error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthistuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
273+
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
237274
]
238275
}
239276
],
240277
"source": [
241-
"tuple1 = ('python', 'geek') \n",
242-
"tuple2 = ('coder', 1) \n",
243-
" \n",
244-
"if (cmp(tuple1, tuple2) != 0): \n",
245-
" \n",
246-
" # cmp() returns 0 if matched, 1 when not tuple1 \n",
247-
" # is longer and -1 when tuple1 is shoter \n",
248-
" print('Not the same') \n",
249-
"else: \n",
250-
" print('Same') \n",
251-
"print ('Maximum element in tuples 1,2: ' + \n",
252-
" str(max(tuple1)) + ',' + \n",
253-
" str(max(tuple2))) \n",
254-
"print ('Minimum element in tuples 1,2: ' + \n",
255-
" str(min(tuple1)) + ',' + str(min(tuple2))) "
278+
"thistuple = (\"apple\", \"banana\", \"cherry\")\n",
279+
"thistuple[3] = \"orange\" # This will raise an error\n",
280+
"print(thistuple)"
256281
]
257282
},
258283
{

0 commit comments

Comments
 (0)