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.

price = 100
growth_rate = 0.05
name = "portfolio"

price, growth_rate, name
(100, 0.05, 'portfolio')

Python keeps track of the type of each value.

type(price), type(growth_rate), type(name)
(int, float, str)

Arithmetic

initial = 100
rate = 0.05
after_one_year = initial * (1 + rate)

after_one_year
105.0

Common arithmetic operators:

Operator Meaning
+ addition
- subtraction
* multiplication
/ division
** exponentiation

Python Connection

from edumath.python_fundamentals import describe_value

describe_value(after_one_year)
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

  1. Create a variable named principal with value 250.
  2. Create a variable named rate with value 0.08.
  3. Compute principal * (1 + rate).
  4. Use describe_value(...) on the result.

Solutions

Answer: principal = 250.

Answer: rate = 0.08.

Answer: 270.0.

The expression is 250 * (1 + 0.08) = 250 * 1.08 = 270.0.

Answer:

describe_value(principal * (1 + rate))

The helper reports the value and its Python type.