Commit f6d3aae4 authored by George Mount's avatar George Mount
Browse files

update files

q
parent 806a2d8c
%% Cell type:markdown id: tags:
# DRILLS
1. Assign the sum of -10 and 2 to `a`.
2. Assign the absolute value of `a` to `b`.
3. Assign `b` minus 1 as `d`.
4. Print the result of `d`. What is the value? What type is this variable?
%% Cell type:code id: tags:
``` python
a = -10 + 2
b = abs(a)
d = b - 1
print(d)
print(type(d))
```
%% Cell type:markdown id: tags:
# DRILL
1. Create a list containing the values `North`, `East`, `South` and `West`.
2. What is the result of the below?
```
len(['Monday','Tuesday','Wednesday','Thursday','Friday',['Saturday','Sunday']])
```
%% Cell type:code id: tags:
``` python
directions = ['North','East','South','West']
print(directions)
print(len(['Monday','Tuesday','Wednesday','Thursday','Friday',['Saturday','Sunday']]))
```
%% Cell type:markdown id: tags:
# DRILL
1. What do you expect to be the result of the following? Run the code and see how you did.
```
my_week = (['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])
my_week.sort()
print(my_week)
```
2. Pass the `clear()` method to `my_week` from above. Re-print `my_week`. What happens?
%% Cell type:code id: tags:
``` python
my_week = (['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])
my_week.sort()
print(my_week)
my_week
```
%% Cell type:code id: tags:
``` python
my_week.clear()
print(my_week)
my_week
```
%% Cell type:markdown id: tags:
## Drill
Practice some more slicing below:
%% Cell type:code id: tags:
``` python
my_list = [7,12,5,10,9]
# Get the first through third elements
print(my_list[0:3])
# Get the third-last to second-last elements
print(my_list[-3:-1])
# Get the second through last elements
print(my_list[1:5])
```
%% Cell type:markdown id: tags:
## DRILL
Practice slicing lists below.
These operations will work the same regardless of whether your list contains floats, strings, or other data types.
%% Cell type:code id: tags:
``` python
this_list = ["Slicing","works","on","lists","of","strings","identically"]
# Get the third to final elements
print(this_list[2:])
# Get everything up to the fourth element
print(this_list[:4])
# Get everything starting with the second-last element
print(this_list[-2:])
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Drill
The `factorial()` function from `math` will take the factorial of a number `X`.
Find the factorial of 10 using this function.
%% Cell type:code id: tags:
``` python
import math
math.factorial(10)
```
%% Cell type:markdown id: tags:
# Drill
What code is used to install `seaborn`?
%% Cell type:code id: tags:
``` python
#!pip install seaborn
```
......
%% Cell type:markdown id: tags:
# Up and running from spreadsheets to Python
## Hello, Jupyter
This is the interface that we will use to execute `.ipynb`, or IPython notebook files.
You may also often see Python files with a `.py` extension. These are *script* files versus *notebook* files.
Notebooks are divided into cells which can be either text or code, among other things.
Go ahead and click into this cell. What happens?
%% Cell type:markdown id: tags:
You can add, reorder, cut and paste cells using the menu icons.
%% Cell type:markdown id: tags:
(From a new cell) you are seeing *raw Markdown* styling in the above cell.
You can close out of it by **running the cell.** `Ctrl + Enter` is the keyboard shortcut.
Markdown allows us to style text using plain-text format.
There's [a lot you can do with Markdown](https://www.markdownguide.org/cheat-sheet). Some basics:
# Big Header 1
## Smaller Header 2
### Even smaller headers
#### Still more
*Using one asterisk renders italics*
**Using two asterisks renders bold**
It's worth studying up on Markdown to write elegant text in your notebooks.
But in this class we'll focus on the *code* block, because that's where executable code goes!
%% Cell type:markdown id: tags:
## Python as a fancy calculator
We can use Python as a highfalutin calculator, just as you might do with Excel.
Enter some basic arithmetic below, then **run the cell** (Do you remember how to do that?).
%% Cell type:code id: tags:
``` python
# This is a code block.
# You can execute code here.
# Python can be used as a fancy calculator.
1+1
```
%% Cell type:markdown id: tags:
Some of these arithmetic operators are the same as in Excel, but others are different:
| Operator | Description |
| -------- | -------------- |
| `+` | Addition |
| `-` | Subtraction |
| `*` | Multiplication |
| `/` | Division |
| `%` | Modulus |
| `**` | Exponent |
| `//` | Floor division |
%% Cell type:markdown id: tags:
Python follows the same PEMDAS order of operations that Excel does:
%% Cell type:code id: tags:
``` python
# Is this -2 or -4?
2 - 3 * 2
```
%% Cell type:markdown id: tags:
## Cell comments
What's the deal with the hashtags and text in the above cell?
Those are cell comments used to give us verbal instructions and reminders about our code. This helps other users -- and ourselves -- remember what we are doing with it.
![Gandalf coding meme](images/gandalf.jpg)
And yes, you can embed images into notebooks 😎.
Write your own comments in the cell below.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Running functions
Python includes many functions for working with data.
Functions take arguments inside parentheses, just like in Excel:
%% Cell type:code id: tags:
``` python
# We can also call functions:
# Let's find the absolute value of -100
abs(-100)
```
%% Cell type:markdown id: tags:
There are some important differences:
%% Cell type:code id: tags:
``` python
# These aren't going to work to find them!
ABS(-100)
```
%% Cell type:code id: tags:
``` python
Abs(-100)
```
%% Cell type:markdown id: tags:
Moral of the story: **Python is case-sensitive** and all-around finicky.
%% Cell type:markdown id: tags:
## Comparison operators
We can also test for whether one value is greater than another, much like you would do in Excel:
%% Cell type:code id: tags:
``` python
# Is 3 > 4?
3 > 4
```
%% Cell type:markdown id: tags:
Like in Excel, Python will return either a `True` or `False`.
Much of these conditional operators will look familiar to you:
| Operator | Meaning |
| -------- | ------------------------ |
| `>` | Greater than |
| `<` | Less than |
| `>=` | Greater than or equal to |
| `<=` | Less than or equal to |
| `!=` | Not equal to |
| **`==`** | **Equal to** |
%% Cell type:markdown id: tags:
Did you catch that last one?
You do not check for whether two values are equal to each other using `=`, but instead using `==`. Why?
Because in Python, we assign data to *variables*. This is a game-changer!
%% Cell type:markdown id: tags:
## Assigning variables
Calling functions like `abs(-100)` can be useful, but where things get *really* interesting in Python is by assigning results of operations to variables.
Let's go ahead and pass the absolute value of -100 to a variable, `my_first_variable`.
%% Cell type:code id: tags:
``` python
my_first_variable = abs(-100)
```
%% Cell type:markdown id: tags:
The result of `abs(-100)` has been stored in a *variable*, which will make it much easier for us to refer to and use it.
The result of `abs(-100)` has been stored in a *variable* or *object*, which will make it much easier for us to refer to and use it.
### Printing variables
To see the result of that variable, we can *print* it using the `print()` function:
%% Cell type:code id: tags:
``` python
print(my_first_variable)
```
%% Cell type:markdown id: tags:
It's not necessary to include the function in Jupyter; simply run the object's name to print:
%% Cell type:code id: tags:
``` python
my_first_variable
```
%% Cell type:markdown id: tags:
What do you think the result of the below will be?
%% Cell type:code id: tags:
``` python
print(MY_FIRST_VARIABLE)
MY_FIRST_VARIABLE
```
%% Cell type:markdown id: tags:
## Python variable naming conventions
> There are only two hard things in Computer Science: cache invalidation and naming things. --Phil Karlton
In theory, you can name your variables almost anything so long as they fit these rules:
- They must start with a letter or underscore.
- The rest of your variable can only contain letters, numbers or underscores.
But `sales` may be a better name for sales data than `scooby_doo`.
### DRILL
Based on these rules, which of the following is an invalid variable name?
A. `My_string_`
B. `string_1`
C. `razzle.dazzle`
D. `_`
%% Cell type:code id: tags:
``` python
# Try assigning and printing these variables if you're not sure!
_ = 1
print(_)
razzle.dazzle = abs(-19)
print(razzle.dazzle)
```
%% Cell type:markdown id: tags:
## Variable types
You can think about a variable as a box that we are putting a piece of information into.
Variables can be of different types, like different categories and dimensions of boxes.
![variables shoebox](images/variables-shoebox.png)
%% Cell type:markdown id: tags:
Some of these distinctions between data types are found in Excel, although you will find Python to be much more exacting about them!
Some of these distinctions between data types are found in Excel, although you will find Python to be much more exacting about them:
%% Cell type:markdown id: tags:
![data types in Excel](images/data-types-excel.PNG)
%% Cell type:markdown id: tags:
You can find a variable's type with the `type()` function.
%% Cell type:code id: tags:
``` python
# Assigning different variable types