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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "marimo",
|
| 5 |
+
# ]
|
| 6 |
+
# ///
|
| 7 |
+
|
| 8 |
+
import marimo
|
| 9 |
+
|
| 10 |
+
__generated_with = "0.10.14"
|
| 11 |
+
app = marimo.App()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.cell
|
| 15 |
+
def _():
|
| 16 |
+
import marimo as mo
|
| 17 |
+
return (mo,)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.cell(hide_code=True)
|
| 21 |
+
def _(mo):
|
| 22 |
+
mo.md(
|
| 23 |
+
"""
|
| 24 |
+
# 🔢 Numbers in Python
|
| 25 |
+
|
| 26 |
+
Let's explore how Python handles numbers and mathematical operations!
|
| 27 |
+
|
| 28 |
+
## Number Types
|
| 29 |
+
Python has several types of numbers:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
integer = 42 # whole numbers (int)
|
| 33 |
+
decimal = 3.14 # floating-point numbers (float)
|
| 34 |
+
complex_num = 2 + 3j # complex numbers
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
Below is an example number we'll use to explore operations.
|
| 38 |
+
"""
|
| 39 |
+
)
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@app.cell
|
| 44 |
+
def _():
|
| 45 |
+
number = 42
|
| 46 |
+
return (number,)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.cell(hide_code=True)
|
| 50 |
+
def _(mo):
|
| 51 |
+
mo.md(
|
| 52 |
+
"""
|
| 53 |
+
## Basic Mathematical Operations
|
| 54 |
+
|
| 55 |
+
Python supports all standard mathematical operations.
|
| 56 |
+
|
| 57 |
+
Try changing the value of `number` above and watch how the results change.
|
| 58 |
+
"""
|
| 59 |
+
)
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@app.cell
|
| 64 |
+
def _(number):
|
| 65 |
+
number + 10 # Addition
|
| 66 |
+
return
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@app.cell
|
| 70 |
+
def _(number):
|
| 71 |
+
number - 5 # Subtraction
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@app.cell
|
| 76 |
+
def _(number):
|
| 77 |
+
number * 3 # Multiplication
|
| 78 |
+
return
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@app.cell
|
| 82 |
+
def _(number):
|
| 83 |
+
number / 2 # Division (always returns float)
|
| 84 |
+
return
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@app.cell(hide_code=True)
|
| 88 |
+
def _(mo):
|
| 89 |
+
mo.md("""Python also has special division operators and power operations.""")
|
| 90 |
+
return
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@app.cell
|
| 94 |
+
def _(number):
|
| 95 |
+
number // 5 # Floor division (rounds down)
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@app.cell
|
| 100 |
+
def _(number):
|
| 101 |
+
number % 5 # Modulus (remainder)
|
| 102 |
+
return
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@app.cell
|
| 106 |
+
def _(number):
|
| 107 |
+
number ** 2 # Exponentiation
|
| 108 |
+
return
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@app.cell
|
| 112 |
+
def _(mo):
|
| 113 |
+
mo.md(
|
| 114 |
+
"""
|
| 115 |
+
## Type Conversion
|
| 116 |
+
|
| 117 |
+
You can convert between different number types. Try changing these values!
|
| 118 |
+
"""
|
| 119 |
+
)
|
| 120 |
+
return
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@app.cell
|
| 124 |
+
def _():
|
| 125 |
+
decimal_number = 3.14
|
| 126 |
+
return (decimal_number,)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
@app.cell
|
| 130 |
+
def _(decimal_number):
|
| 131 |
+
int(decimal_number) # Convert to integer (truncates decimal part)
|
| 132 |
+
return
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
@app.cell
|
| 136 |
+
def _(number):
|
| 137 |
+
float(number) # Convert to float
|
| 138 |
+
return
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
@app.cell(hide_code=True)
|
| 142 |
+
def _(mo):
|
| 143 |
+
mo.md(
|
| 144 |
+
"""
|
| 145 |
+
## Built-in Math Functions
|
| 146 |
+
Python provides many useful built-in functions for working with numbers:
|
| 147 |
+
"""
|
| 148 |
+
)
|
| 149 |
+
return
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@app.cell
|
| 153 |
+
def _(number):
|
| 154 |
+
abs(-number) # Absolute value
|
| 155 |
+
return
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
@app.cell
|
| 159 |
+
def _():
|
| 160 |
+
round(3.14159, 2) # Round to 2 decimal places
|
| 161 |
+
return
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
@app.cell
|
| 165 |
+
def _():
|
| 166 |
+
max(1, 5, 3, 7, 2) # Find maximum value
|
| 167 |
+
return
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@app.cell
|
| 171 |
+
def _():
|
| 172 |
+
min(1, 5, 3, 7, 2) # Find minimum value
|
| 173 |
+
return
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@app.cell(hide_code=True)
|
| 177 |
+
def _(mo):
|
| 178 |
+
mo.md(
|
| 179 |
+
"""
|
| 180 |
+
## Advanced Math Operations
|
| 181 |
+
|
| 182 |
+
For more complex mathematical operations, Python's `math` module is your friend:
|
| 183 |
+
|
| 184 |
+
```python
|
| 185 |
+
import math
|
| 186 |
+
|
| 187 |
+
# Square root
|
| 188 |
+
math.sqrt(16) # 4.0
|
| 189 |
+
|
| 190 |
+
# Trigonometry
|
| 191 |
+
math.sin(math.pi/2) # 1.0
|
| 192 |
+
math.cos(0) # 1.0
|
| 193 |
+
|
| 194 |
+
# Constants
|
| 195 |
+
math.pi # 3.141592653589793
|
| 196 |
+
math.e # 2.718281828459045
|
| 197 |
+
|
| 198 |
+
# Logarithms
|
| 199 |
+
math.log10(100) # 2.0
|
| 200 |
+
math.log(math.e) # 1.0
|
| 201 |
+
```
|
| 202 |
+
"""
|
| 203 |
+
)
|
| 204 |
+
return
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
@app.cell(hide_code=True)
|
| 208 |
+
def _(mo):
|
| 209 |
+
callout_text = mo.md("""
|
| 210 |
+
## Master the Numbers!
|
| 211 |
+
|
| 212 |
+
Next Steps:
|
| 213 |
+
|
| 214 |
+
- Practice different mathematical operations
|
| 215 |
+
|
| 216 |
+
- Experiment with type conversions
|
| 217 |
+
|
| 218 |
+
- Try out the math module functions
|
| 219 |
+
|
| 220 |
+
Keep calculating! 🧮✨
|
| 221 |
+
""")
|
| 222 |
+
|
| 223 |
+
mo.callout(callout_text, kind="success")
|
| 224 |
+
return (callout_text,)
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
app.run()
|