Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions episodes/conditionals.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,52 @@ for checkout in checkouts:

Notice that our `else` statement led to a false output that says 10 is under the limit. We can address this by adding a different kind of `else` statement.

::::::::::::::::::::::::::::::::::::::: challenge

## Age conditionals

Write a Python program that checks the age of a user to determine if they will receive a youth or adult library card. The program should:

1. Store `age` in a variable.
2. Use an `if` statement to check if the age is 16 or older. If true, print "You are eligible for an adult library card."
3. Use an `else` statement to print "You are eligible for a youth library card" if the age is less than 16.

If you finish early, try this challenge:

- In a new cell, adapt your program to loop through a list of age values, testing each age with the same output as above.

::::::::::::::: solution

## Solution

For parts 1 to 3:

```python
age = 25

if age >= 16:
print('You are eligible for an adult library card.')
else:
print('You are eligible for a youth library card.')
```

For the challenge:
```python
ages = [10, 16, 30, 65]

for age in ages:
if age >= 16:
print('You are eligible for an adult library card.')
else:
print('You are eligible for a youth library card.')
```


:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::


## Use `elif` to specify additional tests.

You can use `elif` (short for "else if") to provide several alternative choices, each with its own test. An `elif` statement should always be associated with an `if` statement, and must come before the `else` statement (which is the catch all).
Expand Down Expand Up @@ -153,52 +199,6 @@ for user in users:
*Warning*: 120 is over the grad limit.
```

::::::::::::::::::::::::::::::::::::::: challenge

## Age conditionals

Write a Python program that checks the age of a user to determine if they will receive a youth or adult library card. The program should:

1. Store `age` in a variable.
2. Use an `if` statement to check if the age is 16 or older. If true, print "You are eligible for an adult library card."
3. Use an `else` statement to print "You are eligible for a youth library card" if the age is less than 16.

If you finish early, try this challenge:

- In a new cell, adapt your program to loop through a list of age values, testing each age with the same output as above.

::::::::::::::: solution

## Solution

For parts 1 to 3:

```python
age = 25

if age >= 16:
print('You are eligible for an adult library card.')
else:
print('You are eligible for a youth library card.')
```

For the challenge:
```python
ages = [10, 16, 30, 65]

for age in ages:
if age >= 16:
print('You are eligible for an adult library card.')
else:
print('You are eligible for a youth library card.')
```


:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::


::::::::::::::::::::::::::::::::::::::: challenge

## Conditional logic: Fill in the blanks
Expand Down
99 changes: 50 additions & 49 deletions episodes/data-visualisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,55 +138,6 @@ albany['circulation'].plot(kind='hist', bins=20,

![](fig/albany-circ-hist-9.png){alt="histogram of the Albany branch circulation."}

## Use Plotly for interactive plots

Let’s switch back to the full DataFrame in `df_long` and use another
plotting package in Python called Plotly.

```python
import plotly.express as px
```

Now we can visualize how circulation counts have changed over time for selected branches. This can be especially useful for identifying trends, seasonality, or data anomalies. We willfirst create a subset of our data to look at branches starting with the letter 'A'. Feel free to select different branches. After subsetting, we will sort our new DataFrame by date and then plot our data by date and circulation count.

``` python
# Creating a line plot for a few selected branches to avoid clutter
selected_branches = df_long[df_long['branch'].isin(['Altgeld',
'Archer Heights',
'Austin',
'Austin-Irving',
'Avalon'])]
selected_branches = selected_branches.sort_values(by='date')
```

``` python
fig = px.line(selected_branches, x=selected_branches.index, y='circulation', color='branch', title='Circulation Over Time for Selected Branches')
fig.show()
```

Here is a view of the [interactive output of the Plotly line chart](learners/line_plot_int.html).


One advantage that Plotly provides over Matplotlib is that it has some interactive features out of the box. Hover your cursor over the lines in the output to find out more granular data about specific branches over time.


### Bar plots with Plotly

Let’s use a barplot to compare the distribution of circulation counts
among branches. We first need to group our data by branch and sum up the circulation counts. Then we can use the bar plot to show the
distribution of total circulation over branches.

``` python
# Aggregate circulation by branch
total_circulation_by_branch = df_long.groupby('branch')['circulation'].sum().reset_index()

# Create a bar plot
fig = px.bar(total_circulation_by_branch, x='branch', y='circulation', title='Total Circulation by Branch')
fig.show()
```

Here is a view of the [interactive output of the Plotly bar chart](learners/bar_plot_int.html).

::::::::::::::::::::::::::::::::::::::: challenge

## Plotting with Pandas
Expand Down Expand Up @@ -248,6 +199,56 @@ uptown['circulation'].plot(title='Uptown Circulation',

::::::::::::::::::::::::::::::::::::::::::::::::::


## Use Plotly for interactive plots

Let’s switch back to the full DataFrame in `df_long` and use another
plotting package in Python called Plotly.

```python
import plotly.express as px
```

Now we can visualize how circulation counts have changed over time for selected branches. This can be especially useful for identifying trends, seasonality, or data anomalies. We willfirst create a subset of our data to look at branches starting with the letter 'A'. Feel free to select different branches. After subsetting, we will sort our new DataFrame by date and then plot our data by date and circulation count.

``` python
# Creating a line plot for a few selected branches to avoid clutter
selected_branches = df_long[df_long['branch'].isin(['Altgeld',
'Archer Heights',
'Austin',
'Austin-Irving',
'Avalon'])]
selected_branches = selected_branches.sort_values(by='date')
```

``` python
fig = px.line(selected_branches, x=selected_branches.index, y='circulation', color='branch', title='Circulation Over Time for Selected Branches')
fig.show()
```

Here is a view of the [interactive output of the Plotly line chart](learners/line_plot_int.html).


One advantage that Plotly provides over Matplotlib is that it has some interactive features out of the box. Hover your cursor over the lines in the output to find out more granular data about specific branches over time.


### Bar plots with Plotly

Let’s use a barplot to compare the distribution of circulation counts
among branches. We first need to group our data by branch and sum up the circulation counts. Then we can use the bar plot to show the
distribution of total circulation over branches.

``` python
# Aggregate circulation by branch
total_circulation_by_branch = df_long.groupby('branch')['circulation'].sum().reset_index()

# Create a bar plot
fig = px.bar(total_circulation_by_branch, x='branch', y='circulation', title='Total Circulation by Branch')
fig.show()
```

Here is a view of the [interactive output of the Plotly bar chart](learners/bar_plot_int.html).

::::::::::::::::::::::::::::::::::::::: challenge

## Plot the top five branches
Expand Down
63 changes: 32 additions & 31 deletions episodes/for-loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ for number in range(0,3):
2
```

::::::::::::::::::::::::::::::::::::::: challenge

## Use range() in a loop

Print out the numbers 10, 11, 12, 13, 14, 15, using range() in a `for` loop.

::::::::::::::: solution

## Solution

```python

for num in range(10, 16):
print(num)

```

```output
10
11
12
13
14
15
```


:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::


## Accumulators

A common loop pattern is to initialize an *accumulator* variable to zero, an empty string, or an empty list before the loop begins. Then the loop updates the accumulator variable with values from a collection.
Expand Down Expand Up @@ -249,37 +281,6 @@ for veg in vegetables:
::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::: challenge

## Use range() in a loop

Print out the numbers 10, 11, 12, 13, 14, 15, using range() in a `for` loop.

::::::::::::::: solution

## Solution

```python

for num in range(10, 16):
print(num)

```

```output
10
11
12
13
14
15
```


:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::: challenge

## Use a string index in a loop

How would you loop through a list with the values 'red', 'green', and 'blue' to create the acronym `rgb`, pulling from the first letters in each string? Print the acronym when the loop is finished.
Expand Down
4 changes: 2 additions & 2 deletions episodes/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exercises: 5

- How can I identify and use key features of JupyterLab to create and manage a Python notebook?
- How do I run Python code in JupyterLab, and how can I see and interpret the results?
-

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a stray dash here


::::::::::::::::::::::::::::::::::::::::::::::::::

## Why Python?
Expand Down Expand Up @@ -151,7 +151,7 @@ If you move your cursor back to the first cell, just after the `7 * 3` code, and

```python
7 * 3
2 +1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor cleanup

2 + 1
```

While Python runs both calculations Juypter will only display the output from the last line of code in a specific cell, unless you tell it to do otherwise.
Expand Down
Loading
Loading