Skip to content
Permalink
Browse files
Push some of my changes before todays review meeting
  • Loading branch information
Barnsa committed Jul 13, 2020
1 parent 815dfc3 commit 951d7e6ef26db4b3df61392dd56c4dde5f620a85
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
@@ -2,6 +2,11 @@
In python many variables that you create, whether they be basic ones or composite variables, need to be compared or evaluated at some point. If you have a variable in your code that isn't, then you probably don't need that variable.

## Evaluation
In a python program, each line of the program is evaluated from top to bottom and left to right, unless order of precedents is in effect, with few exceptions. Common evaluations are things such as addition and subtraction, consider this code:

{{ code_from_file("conditionals/example-9.py", execute=True) }}

The last few examples show how even though the order of precedence makes sure that multiplication and division happens before the addition and subtraction, but then immediately once they have been taken care of the evaluation happens from left to right again. As you can also see, the program executes from the top line to the bottom line of code. This creates a logical layout that we will utilise later.

## Order of Precedence
Much like in math, python has an order to when an evaluation gets done. Here is a table:
@@ -69,9 +74,34 @@ To talk about inequalities, we must first need to talk about what an equality is

{{ code_from_file("conditionals/example-8.py", start=1, stop=15, execute=True) }}

Here you can see that the difference between the equality operator and the is statement quite apparent. The equality operator looks to see if the values inside the lists are the same, whereas the is operator looks to see if the labels point to the same object. Understanding this key difference will help you to write good evaluation statements that behave the way you intend them to.
Here you can see that the difference between the equality operator and the is statement quite apparent. The equality operator looks to see if the values inside the lists are the same, whereas the is operator looks to see if the labels point to the same object. Understanding this key difference will help you to write good evaluation statements that behave the way you intend them to. Inequalities cover the other types of comparisons you might want to accomplish, consider this table:

| Symbol | Meaning |
| :----: | :----------------------: |
| == | equal to |
| != | not equal to |
| > | greater than |
| >= | greater than or equal to |
| < | less than |
| <= | less than or equal to |

A comparison always evaluates from left to right in the sense that if you were to create a comparison such as:

```python
print(variable_1 > variable_2)
```
it would compare if variable_1 is greater than variable_2. This can be complicated by multiple evaluations however, consider this example:

```python
print(variable_1 > variable_2 > variable_3)
```
Python handles this type of evaluation by taking the first two and then the last two variables and evaluating them separately. This makes it equivalent to;

```python
print(variable_1 > variable_2 and variable_2 > variable_3)
```

Make sure you consider this behaviour when you write your own conditional statements.

## If
The first control statement you need to learn in Python is the if statement. If statements are used in the construction of conditional execution to create decision making code. The 'if' part will execute if the statement evaluates as true. Consider this example:
@@ -118,3 +148,4 @@ All logic inside a computer boils down to True or False logic. True is either re
When considering your if statements, you need to consider how they will be evaluated. Consider this code fragment:

{{ code_from_file("conditionals/example-5.py", 1, 40, execute=True) }}

@@ -0,0 +1,34 @@
# example-9.py
# Common evaluations and their outcomes
variable_1 = 4
variable_2 = 2
variable_3 = 3
variable_4 = 5

# Simple math functionality
print(variable_1 + variable_2)
print(variable_1 - variable_2)
print(variable_1 * variable_2)
print(variable_1 / variable_2) # / always becomes a floating number
print(variable_1 % variable_2)
print(variable_1 // variable_2)

# Let's do some type checking to prove it
print(type(variable_1 + variable_2))
print(type(variable_1 - variable_2))
print(type(variable_1 * variable_2))
print(type(variable_1 / variable_2))
print(type(variable_1 % variable_2))
print(type(variable_1 // variable_2))

# String concatination
# this uses the + in an interesting way
string_1 = "hey"
string_2 = "you"
print(string_1 + " " + string_2 + "!")
print(type(string_1 + " " + string_2 + "!"))

# interesting evaluations that still happen from left to right
# mostly...
print(variable_1 + variable_2 * variable_3 + variable_4)
print(variable_4 + variable_1 * variable_2 - variable_3)
@@ -1,2 +1,6 @@
# Functions
Functions are self contained objects that are
Functions are self contained objects that are used to create code that we often want to reuse. The most arbitrary use of a function is in this code:

{{ code_from_file("conditionals/example-1.py", 1, 10, execute=True) }}

As you can see, we have created our first function. To create a function you must first use the def keyword followed by the name for the function. The naming conventions for functions are the same as they are for variables. However, it is good practice to have descriptive names for your functions so that you can recognise what they do at a glance.
@@ -0,0 +1,9 @@
# example-1.py
# Introducing functions with functions
def our_first_function(x, y):
total = x + y
print(total)

our_first_function(5, 6)
our_first_function(20, 25)
our_first_function(7, 11)

0 comments on commit 951d7e6

Please sign in to comment.