---
tags: python-course
title: lesson-03
---
# Values and Data Types
[](https://hackmd.io/vc0uFruNTlOGbVWuyhxfCA)
> #### Editing Notes
> 1. Resize James and Nina at the end
> 2. Voice over for Mendel is too quiet
> 3. Code introducing True/False is behind James
:::info
:bulb: Introduce the basic concepts of values and data types: strings, numbers, and variables.
Personas: Teacher is Nina, student is James.
:::
:::success
:movie_camera: VIB background fading to course title slide. James and Ninas smiling faces appear.
:::
:::warning
:notes: Upbeat intro music
:::
**James**: What's a string?
**Nina**: A string is the data type that describes text. The history is a little convoluted but sometime in the 60's engineers started to use the word string to refer to sequences of letters, numbers, and other symbols "strung" together, as a "string" of characters. And since then the name stuck.
**James**: Strings seem really versatile! Can I store any data in a string? Can I store the number of steps I walked yesterday in a string?
**Nina** (sceptical): Yeah... you technically could (animation of "948"). But you're playing the game in hard mode. If you want to sum your step count for every day of the week, for example, things aren't going to work out easily for you.
:::success
:movie_camera: Animation showing...
```python
>>> "83" + "1489" + "794" + "344" + "912" + "1031" + "948"
"8314897943449121031948"
```
:::
**Nina**: That's probably not what you meant! The numbers were just slapped together, not summed. When you use the `+` operator with string operands, Python treats it as the _concatenation operator_, not addition.
**James**: So how do I do addition?
:::success
:movie_camera: Animation showing addition...
```python
>>> 83 + 1489 + 794 + 344 + 912 + 1031 + 948
5601
```
:::
**Nina**: Now you're representing your data as integers, not strings. Integers are a more appropriate data type for this operation. And so this result is probably more like what you intended: the numbers are properly summed.
**James**: What's an integer?
**Nina**: An integer is another data type like strings but for whole number numerical data. Python gives you a bunch of useful types out of the box.
**James**: Ok I get it, the `+` operator behaves differently depending on the data types of its 2 operands. If the operands are text `+` will concatenate them. If the operands are numbers then it will compute the sum of those 2 numbers.
**Nina**: (looking into the the camera) And what if 1 operand is a string and the other is a number? Try yourself to find out...
:::success
:movie_camera: Type `"Hello" + 5` into a new cell
:::
**Nina**: (After a pause) As you can see, there are no sensible ways to combine these data types so Python tells us there was an error. If you read carefully it is a `TypeError`. This message helps us understand that the problem with our program has something to do with the way we are using data types.
**James**: Ok, so I have to make sure I use the correct data types for the operator I want to use. We've talked about arithmetic and concatenation operators. What if I want to compare values?
**Nina**: You're in luck! Python provides several comparison operators that we will use throughout this course. For example, if you want to compare 2 strings you can use 2 equals signs together.
:::success
:movie_camera: Type `"ATTC" == "ATTC"` into a new cell
:::
**James**: It says, `True` what does that mean?
**Nina** (over animation): You can think of the equality operator (`==`) as asking a question: "Are my 2 operands the same"? If it says, true it means "Yes they are the same" and if it says False it means "No they are not the same". `True` and `False` are values of boolean or logical data types.
:::success
:movie_camera: Morph previous example into`"ATTC" == "BCCS"` into a new cell -> False
:::
**Nina**: (To the camera) You should experiment with the comparison operators now. Can you ask Python if 5 is greater than 3? Does it make sense to ask if "Apple" is less than "Orange"?
> [name=James] Say something like, this is a letter-by-letter comparison.
:::warning
:notes: Waiting music
:::
**James**: How can "Apple" be less than "Orange"? "Orange" is longer.
**Nina** (over animation): It turns out that the length of the strings isn't used for comparison. Instead, each letter and symbol is given a numerical value. Capital "A" is 65, and capital "O" is 79. That's all that is checked to say that "Apple" is less than "Orange". "A" < "O". 65 < 79. (pause) If they were the same first letters then the comparison would take the next letter and so on.
**James**: That seems complicated!
**Nina**: Is really is and we're only just scratching the surface! Thankfully you rarely need to worry about the details, you can think of this ordering as "A" comes before "O". That's a lexical ordering.
**James**: Ok I see. I would like to think about a more realistic situation that I can apply all of this too already.
**Nina**: Alright. That's a great idea. What's the problem?
**James**: I want to model Mendelion genetics. One thing I can do is predict the number of expected offspring displaying the dominant phenotype. In my model, there are 6 possible genotype pairings. I use a capital "A" to represent a dominant allel, and a lower-case "a" to represent a recessive allel.
:::success
:movie_camera: Display a slide with the following info on it (animate for each genotype pairing)
:::
1. `AA-AA` has a 100% chance of passing on the dominant phenotype
2. `AA-Aa` has a 100% chance of passing on the dominant phenotype
3. `AA-aa` has a 100% chance of passing on the dominant phenotype
4. `Aa-Aa` has a 75% chance of passing on the dominant phenotype
5. `Aa-aa` has a 50% chance of passing on the dominant phenotype
6. `aa-aa` has a 0% chance of passing on the dominant phenotype
**James**: Each individual is represented by allelic pairs. For example, capital "AA" is an individual with 2 copies of the dominant gene. There are 100 couples in the population, and I assume each couple produces 2 offspring. There are 6 possible pairings as shown behind us. The population contains this many pairings, now.
:::success
:movie_camera: Display a slide with the following info on it (animate for each genotype pairing)
* 13 `AA_AA` genotype couples
* 4 `AA_Aa` genotype couples
* 42 `AA_aa` genotype couples
* 14 `Aa_Aa` genotype couples
* 26 `Aa_aa` genotype couples
* 1 `aa_aa` genotype couple
:::
**James**: And I can compute the expected number of offspring by multiplying the probability of producing a dominant phenotype by the number of couples in the population.
:::success
:movie_camera: Animate this computation in the background.
```python
(1.0 * 13 + 1.0 * 4 + 1.0 * 42 + 0.75 * 14 + 0.5 * 26 + 0.0 * 1) * 2
```
:::
**James**: This produces a correct result. But if I come back to this tomorrow I might not remember what it is supposed to do or mean. How can I make my program more clear?
**Nina**: There are a few things you can do. Firstly, this might be a good place to write down your intentions in a natural language.
**James**: Ok...
:::success
:movie_camera: Display this typing into a notebook cell:
```python
Compute the expected number of offspring with the dominant phenotype
```
Show that a `SyntaxError` happens when executing the cell.
:::
**Nina**: Unfortunately Python doesn't understand natural language so we have to tell Python to effectively ignore this text. You can do that by placing a `#` sign at the beginning of your text.
:::success
:movie_camera: Display this typing into a notebook cell:
```python
# Compute the expected number of offspring with the dominant phenotype
```
Show that the cell executes again.
:::
**Nina**: Now, your description is readable by you, but ignored by Python. Programmers call this a "comment". (pause) You can make your intentions even more clear by giving the numeric values meaningful names like this...
:::success
:movie_camera: Display this in a notebook cell:
```python
# Compute the expected number of offspring with the dominant phenotype
AA_AA = 13
AA_Aa = 4
AA_aa = 42
Aa_Aa = 14
Aa_aa = 26
aa_aa = 1
offspring = 2
(1.0 * AA_AA + 1.0 * AA_Aa + 1.0 * AA_aa + 0.75 * Aa_Aa + 0.5 * Aa_aa + 0.0 * aa_aa) * offspring
```
Show that the cell still executes correctly.
:::
**James**: That is definitely more clear. And I can use names the same way as I use values. When I read my equaltion, Python can simply replace the name with whatever value I gave it.
**Nina**: Exactly! These are called _variables_. You can think of variables as a box, the equals sign (`=`), or _assignment operator_, places a value into the box. You can use variables anywhere you use values, and treat them the same way. The advantage of variables is that you can describe the value they contain which makes the program easier to understand for you and for others.
**Nina**: What have you learned in this lesson?
**James**: In this lesson I learned about values, their data types, and some of the operators you can use. You also talked about making computations clearer with comments and variables.
**Nina**: These are the basic "building blocks" of our programs. We're still talking about the fundamental concepts you will need before you can start building more interesting programs. Once you have a feel for these ideas, writing useful programs will become much easier.
:::success
:movie_camera: Fade to VIB logo slide.
:::
:::warning
:notes: Upbeat outro music
:::