diff --git a/_posts/2021-11-01-lesson-3.md b/_posts/2021-11-01-lesson-3.md deleted file mode 100644 index 8557c40..0000000 --- a/_posts/2021-11-01-lesson-3.md +++ /dev/null @@ -1,260 +0,0 @@ ---- -layout: default -title: November 1 - Programming Basics -date: 2021-11-01 00.00.00 -0400 -categories: group-a -current_page: lessons -code_editor: true -// published: false ---- -# November 1 - Programming Basics - -Welcome to the fourth lesson of Woodlands Computer Science! -Presentation Slides are available [here](https://docs.google.com/presentation/d/1q_tvtd2422gJFqQ2y3P0ENGRsfu5QHNdPjT_tv3edWU/edit?usp=sharing) (use your PDSB account to view the slides) - -
- -## Table of Contents - -In this meeting, we'll be covering: - -- Functions -- User Input -- Booleans -- Conditional Statements - -
- -## What Are Functions? - - -Functions in programming are a set of instructions grouped together to achieve a **specific task**. You can pass data **into** a function (known as function **parameters**) and the function can **return** data. - -We’ve already been unknowingly exposed to functions - Python has **built-in functions** that we’ve used throughout the last few lessons. - -Here are some examples: -| Built-in Function | Description | -|:-----------------:|:--------------------------------------------------------------------------------------:| -| print() | Prints to the standard output device (display messages and information on the screen). | -| str() | Returns the string version of the object which is passed in. | -| range() | Returns a sequence of numbers, starting from 0. | -| abs() | Returns the absolute value of a number. | -
- - -## Why Use Functions? - -* Functions make our code more **organized** and **readable.** -* Allow us to **reuse the same code** in different parts of the program. - * For example, a function could be created to compute the volume of a sphere if we needed to reuse this calculation throughout our program. - -**Syntax of a Function** - -```Python -def func_name(parameters): - -``` - -1. The `def `keyword marks the start of the function header. -2. Next, the name of the function, which follows the rules of writing identifiers in [Python](https://www.programiz.com/python-programming/keywords-identifier#rules). -3. The function name **must** be followed by parentheses with any number of parameters inside. -4. A semicolon to mark the end of the function header. -5. At least one line of indented code that will execute when the function is called. -6. If want our function to **return** data, we can use the `return `keyword inside our function. - - -## Example 1 -In our first meeting, we created a program (**Pythagorean Theorem**) that calculated the value of $\sqrt{a^2+b^2}$. - -If we needed to compute this value multiple times in our program, we could create a function instead of rewriting the code! - -```Python -def calculate_hypotenuse(a, b): - return (a**2 + b**2)**0.5 -``` - -**In this example**: - -* **calculate_hypotenuse** is the name of the function. -* **a** and **b** are the parameters/arguments of the function. -* `return (a**2 + b**2)**0.5 `is the code that executes when our function is called. - * Note the `return `keyword - this means that our function **returns** data whenever it is called. - -
-a = 3 -b = 4 -def calculate_hypotenuse(a, b): - return (a**2 + b**2)**0.5 -print(calculate_hypotenuse(a, b)) -print(calculate_hypotenuse(5, 12)) -
- - -## Scope of Functions - -The “**scope**” of a function refers to where in the program the variable is recognized. For functions, the **parameters** and **variables declared inside** have a **local** scope. This means that they cannot be accessed outside of the function. - -**Example:** - - - -
-def cool_function(): - x = 10 - print(f'Inside: x = {x}') -x = 20 -cool_function() -print(f'Outside: x = {x}') -
- - -What if we want to modify or access a variable **outside** the local scope of our function? - -We can use the `global` keyword. By placing `global` before any global variable name inside a function, it will be assigned its value and any modifications will **not **be local. - -**A more intuitive explanation is through an example**: - - - -
-x = 10 -def add_ten(): - global x - x = x + 10 -add_ten() -print(x) -
- - -## Activity - - - ---- - -## Using Comments in Code - -Comments are used to provide **explanatory information **about the source code of a program. - -Comments keep your code **readable**, especially if you plan to revisit your program in the future or share it with others. - -In Python, single line comments are created by preceding the comment with ‘`#`’ and multi-line comments are created by enclosing the comments with a pair of triple apostrophes (''' or """). - -```Python -def calculate_hypotenuse(a, b): - ''' - Parameters: - a: the first leg of the triangle - b: the second leg of the triangle - Returns: - The hypotenuse of the triangle - ''' - return (a**2 + b**2)**0.5 # single line comment -``` - - ---- - -## Reading Text Files - -Sometimes, instead of user input, we want to read information from a file instead. If we have a file named `a.txt `which contains: - -``` -Hello! -Line 1. -Line 2. -``` - -1. We can use Python’s built-in `open() `function to open the file. -2. To read a file, we can specify the text file’s path and name, and then pass in `'r' `as the function’s second argument. -3. Finally, we use the .`read() `method to see the contents of the file. - -**Example**: - - - - -If we pass an integer **N** into the .`read()` method, we will only read the **first N characters**. - - - -The .`readline() `method returns one line of the text file. - - - -If we want to get rid of the new lines or any whitespace at the start or end of a line, we can use Python’s built-in .`strip()` method - - - -It is often useful to loop through a text file line by line. - - - - -## Writing to a Text File - -Sometimes, we want to output to a text file instead of to the screen. Once again, we will employ Python’s built-in `open() `function. - -However, the second parameter of the function depends on what you’re trying to achieve: - -| Second Parameter | Description | -|:----------------:|:-----------------------------------------------------------------------------------------------------:| -| 'a' | Adds to the end of the file, and creates a new file if the specified file does not exist. | -| 'w' | Overwrites any existing content of the file, creates a new file if the specified file does not exist. | -| 'x' | Creates a new file, throws an error if the specified file already exists. | -| abs() | Returns the absolute value of a number. | -
- -If we wanted to add more text to the end of `a.txt`, we would use the following code: - -```Python -text = open('a.txt', 'a') -``` - -Now that we have opened the file, we can use the `.write()` method to add any text to the file! - -Note that for the text file to save, you need to use the .close() method after you are finished editing the file. - - - - -## Activity - -
- ---- - -## Try/Except - -Usually, errors in our code would stop the execution of our program. However, if we want to **catch and handle these errors**, we can use the `try` and `except` block. - -The format of a `try` and `except` block is as follows (**indentation is needed**): - -**Example:** - - - - -We can also specify a specific error to catch using `except`. Consequently, like `if/else `statements, we can stack `except` statements: - - - -We can use an `else` statement after `except` which will execute if **no errors** were encountered. - - - -
-try: - name = "Jeffrey" - print(name) -except: - print("Program didn't crash :)") -else: - print("Else block!") -
- -
- -## Activity - - diff --git a/assets/img/lesson-11-01/example1.png b/assets/img/lesson-11-01/example1.png deleted file mode 100644 index 3a98238..0000000 Binary files a/assets/img/lesson-11-01/example1.png and /dev/null differ diff --git a/assets/img/lesson-11-01/pythagorean.png b/assets/img/lesson-11-01/pythagorean.png deleted file mode 100644 index c346fbd..0000000 Binary files a/assets/img/lesson-11-01/pythagorean.png and /dev/null differ diff --git a/assets/img/lesson-11-01/readtxtfile1.png b/assets/img/lesson-11-01/readtxtfile1.png deleted file mode 100644 index f437541..0000000 Binary files a/assets/img/lesson-11-01/readtxtfile1.png and /dev/null differ diff --git a/assets/img/lesson-11-01/readtxtfile2.png b/assets/img/lesson-11-01/readtxtfile2.png deleted file mode 100644 index 7079940..0000000 Binary files a/assets/img/lesson-11-01/readtxtfile2.png and /dev/null differ diff --git a/assets/img/lesson-11-01/readtxtfile3.png b/assets/img/lesson-11-01/readtxtfile3.png deleted file mode 100644 index 367bc76..0000000 Binary files a/assets/img/lesson-11-01/readtxtfile3.png and /dev/null differ diff --git a/assets/img/lesson-11-01/readtxtfile4.png b/assets/img/lesson-11-01/readtxtfile4.png deleted file mode 100644 index b1be6e1..0000000 Binary files a/assets/img/lesson-11-01/readtxtfile4.png and /dev/null differ diff --git a/assets/img/lesson-11-01/readtxtfile5.png b/assets/img/lesson-11-01/readtxtfile5.png deleted file mode 100644 index 3c02b58..0000000 Binary files a/assets/img/lesson-11-01/readtxtfile5.png and /dev/null differ diff --git a/assets/img/lesson-11-01/scope1.png b/assets/img/lesson-11-01/scope1.png deleted file mode 100644 index cb85f0a..0000000 Binary files a/assets/img/lesson-11-01/scope1.png and /dev/null differ diff --git a/assets/img/lesson-11-01/scope2.png b/assets/img/lesson-11-01/scope2.png deleted file mode 100644 index bb67d6e..0000000 Binary files a/assets/img/lesson-11-01/scope2.png and /dev/null differ diff --git a/assets/img/lesson-11-01/tryexcept1.png b/assets/img/lesson-11-01/tryexcept1.png deleted file mode 100644 index c1eaebb..0000000 Binary files a/assets/img/lesson-11-01/tryexcept1.png and /dev/null differ diff --git a/assets/img/lesson-11-01/tryexcept2.png b/assets/img/lesson-11-01/tryexcept2.png deleted file mode 100644 index 1e07536..0000000 Binary files a/assets/img/lesson-11-01/tryexcept2.png and /dev/null differ diff --git a/assets/img/lesson-11-01/tryexcept3.png b/assets/img/lesson-11-01/tryexcept3.png deleted file mode 100644 index 24b59dc..0000000 Binary files a/assets/img/lesson-11-01/tryexcept3.png and /dev/null differ diff --git a/assets/img/lesson-11-01/writetxtfile1.png b/assets/img/lesson-11-01/writetxtfile1.png deleted file mode 100644 index e32259b..0000000 Binary files a/assets/img/lesson-11-01/writetxtfile1.png and /dev/null differ