FoundationsLesson 2 of 1914 min
Variables and Types
Give values names so you can reuse them, and learn the four core types you will use constantly.
By the end you will be able to
- Assign values to variables and reassign them
- Identify int, float, str, and bool values
- Convert between types deliberately with int(), float(), and str()
A variable is a name attached to a value. You create one with =, which is not the equals of mathematics — read it as "gets" or "is assigned".
Python 20 4.8 True
— expected output; press Run to execute it yourselfThe name goes on the left, the value on the right. Python evaluates the right side first, then binds the result to the name.
A variable can be reassigned at any time, and the new value can even be a different type.
10
15
ten
— expected output; press Run to execute it yourselfThe four core types
| Type | Meaning | Examples |
|---|---|---|
int | whole number | 0, 42, -7 |
float | number with a decimal point | 3.14, -0.5, 2.0 |
str | text, in quotes | "hi", 'a', "" |
bool | truth value | True, False |
type() tells you what you are holding — invaluable when debugging.
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
— expected output; press Run to execute it yourselfConverting on purpose
When you do need to cross between types, say so explicitly with int(), float(), or str().
31
39.98
Year 2026
7
— expected output; press Run to execute it yourselfNaming rules and habits
Names may contain letters, digits, and underscores, and may not start with a digit. They are case-sensitive: total and Total are two different variables.
The Python convention is snake_case — lowercase words joined by underscores. Prefer items_sold over x; the extra typing pays for itself the first time you reread the code.
Exercise
Create a variable minutes holding the integer 150. Then create hours holding that value converted to a float number of hours (150 / 60). Print hours.
Press Run to execute this code.