Skip to content

Commit

Permalink
2020 Updates Phase ONE
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierian-Data committed Jun 13, 2020
1 parent 96a89f1 commit eff30b2
Show file tree
Hide file tree
Showing 218 changed files with 12,901 additions and 3,138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,42 @@
"\n",
"On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again. If you remember back to the lessons on strings and lists, remember that we used a function len() to get the length of a string. Since checking the length of a sequence is a common task you would want to write a function that can do this repeatedly at command.\n",
"\n",
"Functions will be one of most basic levels of reusing code in Python, and it will also allow us to start thinking of program design (we will dive much deeper into the ideas of design when we learn about Object Oriented Programming)."
"Functions will be one of most basic levels of reusing code in Python, and it will also allow us to start thinking of program design (we will dive much deeper into the ideas of design when we learn about Object Oriented Programming).\n",
"\n",
"## Function Topics\n",
"* def keyword\n",
"* simple example of a function\n",
"* calling a function with ()\n",
"* accepting parameters\n",
"* print versus return\n",
"* adding in logic inside a function\n",
"* multiple returns inside a function\n",
"* adding in loops inside a function\n",
"* tuple unpacking\n",
"* interactions between functions\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## def Statements\n",
"### def keyword\n",
"\n",
"Let's see how to build out a function's syntax in Python. It has the following form:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def name_of_function(arg1,arg2):\n",
" '''\n",
" This is where the function's Document String (docstring) goes\n",
" This is where the function's Document String (docstring) goes.\n",
" When you call help() on your function it will be printed out.\n",
" '''\n",
" # Do stuff here\n",
" # Return desired result"
Expand All @@ -46,13 +61,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We begin with <code>def</code> then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a [built-in function in Python](https://docs.python.org/2/library/functions.html) (such as len).\n",
"We begin with <code>def</code> then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a [built-in function in Python](https://docs.python.org/3/library/functions.html) (such as len).\n",
"\n",
"Next come a pair of parentheses with a number of arguments separated by a comma. These arguments are the inputs for your function. You'll be able to use these inputs in your function and reference them. After this you put a colon.\n",
"\n",
"Now here is the important step, you must indent to begin the code inside your function correctly. Python makes use of *whitespace* to organize code. Lots of other programing languages do not do this, so keep that in mind.\n",
"\n",
"Next you'll see the docstring, this is where you write a basic description of the function. Using iPython and iPython Notebooks, you'll be able to read these docstrings by pressing Shift+Tab after a function name. Docstrings are not necessary for simple functions, but it's good practice to put them in so you or other people can easily understand the code you write.\n",
"Next you'll see the docstring, this is where you write a basic description of the function. Using Jupyter and Jupyter Notebooks, you'll be able to read these docstrings by pressing Shift+Tab after a function name. Docstrings are not necessary for simple functions, but it's good practice to put them in so you or other people can easily understand the code you write.\n",
"\n",
"After all this you begin writing the code you wish to execute.\n",
"\n",
Expand All @@ -63,19 +78,28 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 1: A simple print 'hello' function"
"### Simple example of a function"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def say_hello():\n",
" print('hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calling a function with ()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -85,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -104,23 +128,52 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 2: A simple greeting function\n",
"Let's write a function that greets people with their name."
"If you forget the parenthesis (), it will simply display the fact that say_hello is a function. Later on we will learn we can actually pass in functions into other functions! But for now, simply remember to call functions with ()."
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.say_hello>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"say_hello"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accepting parameters (arguments)\n",
"Let's write a function that greets people with their name."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def greeting(name):\n",
" print('Hello %s' %(name))"
" print(f'Hello {name}')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -140,15 +193,19 @@
"metadata": {},
"source": [
"## Using return\n",
"So far we've only seen print() used, but if we actually want to save the resulting variable we need to use the **return** keyword.\n",
"\n",
"Let's see some example that use a <code>return</code> statement. <code>return</code> allows a function to *return* a result that can then be stored as a variable, or used in whatever manner a user wants.\n",
"\n",
"### Example 3: Addition function"
"### Example: Addition function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def add_num(num1,num2):\n",
Expand Down Expand Up @@ -178,7 +235,9 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Can also save as variable due to return\n",
Expand Down Expand Up @@ -238,6 +297,38 @@
"Let's also start using <code>break</code>, <code>continue</code>, and <code>pass</code> statements in our code. We introduced these during the <code>while</code> lecture."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Check if a number is even "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Check if a number is even or odd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -252,7 +343,9 @@
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def is_prime(num):\n",
Expand Down Expand Up @@ -307,29 +400,7 @@
"source": [
"Note how the <code>else</code> lines up under <code>for</code> and not <code>if</code>. This is because we want the <code>for</code> loop to exhaust all possibilities in the range before printing our number is prime.\n",
"\n",
"Also note how we break the code after the first print statement. As soon as we determine that a number is not prime we break out of the <code>for</code> loop.\n",
"\n",
"We can actually improve this function by only checking to the square root of the target number, and by disregarding all even numbers after checking for 2. We'll also switch to returning a boolean value to get an example of using return statements:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"\n",
"def is_prime2(num):\n",
" '''\n",
" Better method of checking for primes. \n",
" '''\n",
" if num % 2 == 0 and num > 2: \n",
" return False\n",
" for i in range(3, int(math.sqrt(num)) + 1, 2):\n",
" if num % i == 0:\n",
" return False\n",
" return True"
"Also note how we break the code after the first print statement. As soon as we determine that a number is not prime we break out of the <code>for</code> loop.\n"
]
},
{
Expand All @@ -352,13 +423,6 @@
"is_prime2(18)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why don't we have any <code>break</code> statements? It should be noted that as soon as a function *returns* something, it shuts down. A function can deliver multiple print statements, but it will only obey one <code>return</code>."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -383,7 +447,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.6.6"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit eff30b2

Please sign in to comment.