(100, 0.05, 'portfolio')
Python Basics
Variables, values, types, and arithmetic for quantitative Python.
Learning Objectives
- Assign values to variables.
- Recognize common Python types.
- Use arithmetic operators.
- Inspect values with helper functions.
Motivation
Python is useful for quantitative mathematics because it lets us compute, visualize, and experiment. The first step is understanding how Python stores values.
Variables and Values
A variable is a name that points to a value.
Python keeps track of the type of each value.
(int, float, str)
Arithmetic
105.0
Common arithmetic operators:
| Operator | Meaning |
|---|---|
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
** |
exponentiation |
Python Connection
ValueInfo(value=105.0, type_name='float', representation='105.0')
Common Mistakes
Confusing Assignment With Equality
In Python, x = 3 assigns the value 3 to x. It does not mean “prove that x equals 3.”
Forgetting That Strings Are Text
"100" is text. 100 is a number. They behave differently.
Practice
- Create a variable named
principalwith value250. - Create a variable named
ratewith value0.08. - Compute
principal * (1 + rate). - Use
describe_value(...)on the result.
Solutions
TipSolution 1
Answer: principal = 250.
TipSolution 2
Answer: rate = 0.08.
TipSolution 3
Answer: 270.0.
The expression is 250 * (1 + 0.08) = 250 * 1.08 = 270.0.
TipSolution 4
Answer:
describe_value(principal * (1 + rate))The helper reports the value and its Python type.