Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Lab needs to be fixed along with greater details on functional inputs

Signed-off-by: Andy <[email protected]>
  • Loading branch information
Mw4mba authored Sep 8, 2024
1 parent 9e85eee commit 9c792d6
Showing 1 changed file with 292 additions and 0 deletions.
292 changes: 292 additions & 0 deletions python/week4-theory.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Week 2: Variables and Expressions - Python Course</title>
<link rel="icon" type="image/svg+xml" href="https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
display: flex;
flex-direction: column;
min-height: 100vh;
}

header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
display: flex;
align-items: center;
justify-content: space-between;
}

header img {
margin-right: 15px;
height: 40px;
}

.burger-menu {
cursor: pointer;
display: none;
flex-direction: column;
justify-content: space-around;
width: 30px;
height: 30px;
}

.burger-menu div {
width: 100%;
height: 3px;
background-color: #fff;
}

nav {
background-color: #555;
color: #fff;
padding: 10px;
text-align: center;
display: none;
flex-direction: column;
}

nav a {
color: #fff;
margin: 10px 0;
text-decoration: none;
font-weight: bold;
}

.container {
flex: 1;
padding: 20px;
max-width: 1200px;
margin: auto;
}

h1, h2 {
color: #0056b3;
}

h2 {
margin-top: 40px;
}

ul {
list-style-type: disc;
margin-left: 20px;
}

p, ul {
margin-bottom: 20px;
line-height: 1.6;
}

.section-title {
background-color: #e0e0e0;
padding: 10px;
border-radius: 5px;
margin-bottom: 10px;
}

footer {
background-color: #555;
color: #fff;
text-align: center;
padding: 10px;
width: 100%;
margin-top: 40px;
}

footer a {
color: #fff;
text-decoration: none;
margin: 0 10px;
}

.disclaimer {
margin-top: 20px;
font-size: 0.8em;
color: #888;
}

@media (max-width: 768px) {
header {
flex-direction: column;
}

header img {
margin: 0 0 10px 0;
}

nav {
display: flex;
}
}
</style>
</head>

<body>
<header>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg" alt="Python Logo">
<div class="burger-menu" id="burger-menu">
<div></div>
<div></div>
<div></div>
</div>
</header>

<nav id="nav">
<a href="index.html">Home</a>
<a href="activities.html">Activities</a>
<a href="assessment.html">Assessment</a>
<a href="https://courses.skunkworks.africa/">Training Home</a>
<a href="https://skunkworks.africa/">Skunkworks Home</a>
<a href="terms.html">Terms and Conditions</a>
</nav>

<div class="container">
<h1>Week 4: Functions</h1>
<h2 class="section-title">What is a function?</h2>
<p>
Functions are a set of recallable instructions, that perform and action or return a value.
are an important tool in programming that greatly increase the modularity and efficiency of your program. That means that a function is **a piece of code written to carry out a specified task**. Its easier to think of them as mini-programs that perform more particular actions, similar to how a factory may create a product, there is smaller machinery which helps create part of the product before its final assembly.
</p>

<h2 class="section-title">Creating functions</h2>
<h3>Keywords</h3>
<li><strong>def</strong>- Define function keyword</li>
<li><strong>return</strong>- Return variable value to global scope</li>


<h3>Declaring function</h3>
<p> Functions follow the following format:
<pre><code>
def functionName(arguments)
*code to be executed*
</code></pre>
Remember that in python indentation determines the scope. To ensure that the code to be executed is part of the function indent with 4 spaces or tab after defining the function.
Here is an example:
<pre><code>
def demoFunction():
print("This is a function")
</code></pre>
Functions will only run when called, you call a function by using its identifier and brackets to show that it is a function, e.g.
<pre><code>
demoFunction()
//Expected result: This is a function
</code></pre>

Functions are defined
<h3>Return statements</h3>
In most cases your functions will have to return a value, be it the output of a mathematical operation or e something else. However variables defined in functions exist only in the functions scope, so they can not be called outside of the function, but that is why we have the return keyword. This keyword 'return's' a value from the function to its parent scope. e.g.:
<pre><code>
def multiply(num1,num2):
product=num1*num2
return product
Using the return keyword returns the value stored in the product variable ( which in this case we expect to be a number ) as the output of the function

multiply(3,4)
// outputs 12
</code></pre>

<h2 class="section-title">Other functions</h2>
<h3>Arrow function</h3>
Now the previous function we had made is not made to return an output of a particular type, but the type is inferred to be number because we define product with an arithmetic operation. However because the type of input is not specified, a string could be entered as an argument and this could render the whole function useless. In order to get around it, we can use more explicitly define the types of variables we would want this function to return and leave less up to type inference( see variables and expressions)
we could use an arrow function to ensure that we get the intended output
<pre><code>
def multiply(num1, num2)->int:
product=num1*num2
return product
</code></pre>
A more type strict version of the previous function would be:

<pre><code>
def multiply(num1: int, num2: int)->int:
product=num1#num2
return product
</code></pre>
We can see the input types are defined in the brackets, <strong>num1: int</strong>
This is declaring that num1 can only hold a numerical values and any other type of input will be rejected. However what is interesting is that an arrow is used to "direct" the output to the type integer. This types the output making it easier to deal with.
<h3>Arrow function</h3>
Lambda function are shorthand functions, usually used as inputs to higher order functions( a more advanced topic). These cannot be recalled as they aren't intended to be recalled.
e.g.
<pre><code> lambda x: x/2</code></pre>
Here x is the input and will be entered into the function, this can be a useful shorthand for functions e.g.
<pre><code>
myFunct=lambda x:x/2
myFunct(4)
// will return 1
</code></pre>

Here is an example of a higher order function:
<pre><code>
def div_list(funct, myList)
divList=[]
for item in myList:
new_item=funct(new_item)
divList.append(new_item)
return(divList)
myList=[1, 2, 3, 4]

div_by_2= div_list(lambda x: x/2, myList)
print(div_by_2)
// This should output the list divided by two
</code></pre>
The aforementioned example is a bit advanced, but should open your eyes to the possibilities in python.
</p>
<h2 class="section-title">Scope and applicability of functions</h2>
<p>
Function variables exist only within the function, if the variable was initialized outside the function then the identifier is already taken, however a variable initialized in the function cannot be used outsider the function unless it is returned to the global scope.
When it comes to conditional/control structures, they follow a parent child relation ship when it comes to variable inheritance.
</p>
<h2 class="section-title">Practices of good measure</h2>
<p><ul>
<li>Use more strict typing whenever possible to ensure that there is less stress when debugging</li>
<li>Use appropriate naming schemes for your functions to improve the readability of your code</li>
<li>Have your void functions( functions which do not have a return value), return some sort of a value so you can ensure that they've ran.</li>
</ul></p>

<h2 class="section-title">Practical Lab Exercises</h2>
<p>Now that you have an understanding of variables and expressions, it's time to apply this knowledge in practical scenarios. The following exercises will help reinforce your learning.</p>
<ul>
<li><strong>Creating and Using Variables:</strong> Create various types of variables, assign values, and use them in expressions.</li>
<li><strong>Writing Expressions and Statements:</strong> Practice writing expressions that perform calculations and assign the results to variables.</li>
</ul>

<h2 class="section-title">Q&A and Review</h2>
<p>As you complete these exercises, it's important to review and clarify any concepts that are unclear. Use this section to revisit key ideas and ask questions.</p>
<p>If you're uncertain about any topics or need further explanation, don't hesitate to reach out through the discussion forum or during the Q&A sessions.</p>

</div>

<footer>
<p>&copy; 2024 Skunkworks. All Rights Reserved.</p>
<a href="terms.html">Terms and Conditions</a> |
<a href="https://www.skunkworks.africa/privacy">Privacy Policy</a> |
<a href="https://www.skunkworks.africa/cookie-policy">Cookie Policy</a>
<div class="disclaimer">
<p>Disclaimer: This theory guide is part of the Python programming course provided by Skunkworks. All rights and content are reserved by Skunkworks.</p>
<p>The content provided in this document is for educational purposes only and may not be reproduced or redistributed without permission.</p>
</div>
</footer>

<script>
const burgerMenu = document.getElementById('burger-menu');
const nav = document.getElementById('nav');

burgerMenu.addEventListener('click', () => {
nav.style.display = nav.style.display === 'flex' ? 'none' : 'flex';
});
</script>

</body>

</html>

0 comments on commit 9c792d6

Please sign in to comment.