|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "# Packages" |
| 7 | + "# Custom Modules" |
8 | 8 | ] |
9 | 9 | }, |
10 | 10 | { |
|
16 | 16 | "* functions\n", |
17 | 17 | "* types\n", |
18 | 18 | "* collections\n", |
19 | | - "* loops" |
| 19 | + "* packages" |
20 | 20 | ] |
21 | 21 | }, |
22 | 22 | { |
|
25 | 25 | "source": [ |
26 | 26 | "# Modules\n", |
27 | 27 | "\n", |
28 | | - "Before describing what a Python package is, we must first understand Python modules. We are now comfortable with writing simple Python code and may now want to write our first dedicated program. But what is the best way to set out our code? One of the key concepts in programming is the creation of simple and easy to maintain code, this is where modules come in. \n", |
29 | | - "\n", |
30 | | - "Instead of creating one large piece of code that can often be unwieldy we can break it up into smaller chunks, or modules. Each module has a specific functionality that allows it to be reused without the need for duplicating code." |
| 28 | + "Instead of creating one large piece of code that can often be unwieldy we can break it up into smaller chunks, or modules. Modules can be accessed through installed packages or they can written by the user. " |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "## Custom Modules" |
31 | 36 | ] |
32 | 37 | }, |
33 | 38 | { |
34 | 39 | "cell_type": "markdown", |
35 | 40 | "metadata": {}, |
36 | 41 | "source": [ |
37 | | - "Below is an example module called `print_things.py`:" |
| 42 | + "Below is an example of a custom module (i.e. one we have writeen ourselves) called `print_things.py`:" |
38 | 43 | ] |
39 | 44 | }, |
40 | 45 | { |
|
100 | 105 | }, |
101 | 106 | { |
102 | 107 | "cell_type": "code", |
103 | | - "execution_count": 1, |
| 108 | + "execution_count": null, |
104 | 109 | "metadata": {}, |
105 | | - "outputs": [ |
106 | | - { |
107 | | - "output_type": "stream", |
108 | | - "name": "stdout", |
109 | | - "text": "Hydrogen\nHelium\nLithium\nKey: Hydrogen, Value: 1\nKey: Helium, Value: 2\nKey: Lithium, Value: 3\n" |
110 | | - } |
111 | | - ], |
| 110 | + "outputs": [], |
112 | 111 | "source": [ |
113 | 112 | "import print_things\n", |
114 | 113 | "\n", |
|
121 | 120 | "print_things.print_dictionary(element_protons)" |
122 | 121 | ] |
123 | 122 | }, |
124 | | - { |
125 | | - "cell_type": "markdown", |
126 | | - "metadata": {}, |
127 | | - "source": [ |
128 | | - "### Using `from`" |
129 | | - ] |
130 | | - }, |
131 | 123 | { |
132 | 124 | "cell_type": "markdown", |
133 | 125 | "metadata": {}, |
|
137 | 129 | }, |
138 | 130 | { |
139 | 131 | "cell_type": "code", |
140 | | - "execution_count": 2, |
| 132 | + "execution_count": null, |
141 | 133 | "metadata": {}, |
142 | | - "outputs": [ |
143 | | - { |
144 | | - "output_type": "stream", |
145 | | - "name": "stdout", |
146 | | - "text": "Hydrogen\nHelium\nLithium\n" |
147 | | - } |
148 | | - ], |
| 134 | + "outputs": [], |
149 | 135 | "source": [ |
150 | 136 | "from print_things import print_names\n", |
151 | 137 | "\n", |
|
170 | 156 | "Another useful way to use both the `import` / `from` statements is to `import` modules and use an alternative name:\n", |
171 | 157 | "\n", |
172 | 158 | "```python\n", |
173 | | - "from print_things import print_names as pn # import print_names from the print_things module \n", |
174 | | - "import print_things as pt # import the whole print_things module\n", |
| 159 | + "from print_things import print_names as pn # Import print_names from the print_things module \n", |
| 160 | + "import print_things as pt # Import the whole print_things module\n", |
175 | 161 | "``` " |
176 | 162 | ] |
177 | 163 | }, |
178 | 164 | { |
179 | 165 | "cell_type": "markdown", |
180 | 166 | "metadata": {}, |
181 | 167 | "source": [ |
182 | | - "# Packages" |
| 168 | + "## Organising Modules: Packages" |
183 | 169 | ] |
184 | 170 | }, |
185 | 171 | { |
|
221 | 207 | "\n", |
222 | 208 | "Luckily we usually don't need to worry about where packages are as we can use a dedicated package manager to automatically take care of their location for us. These managers are useful and contain a huge variety of different packages that aren't included within Python as standard. For example, packages such as `NumPy` are commonly retrieved this way. \n" |
223 | 209 | ] |
224 | | - }, |
225 | | - { |
226 | | - "cell_type": "markdown", |
227 | | - "metadata": {}, |
228 | | - "source": [ |
229 | | - "# Python Package Managers" |
230 | | - ] |
231 | | - }, |
232 | | - { |
233 | | - "cell_type": "markdown", |
234 | | - "metadata": {}, |
235 | | - "source": [ |
236 | | - "TODO\n", |
237 | | - "\n", |
238 | | - "pip\n", |
239 | | - "\n", |
240 | | - "conda\n" |
241 | | - ] |
242 | 210 | } |
243 | 211 | ], |
244 | 212 | "metadata": { |
|
0 commit comments