Skip to content

Commit 7c0dd0a

Browse files
committed
Added 3 new draft curriculum items
- variables - data types - numbers and math
1 parent dcdd6c4 commit 7c0dd0a

3 files changed

Lines changed: 262 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: variables
3+
title: Variables
4+
sidebar_label: 3. Variables
5+
sidebar_position: 3
6+
---
7+
8+
# Variables: Labeling the Ledger
9+
10+
In the previous lesson, we printed text directly. But what if you want to use the same piece of information ten times? Or what if that information changes?
11+
12+
In Python, we use **Variables** to store data. Think of a variable as a **labeled box**. The box holds a value, and the label (the name) allows you to find that value whenever you need it.
13+
14+
15+
16+
### The Assignment Operator
17+
To put something in a box, we use the `=` symbol. In Python, this is called the **Assignment Operator**.
18+
19+
**Important:** This is NOT the same as "equals" in math. It means "Take the value on the right and store it into the name on the left."
20+
21+
```python interactive
22+
# Assigning a value to a variable
23+
ledger_entry = "Bought 10 apples"
24+
25+
# Using the variable
26+
print(ledger_entry)
27+
```
28+
29+
---
30+
31+
### Reassignment: Updating the Record
32+
The beauty of a variable is that it is *variable* - it can change. If you assign a new value to an existing name, Python throws away the old value and keeps the new one.
33+
34+
```python interactive
35+
current_status = "Level 1: Novice"
36+
print(current_status)
37+
38+
# Now we reassign it
39+
current_status = "Level 2: Apprentice"
40+
print(current_status)
41+
```
42+
43+
---
44+
45+
### Naming Your Variables (The Rules)
46+
You can't just name a variable anything. Python has a few "laws of the land":
47+
1. **No Spaces:** Use underscores instead (`my_variable`).
48+
2. **Start with a Letter:** You cannot start a name with a number (e.g., `1_entry` is illegal).
49+
3. **Be Descriptive:** `x` is a bad name. `user_balance` is a great name.
50+
4. **Case Sensitive:** `Ledger` and `ledger` are two different boxes.
51+
52+
In Python, the community standard is called **snake_case** (all lowercase with underscores).
53+
54+
---
55+
56+
### 🏆 The Ledger Challenge: The Character Sheet
57+
Let's use variables to build a mini "Character Sheet" for your coding journey.
58+
59+
**Task:** 1. Create a variable called `coder_name` and set it to your name.
60+
2. Create a variable called `coding_level` and set it to `1`.
61+
3. Create a variable called `favorite_language` and set it to `"Python"`.
62+
4. Use a single `print()` statement (or multiple!) to show your stats.
63+
64+
**Write your code below:**
65+
66+
```python interactive
67+
# 1. Define your variables here
68+
69+
70+
# 2. Print them out!
71+
72+
```
73+
74+
---
75+
76+
## 📚 Deep Dive
77+
* [Python.org: Informal Introduction to Variables](https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator)
78+
* [Real Python: Variables in Python](https://realpython.com/python-variables/)
79+
80+
---
81+
82+
## Next Steps
83+
Now that we have boxes to store information, we need to talk about the **different types of stuff** we can put inside them.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
id: data-types
3+
title: Data Types
4+
sidebar_label: 4. Data Types
5+
sidebar_position: 4
6+
---
7+
8+
# Data Types: Sorting the Ledger
9+
10+
Not all data is created equal. You can't multiply a "Name" by a "Phone Number," and you can't capitalize a "Price." To keep your code from crashing, you must understand the four fundamental **Data Types** in Python.
11+
12+
13+
14+
### 1. Strings (`str`)
15+
As we saw in Lesson 2, these are sequences of characters wrapped in quotes.
16+
* **Example:** `"Hello"`, `'12345'`, `"Python_Ledger_v1"`
17+
* **Use for:** Names, addresses, or any text.
18+
19+
### 2. Integers (`int`)
20+
These are whole numbers with no decimal point.
21+
* **Example:** `10`, `-5`, `1000000`
22+
* **Use for:** Counting items, ages, or quantities.
23+
24+
### 3. Floats (`float`)
25+
Short for "floating-point numbers," these are numbers that contain a decimal point.
26+
* **Example:** `10.5`, `-0.01`, `3.14`
27+
* **Use for:** Prices, percentages, or precise measurements.
28+
29+
### 4. Booleans (`bool`)
30+
These represent logical values: either **True** or **False**. Note the capital letters!
31+
* **Example:** `True`, `False`
32+
* **Use for:** Checking if a user is logged in, if a task is finished, or if a price is too high.
33+
34+
---
35+
36+
### The `type()` Inspector
37+
If you ever get confused about what is inside a variable, Python provides a built-in magnifying glass: the `type()` function.
38+
39+
```python interactive
40+
# Try running this to see the types!
41+
name = "Alice"
42+
age = 25
43+
price = 19.99
44+
is_coding = True
45+
46+
print(type(name))
47+
print(type(age))
48+
print(type(price))
49+
print(type(is_coding))
50+
```
51+
52+
### Dynamic Typing: The Python Superpower
53+
In some languages, you have to tell the computer exactly what type a variable is. In Python, the computer figures it out automatically based on the value you provide. This is called **Dynamic Typing**.
54+
55+
---
56+
57+
### 🏆 The Ledger Challenge: Inventory Audit
58+
You are auditing your supply of "Coding Fuel."
59+
60+
**Task:**
61+
1. Create a variable `item_name` and assign it a **String** (e.g., "Coffee").
62+
2. Create a variable `quantity` and assign it an **Integer** (e.g., 5).
63+
3. Create a variable `price_per_unit` and assign it a **Float** (e.g., 3.50).
64+
4. Create a variable `in_stock` and assign it a **Boolean** (`True` or `False`).
65+
5. Print all four variables.
66+
67+
**Write your code below:**
68+
69+
```python interactive
70+
# 1. Define your four data types here
71+
72+
73+
# 2. Print them out to check your work
74+
75+
```
76+
77+
---
78+
79+
## 📚 Deep Dive
80+
* [Python Docs: Built-in Types](https://docs.python.org/3/library/stdtypes.html)
81+
* [Real Python: Basic Data Types in Python](https://realpython.com/python-data-types/)
82+
83+
---
84+
85+
## Next Steps
86+
Now that we know the types, it's time to put them to work. In the next lesson, we’ll learn **Basic Math**—how to add, subtract, and manipulate these numbers in our Ledger.
87+
88+
[Go to next lesson: Numbers and Math](./05-numbers-and-math.md)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
id: numbers-and-math
3+
title: Numbers and Math
4+
sidebar_label: 5. Numbers and Math
5+
sidebar_position: 5
6+
---
7+
8+
# Numbers and Math: Balancing the Ledger
9+
10+
A ledger is useless if you can't add up the totals. In Python, we use **Arithmetic Operators** to perform calculations. Since you already know about Integers and Floats, let's see how they interact.
11+
12+
13+
14+
### The Basic Operators
15+
Most of these look exactly like the math you learned in school:
16+
* `+` **Addition:** `5 + 2` is `7`
17+
* `-` **Subtraction:** `5 - 2` is `3`
18+
* `*` **Multiplication:** `5 * 2` is `10`
19+
* `/` **Division:** `5 / 2` is `2.5` (Note: Division *always* results in a **Float**)
20+
21+
### The "Special" Operators
22+
Python has a few tools that are specific to programming:
23+
24+
1. **Floor Division (`//`):** Divides and chops off the decimal. `5 // 2` is `2`.
25+
2. **Exponent (`**`):** Raises a number to a power. `5 ** 2` is `25`.
26+
3. **Modulo (`%`):** Returns the **remainder** of a division. `5 % 2` is `1` (because 2 goes into 5 twice, with 1 left over).
27+
28+
```python interactive
29+
# Try these out!
30+
print(10 / 3) # Normal Division
31+
print(10 // 3) # Floor Division
32+
print(10 % 3) # Modulo (The Remainder)
33+
print(2 ** 3) # 2 to the power of 3
34+
```
35+
36+
---
37+
38+
### Order of Operations
39+
Python follows the standard mathematical order of operations, often remembered by the acronym **PEMDAS** (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).
40+
41+
42+
43+
When in doubt, use **parentheses** to make your intentions clear to the computer:
44+
45+
```python interactive
46+
result_one = 5 + 2 * 10 # Becomes 25
47+
result_two = (5 + 2) * 10 # Becomes 70
48+
49+
print(result_one)
50+
print(result_two)
51+
```
52+
53+
---
54+
55+
### 🏆 The Ledger Challenge: The Interest Calculator
56+
You are calculating the final balance of a ledger entry after a year of 5% interest.
57+
58+
**Task:**
59+
1. Create a variable `principal` and set it to `1000`.
60+
2. Create a variable `interest_rate` and set it to `0.05`.
61+
3. Create a variable `total` that calculates the final amount using the formula:
62+
$$Total = principal + (principal \times interest\_rate)$$
63+
4. Print the `total`.
64+
65+
**Bonus:** Use the `int()` function to print only the whole number part of the total!
66+
67+
**Write your code below:**
68+
69+
```python interactive
70+
# 1. Define your variables
71+
72+
73+
# 2. Calculate the total
74+
75+
76+
# 3. Print the result
77+
78+
```
79+
80+
---
81+
82+
## 📚 Deep Dive
83+
* [Python Docs: Numeric Types](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)
84+
* [Programiz: Python Operators](https://www.programiz.com/python-programming/operators)
85+
86+
---
87+
88+
## Next Steps
89+
Calculations are great, but a ledger needs to make decisions. In the next lesson, we’ll learn how to compare values using **Booleans and Comparison Operators**.
90+
91+
[Go to next lesson: Comparisons](./06-comparisons.md)

0 commit comments

Comments
 (0)