diff options
Diffstat (limited to 'tutorials')
| -rw-r--r-- | tutorials/1_04_spyder_getting_started.md | 2 | ||||
| -rw-r--r-- | tutorials/1_05_basics_of_python.md | 57 | ||||
| -rw-r--r-- | tutorials/1_06_arrays.md | 56 | ||||
| -rw-r--r-- | tutorials/1_07_control_structures.md | 3 | ||||
| -rw-r--r-- | tutorials/1_computational_expense.md | 2 | ||||
| -rw-r--r-- | tutorials/1_functions.md | 11 |
6 files changed, 96 insertions, 35 deletions
diff --git a/tutorials/1_04_spyder_getting_started.md b/tutorials/1_04_spyder_getting_started.md index 08cbde9..df141bf 100644 --- a/tutorials/1_04_spyder_getting_started.md +++ b/tutorials/1_04_spyder_getting_started.md @@ -1,6 +1,6 @@ # Getting started with Spyder -In this tutorial we will cover the basics of the Spyder IDE (Interactive Development Environment). If you've ever worked with MATLAB before, then this will feel familiar. Spyder is a program +In this tutorial we will cover the basics of using the Spyder IDE (Interactive Development Environment). If you've ever worked with MATLAB before, then this will feel familiar. Spyder is a program that allows you to write, run and de-bug code. ## Launching Spyder diff --git a/tutorials/1_05_basics_of_python.md b/tutorials/1_05_basics_of_python.md index 59a9c63..5cc9aee 100644 --- a/tutorials/1_05_basics_of_python.md +++ b/tutorials/1_05_basics_of_python.md @@ -2,13 +2,23 @@ This page contains important fundamental concepts used in Python such as syntax, operators, order or precedence and more. +[Syntax](1_05_basics_of_python#Syntax) +[Operators](1_05_basics_of_python#Operators) +[Order of operation](1_05_basics_of_python#Order%20of%20Operation) +[Data types](1_05_basics_of_python#data%20types) +[Variables](1_05_basics_of_python#Variables) + +--- ## Syntax ### Indentations and blocks In python *indentations* or the space at the start of each line, signifies a block of code. This becomes important when we start working with function and loops. We will talk more about this in the controls structures tutorial. ### Comments Comments can be added to your code using the hash operator (#). Any text behind the comment operator till the end of the line will be rendered as a comment. -If you have an entire block of text or code that needs to be commented out, the triple quotation marks (""") can be used. Once used all the code after it will be considered a comment until the comment is ended with the triple quotation marks. +If you have an entire block of text or code that needs to be commented out, the triple quotation marks (""") can be used. Once used all the code after it will be considered a comment until the comment is ended with the triple quotation marks.f + +--- ## Operators +In python, operators are special symbols or keywords that perform operations on values or variables. This section covers some of the most common operator that you will see in this course. ### Arithmetic operators | Operator | Name | | --- | --- | @@ -22,6 +32,8 @@ If you have an entire block of text or code that needs to be commented out, the ### Comparison operators +Used in conditional statements such as `if` statements or `while` loops. Note that in the computer world a double equal sign (`==`) means *is equal to*, where as the single equal sign assigns the variable or defines the variable to be something. + | Operator | Name | | --- | --- | | == | Equal | @@ -44,27 +56,33 @@ If you have an entire block of text or code that needs to be commented out, the | is | Returns True if both variables are the same object | | is not | Returns True if both variables are not the same object | +--- ## Order of Operation -| Operator | Description | -| --- | --- | -| () | Parentheses | -| ** | Exponentiation | -| * / // % | Multiplication, Division, floor division, and modulus | -| & | AND | -| ^ | XOR | -| \| | OR | -| == | Comparision, identity and membership operators | -| not | logical NOT | -| and | AND | -| or | OR | - - +Similarly to the order or precedence in mathematics, different computer languages have their own set of rules. Here is a comprehensive table of the order of operation that python follows. + +| Operator | Description | +| ------------------------------------------------------- | ----------------------------------------------------- | +| `()` | Parentheses | +| `**` | Exponentiation | +| `+x` `-x` `~x` | Unary plus, unary minus, and bitwise NOT | +| `*` `/` `//` `%` | Multiplication, Division, floor division, and modulus | +| `+` `-` | Addition and subtraction | +| `<<` `>>` | Bitwise left and right shifts | +| & | Bitwise AND | +| ^ | Bitwise XOR | +| \| | Bitwise OR | +| `==` `!=` `>` `>=` `<` `<=` `is` `is not` `in` `not in` | Comparision, identity and membership operators | +| `not` | logical NOT | +| `and` | AND | +| `or` | OR | + +--- ## Data types Data types are different ways a computer stores data. Other data types use fewer bits than others allowing you to better utilize your computer memory. This is important for engineers because The most common data types that an engineer encounters in python are numeric types. -- 'int' - integer -- 'float' - a decimal number -- 'complex' - imaginary number +- `int` - integer +- `float` - a decimal number +- `complex` - imaginary number The comprehensive table below show all built-in data types available in python. @@ -78,13 +96,14 @@ The comprehensive table below show all built-in data types available in python. | Binary | bytes, bytearray, memoryview | | None | NoneType | +--- ## Variables A **variable** in Python is a name that stores a value, allowing you to use and manipulate data efficiently. #### Declaring and Assigning Variables -In Python, you don’t need to declare the type of a variable. Just assign a value using `=`. +It is common in low-level computer languages to declare the datatype if the variable. In python, the datatype is set whilst you assign it. We assign values to variables using a single `=`. ```python x = 10 # Integer diff --git a/tutorials/1_06_arrays.md b/tutorials/1_06_arrays.md index b16e7fc..76dd1ee 100644 --- a/tutorials/1_06_arrays.md +++ b/tutorials/1_06_arrays.md @@ -2,14 +2,15 @@ In computer programming, an array is a structure for storing and retrieving data. We often talk about an array as if it were a grid in space, with each cell storing one element of the data. For instance, if each element of the data were a number, we might visualize a “one-dimensional” array like a list: -| 1 | 5 | 2 | 0 | +| 1 | 5 | 2 | 0 | +| --- | --- | --- | --- | A two-dimensional array would be like a table: -| 1 | 5 | 2 | 0 | -| 8 | 3 | 6 | 1 | -| 1 | 7 | 2 | 9 | - +| 1 | 5 | 2 | 0 | +| --- | --- | --- | --- | +| 8 | 3 | 6 | 1 | +| 1 | 7 | 2 | 9 | A three-dimensional array would be like a set of tables, perhaps stacked as though they were printed on separate pages. @@ -17,30 +18,59 @@ A three-dimensional array would be like a set of tables, perhaps stacked as thou --- -In this tutorial we will be introducing arrays and we will be using the numpy library. Arrays, lists, vectors, matrices, sets - You might've heard of them before, they all store data. In programming, an array is a variable that can hold more than one value at a time. We will be using the Numpy python library to create arrays. +In this tutorial we will be introducing arrays and we will be using the numpy library. Arrays, lists, vectors, matrices, sets - You might've heard of them before, they all store data. In programming, an array is a variable that can hold more than one value at a time. We will be using the Numpy python library to create arrays. Since we already have installed Numpy previously, we can start using the package. -Since we already have installed Numpy previously, we can start using the package. +Before importing our first package, let's as ourselves *what is a package?* A package can be thought of as pre-written python code that we can re-use. This means the for every script that we write in python we need to tell it to use a certain package. We call this importing a package. -## Import Numpy +## Importing Numpy When using packages in python, we need to let it know what package we will be using. This is called importing. To import numpy we need to declare it a the start of a script as follows: ```python import numpy as np ``` -<code> import </code> calls for a library to use, in our case it is Numpy. -<code> as </code> gives the library an alias in your script. It's common convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects. +- `import` - calls for a library to use, in our case it is Numpy. +- `as` - gives the library an alias in your script. It's common convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects. # Creating arrays -Now that we have imported the library we can create a one dimensional array or *vector* +Now that we have imported the library we can create a one dimensional array or *vector* with three elements. ```python x = np.array([1,2,3]) ``` -To create a *matrix* we can nest the arrays to create a two dimensional array. +To create a *matrix* we can nest the arrays to create a two dimensional array. This is done as follows. ```python -matrix = np.array([[1,2,3],[4,5,6],[7,8,9]]) +matrix = np.array([[1,2,3], + [4,5,6], + [7,8,9]]) ``` *Note: for every array we nest, we get a new dimension in our data structure.* +## Numpy array creation functions +### np.arange +The `np.arange()` function returns an array with evenly spaced values within a specified range. It is similar to the built-in `range()` function in Python but returns a Numpy array instead of a list. The parameters for this function are the start value (inclusive), the stop value (exclusive), and the step size. If the step size is not provided, it defaults to 1. + +```python +>>> np.arange(4) +array([0. , 1., 2., 3. ]) +``` + +In this example, `np.arange(4)` generates an array starting from 0 and ending before 4, with a step size of 1. + +### np.linspace +The `np.linspace()` function returns an array of evenly spaced values over a specified range. Unlike `np.arange()`, which uses a step size to define the spacing between elements, `np.linspace()` uses the number of values you want to generate and calculates the spacing automatically. It accepts three parameters: the start value, the stop value, and the number of samples. + +```python +>>> np.linspace(1., 4., 6) +array([1. , 1.6, 2.2, 2.8, 3.4, 4. ]) +``` + +In this example, `np.linspace(1., 4., 6)` generates 6 evenly spaced values between 1. and 4., including both endpoints. + +Try this and see what happens: + +```python +x = np.linspace(0,100,101) +y = np.sin(x) +``` diff --git a/tutorials/1_07_control_structures.md b/tutorials/1_07_control_structures.md index 245ef5a..21826bb 100644 --- a/tutorials/1_07_control_structures.md +++ b/tutorials/1_07_control_structures.md @@ -1,6 +1,9 @@ # Control Structures Control structures allow us to control the flow of execution in a Python program. The two main types are **conditional statements (`if` statements)** and **loops (`for` and `while` loops)**. +## Flowcharts + + ## Conditional statements Conditional statements let you execute code based on different conditions using if, elif and else statements. diff --git a/tutorials/1_computational_expense.md b/tutorials/1_computational_expense.md new file mode 100644 index 0000000..c9631e1 --- /dev/null +++ b/tutorials/1_computational_expense.md @@ -0,0 +1,2 @@ +# Computational Expense + diff --git a/tutorials/1_functions.md b/tutorials/1_functions.md index edbd3a2..8235fe3 100644 --- a/tutorials/1_functions.md +++ b/tutorials/1_functions.md @@ -2,7 +2,7 @@ Like a traditional mathematical functions, python functions can take an input, process it, and give an output. In python, the input variables are referred to as *arguments*. Functions are blocks of code that is run every time it's called. This allows us to re-use code. -Functions are defined by using the <code> def </code> keyword. Reminder: it is important to keep track of indentation as it signify the end of the function when the indentation. +Functions are defined by using the <code> def </code> keyword. Reminder: it is important to keep track of indentations as it signifies the end of the function when the indentation returns back to the same level. ## Defining Functions ### Simple function @@ -39,8 +39,15 @@ Now that we've covered defining functions we want to call the function in order function(2,-1) ``` -Note that when running this code, nothing happens. This is because we haven't told the computer to print the result. +Note that when running this code, nothing happens. This is because we haven't told the computer what to do with the output. Hence, if we wish to store the output then we need to use the assign operator `=`. +```python +output = function(2,-1) + +print(output) +``` + +In case you want to return multiple output variable from a single function we will have... ## Summary |
