Skip to content

Commit

Permalink
update datatypes slides
Browse files Browse the repository at this point in the history
  • Loading branch information
d-chambers committed Feb 21, 2024
1 parent 6a27052 commit 1e5db70
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 112 deletions.
2 changes: 1 addition & 1 deletion _extensions/mcanouil/iconify/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Mickaël Canouil
Copyright (c) 2024 Mickaël Canouil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 0 additions & 2 deletions python_intro/modules/datatypes/exercises.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Exercise

Complete the following exercise by clicking [here](https://opensourcecourse.github.io/hub/lab/?path=data_types.ipynb)


Binary file removed python_intro/modules/datatypes/img.png
Binary file not shown.
17 changes: 11 additions & 6 deletions python_intro/modules/datatypes/overview.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ a very good place to start

This module covers some common built-in datatypes in python.

Understanding these datatypes is not only important for using python effectively, but also for understanding more advanced features of python's type system and implementation.
Understanding these datatypes is not only important for using python effectively, but also for understanding more advanced concepts of python's type system.

# Objectives

In this module we will learn about:
After completing this module you will be able to:

1. Numeric types: int, float, complex
2. Boolean
3. Strings
4. Containers: list and dict
1. Create python variables

2. Identify usages of basic python types, including:
* Numeric types: int, float, complex
* Boolean
* Strings
* Container types: list and dict

3. Debug short code snippets using these concepts


# Reading
Expand Down
151 changes: 48 additions & 103 deletions python_intro/modules/datatypes/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Variables reference a value
<br>

:::{.fragment}
```{.python filename="functions" code-line-numbers="1|2|3|4|5"}
```{.python filename="variables" code-line-numbers="1|2|3|4|5"}
bar = 1
foo = "two"
que = 3.0
Expand Down Expand Up @@ -56,8 +56,8 @@ Why doesn't this work?
:::{.fragment}

```{.python filename="variables"}
int_2 = int_1
int_1 = 2
var_1 = var_2
var_2 = 2
```

:::
Expand Down Expand Up @@ -94,6 +94,8 @@ print(foo)
- Python supports `ints`, `floats`, `complex`
- Operators return appropriate types

<br>

```{.python filename="numbers" code-line-numbers="1|2|3|4|5"}
my_sum = 42 + 2.7182818 # 44.7182818
my_product = 42 * 2 # 84
Expand All @@ -106,6 +108,8 @@ my_expontential = 10**6 # 1_000_000

How do you think complex numbers behave when added by an `int`?

<br>

:::{.fragment}
```{.python filename="numbers"}
my_complex = complex(1, 1)
Expand All @@ -117,18 +121,32 @@ my_complex + 1
# Boolean

- Booleans can be either `True` or `False`
- Everything in python is "truthy" except:
- 0, empty things, `None`

:::{.fragment}
```{.python filename="variables" code-line-numbers="1-2|4-5|7"}
```{.python filename="boolean" code-line-numbers="1-2|4-5"}
so_true = True
so_false = False
my_var1 = so_true and so_false
my_var2 = so_true or so_false
```
:::



another_bool = bool(0)
# Boolean

- Everything in python is "truthy" except:
- 0, empty things, `None`, `False`, ...

<br>

:::{.fragment}
```{.python filename="boolean" code-line-numbers="1|2|3|4"}
bool(10)
bool(1.0)
bool("")
bool(0)
```
:::

Expand All @@ -138,7 +156,7 @@ another_bool = bool(0)
Strings represent characters

:::{.fragment}
```{.python filename="variables" code-line-numbers="1|3|5"}
```{.python filename="strings" code-line-numbers="1|3|5|7-9"}
my_str = "Bloom's Taxonomy"
str_concat = my_str + " is great!"
Expand All @@ -153,10 +171,12 @@ upper_case = my_str.upper()

# Strings: Knowledge Check

What do these functions do?
What do these methods do?

<br>

:::{.fragment}
```{.python code-line-numbers="1|3|4"}
```{.python filename="strings" code-line-numbers="1|3|4"}
my_str = "a farewell to arms"
print(my_str.title())
Expand All @@ -171,7 +191,7 @@ print(my_str.startswith("a"))


:::{.fragment}
```{.python filename="variables" code-line-numbers="1|3|4|5|6|7-8|9-10"}
```{.python filename="lists" code-line-numbers="1|3|4|5|6|7-8|9-10"}
my_list = [1, 2, True, 0.0]
len(my_list) # get the length of the list
Expand All @@ -181,115 +201,40 @@ my_list[1:2] # gets middle values
# append value to end
my_list.append("Grapes of Wrath")
# join two lists together
concated = [1,2,3] + [4, 5, 6]
concated = [1, 2, 3] + [4, 5, 6]
```
:::


# Lists: Knowledge Check

:::{.fragment}
```{.python filename="lists" code-line-numbers="1|3|4|5|6"}
my_list = [1, 2, 3]
len(my_list)
my_list[1]
my_list.append(8)
my_list[-2]
```
:::


# Dictionaries

:::: {.columns}

::: {.column width="50%"}

::: {.incremental}
<br>
# Lists

- A Key -> Value container
- Keys must be **unique** and **hashable**
- Values have no restrictions
:::
- Lists support iteration.

:::

::: {.column width="50%"}
:::{.fragment}
```{mermaid}
%%| fig-width: 4.2
flowchart LR
A[Key 1] --> B(Value 1)
C[Key 2] --> D(Value 2)
E[Key 3] --> D
```
:::

:::

:::


# Dictionaries

- Accessing keys

```{.python code-line-numbers="1-5|7|8|9"}
map = {
1: 2,
4: 3.1415,
"time": "money",
}
map[1] # 2
map[4] # 3.1425
time = map["time"] # money
```

# Dictionaries

- Adding/removing keys

```{.python code-line-numbers="1-3|5|6"}
map = {
1: 2,
}
```{.python filename="lists" code-line-numbers="1|3-4"}
some_phrase = ["Heghlu'meH", "QaQ", "jajvam"]
map['time'] = "money"
map.pop(1)
```


# Dictionaries: Knowledge Check

<br>

```{.python code-line-numbers="1-4|6|7|8"}
map = {
1: 2,
"bob": "sue",
}
map[1]
map["bob"]
map["sue"]
for word in some_phrase:
print(word)
```
:::


# Dictionaries: Intuition Check
# Lists: Knowledge Check

<br>

```{.python}
map = {
1: 2,
"bob": "sue",
}
:::{.fragment}
```{.python filename="lists" code-line-numbers="1|3|4|5|6"}
my_list = [1, 2, 3]
len(map)
len(my_list)
my_list[1]
my_list.append(8)
my_list[-2]
```
:::


# Exercise
Expand Down

0 comments on commit 1e5db70

Please sign in to comment.