Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions, Conditionals, and Iteration in Python\n",
"\n",
"Let us create a Python function, and call it from a loop."
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World, x was < 10\n",
"3\n"
]
}
],
"source": [
"def HelloWorldXY(x, y):\n",
" if (x < 10):\n",
" print(\"Hello World, x was < 10\")\n",
" elif (x < 20):\n",
" print(\"Hello World, x was >= 10 but < 20\")\n",
" else:\n",
" print(\"Hello World, x was >= 20\")\n",
" return x + y\n",
"\n",
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us call the function `HelloWorldXY()` from a loop:"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Now running with i: 8\n",
"Hello World, x was < 10\n",
"Result from HelloWorld: 16\n",
"\n",
"--- Now running with i: 13\n",
"Hello World, x was >= 10 but < 20\n",
"Result from HelloWorld: 26\n",
"\n",
"--- Now running with i: 18\n",
"Hello World, x was >= 10 but < 20\n",
"Result from HelloWorld: 36\n",
"\n",
"--- Now running with i: 23\n",
"Hello World, x was >= 20\n",
"Result from HelloWorld: 46\n"
]
}
],
"source": [
"for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)\n",
" print(\"\\n--- Now running with i: {}\".format(i))\n",
" r = HelloWorldXY(i,i)\n",
" print(\"Result from HelloWorld: {}\".format(r))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want a loop starting at 0 to 2 (exclusive) you could do any of the following:"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iterate over the items. `range(2)` is like a list [0,1].\n",
"0\n",
"1\n",
"Iterate over an actual list.\n",
"0\n",
]
}
],
"source": [
"print(\"Iterate over the items. `range(2)` is like a list [0,1].\")\n",
"for i in range(2):\n",
" print(i)\n",
"\n",
"print(\"Iterate over an actual list.\")\n",
"for i in [0,1]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"While works\n",
"0\n",
"1\n",
"Python supports standard key words like continue and break\n",
"Entered while\n",
"while broken\n"
]
}
],
"source": [
"print(\"While works\")\n",
"i = 0\n",
"while i < 2:\n",
" print(i)\n",
" i += 1\n",
" \n",
"print(\"Python supports standard key words like continue and break\")\n",
"while True:\n",
" print(\"Entered while\")\n",
" break\n",
"print(\"while broken\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NumPy\n",
"\n",
"### Introducing NumPy\n",
"\n",
"Python is convenient, but it can also be slow. However, it does \n",
"allow you to access libraries that execute faster code written in \n",
"languages like C. NumPy is one such library: it provides fast alternatives \n",
"to math operations in Python and is designed to work efficiently with \n",
"groups of numbers - like matrices.\n",
"\n",
"NumPy is a large library and we are only going to scratch the surface \n",
"of it here. If you plan on doing much math with Python, you should \n",
"definitely spend some time exploring its documentation to learn more.\n",
"\n",
"### Importing Numpy\n",
"\n",
"When importing the NumPy library, the convention you will see \n",
"used most often - including here - is to name it `np`, like so:"
]
},
{
"cell_type": "code",
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now you can use the library by prefixing the names of functions and \n",
"types with `np`, which you will see in the following examples.\n",
"\n",
"### Data Types and Shapes\n",
"\n",
"The most common way to work with numbers in NumPy is through `ndarray` \n",
"objects. They are similar to Python lists, but can have any number of \n",
"dimensions. Also, `ndarray` supports fast math operations, which \n",
"is just what we want.\n",
"\n",
"Since it can store any number of dimensions, you can use `ndarrays` \n",
"to represent any of the data types : scalars, vectors, \n",
"matrices, or tensors. "
]
},
{
Loading
Loading full blame...