Variables and Variable Types¶
Naming and storing values in Python.
What Is a Variable¶
In Python, a variable is a name that holds a value.
A variable associates a readable name with a piece of data so it can be reused and inspected throughout a program.
Common Variable Types¶
| Type | Holds | Example value |
|---|---|---|
str |
text | "Kansas City" |
int |
a whole number | 42 |
float |
a decimal number | 3.14 |
bool |
true or false | True |
list[str] |
an ordered collection of text values | ["patience", "curiosity"] |
Type Hints¶
Type hints make the intended type of a variable explicit.
name: str = "Abbie"
age: int = 5
weight_lbs: float = 9.8
is_cat: bool = True
skills: list[str] = ["patience", "curiosity"]
Benefits:
- clearer intent
- better editor feedback
- easier to collaborate
Type hints do not change how Python runs the code.
Include Units in Numeric Names¶
Units matter when working with numbers. Build the unit into the variable name to avoid confusion.
Constants¶
Values that do not change are indicated by:
- naming them in
ALL_CAPS_WITH_UNDERSCORES - adding
Finalto the type hint
Collections¶
A list holds an ordered sequence of values of the same type.
The type inside the square brackets is called a type parameter.
It describes what kind of values the list contains.
A dict maps keys to values.
Two type parameters are used: one for the key type, one for the value type.
dict is introduced in a later module. It is shown here for completeness.
Why Variables Come First¶
Variables are introduced early because:
- programs are built from values
- data analysis depends on naming data clearly
- readable variable names make intent visible to anyone reading the code