Skip to content

Commit 930d1e7

Browse files
committed
collect
1 parent de9ff5f commit 930d1e7

6 files changed

+463
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#Advanced Functions Quiz"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": []
18+
}
19+
],
20+
"metadata": {
21+
"kernelspec": {
22+
"display_name": "Python 2",
23+
"language": "python",
24+
"name": "python2"
25+
},
26+
"language_info": {
27+
"codemirror_mode": {
28+
"name": "ipython",
29+
"version": 2
30+
},
31+
"file_extension": ".py",
32+
"mimetype": "text/x-python",
33+
"name": "python",
34+
"nbconvert_exporter": "python",
35+
"pygments_lexer": "ipython2",
36+
"version": "2.7.10"
37+
}
38+
},
39+
"nbformat": 4,
40+
"nbformat_minor": 0
41+
}

.ipynb_checkpoints/Collections Module-checkpoint.ipynb

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,188 @@
488488
"print d1 == d2"
489489
]
490490
},
491+
{
492+
"cell_type": "markdown",
493+
"metadata": {},
494+
"source": [
495+
"#namedtuple\n",
496+
"The standard tuple uses numerical indexes to access its members, for example:"
497+
]
498+
},
491499
{
492500
"cell_type": "code",
493-
"execution_count": null,
501+
"execution_count": 38,
502+
"metadata": {
503+
"collapsed": true
504+
},
505+
"outputs": [],
506+
"source": [
507+
"t = (12,13,14)"
508+
]
509+
},
510+
{
511+
"cell_type": "code",
512+
"execution_count": 39,
513+
"metadata": {
514+
"collapsed": false
515+
},
516+
"outputs": [
517+
{
518+
"data": {
519+
"text/plain": [
520+
"12"
521+
]
522+
},
523+
"execution_count": 39,
524+
"metadata": {},
525+
"output_type": "execute_result"
526+
}
527+
],
528+
"source": [
529+
"t[0]"
530+
]
531+
},
532+
{
533+
"cell_type": "markdown",
534+
"metadata": {},
535+
"source": [
536+
"For simple use cases, this is usually enough. On the other hand, remembering which index should be used for each value can lead to errors, especially if the tuple has a lot of fields and is constructed far from where it is used. A namedtuple assigns names, as well as the numerical index, to each member. \n",
537+
"\n",
538+
"Each kind of namedtuple is represented by its own class, created by using the namedtuple() factory function. The arguments are the name of the new class and a string containing the names of the elements.\n",
539+
"\n",
540+
"You can basically think of namedtuples as a very quick way of creating a new object/class type with some attribute fields.\n",
541+
"For example:"
542+
]
543+
},
544+
{
545+
"cell_type": "code",
546+
"execution_count": 40,
547+
"metadata": {
548+
"collapsed": true
549+
},
550+
"outputs": [],
551+
"source": [
552+
"from collections import namedtuple"
553+
]
554+
},
555+
{
556+
"cell_type": "code",
557+
"execution_count": 47,
494558
"metadata": {
495559
"collapsed": true
496560
},
497561
"outputs": [],
498-
"source": []
562+
"source": [
563+
"Dog = namedtuple('Dog','age breed name')\n",
564+
"\n",
565+
"sam = Dog(age=2,breed='Lab',name='Sammy')\n",
566+
"\n",
567+
"frank = Dog(age=2,breed='Shepard',name=\"Frankie\")"
568+
]
569+
},
570+
{
571+
"cell_type": "markdown",
572+
"metadata": {},
573+
"source": [
574+
"We construct the namedtuple by first passing the object type name (Dog) and then passing a string with the variety of fields as a string with spaces between the field names. We can then call on the various attributes:"
575+
]
576+
},
577+
{
578+
"cell_type": "code",
579+
"execution_count": 42,
580+
"metadata": {
581+
"collapsed": false
582+
},
583+
"outputs": [
584+
{
585+
"data": {
586+
"text/plain": [
587+
"Dog(age=2, breed='Lab', name='Sammy')"
588+
]
589+
},
590+
"execution_count": 42,
591+
"metadata": {},
592+
"output_type": "execute_result"
593+
}
594+
],
595+
"source": [
596+
"sam"
597+
]
598+
},
599+
{
600+
"cell_type": "code",
601+
"execution_count": 43,
602+
"metadata": {
603+
"collapsed": false
604+
},
605+
"outputs": [
606+
{
607+
"data": {
608+
"text/plain": [
609+
"2"
610+
]
611+
},
612+
"execution_count": 43,
613+
"metadata": {},
614+
"output_type": "execute_result"
615+
}
616+
],
617+
"source": [
618+
"sam.age"
619+
]
620+
},
621+
{
622+
"cell_type": "code",
623+
"execution_count": 44,
624+
"metadata": {
625+
"collapsed": false
626+
},
627+
"outputs": [
628+
{
629+
"data": {
630+
"text/plain": [
631+
"'Lab'"
632+
]
633+
},
634+
"execution_count": 44,
635+
"metadata": {},
636+
"output_type": "execute_result"
637+
}
638+
],
639+
"source": [
640+
"sam.breed"
641+
]
642+
},
643+
{
644+
"cell_type": "code",
645+
"execution_count": 45,
646+
"metadata": {
647+
"collapsed": false
648+
},
649+
"outputs": [
650+
{
651+
"data": {
652+
"text/plain": [
653+
"2"
654+
]
655+
},
656+
"execution_count": 45,
657+
"metadata": {},
658+
"output_type": "execute_result"
659+
}
660+
],
661+
"source": [
662+
"sam[0]"
663+
]
664+
},
665+
{
666+
"cell_type": "markdown",
667+
"metadata": {},
668+
"source": [
669+
"##Conclusion\n",
670+
"\n",
671+
"Hopefully you now see how incredibly useful the collections module is in Python and it should be your go-to module for a variety of common tasks!"
672+
]
499673
}
500674
],
501675
"metadata": {

.ipynb_checkpoints/Final Capstone Project-checkpoint.ipynb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"#Final Capstone Projects\n",
10+
"\n",
11+
"Go to this link for the Descriptions and Code Outlines of several Capstone Projects!"
12+
]
13+
},
314
{
415
"cell_type": "code",
516
"execution_count": null,

Advanced Functions Quiz.ipynb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#Advanced Functions Quiz"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": []
18+
}
19+
],
20+
"metadata": {
21+
"kernelspec": {
22+
"display_name": "Python 2",
23+
"language": "python",
24+
"name": "python2"
25+
},
26+
"language_info": {
27+
"codemirror_mode": {
28+
"name": "ipython",
29+
"version": 2
30+
},
31+
"file_extension": ".py",
32+
"mimetype": "text/x-python",
33+
"name": "python",
34+
"nbconvert_exporter": "python",
35+
"pygments_lexer": "ipython2",
36+
"version": "2.7.10"
37+
}
38+
},
39+
"nbformat": 4,
40+
"nbformat_minor": 0
41+
}

0 commit comments

Comments
 (0)