Variables in Python#
š Data Analysis Using Python š Python Fundamentals Lesson 005
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Naming values for reuse#
A program that could not remember values would be useless; variables are how Python remembers. A variable is a named container for a value, letting you store data, refer to it by name, and change it as the program runs. This lesson covers variables in Python ā assigning, reassigning, and using them ā the foundation of holding data in code.
Assignment#
A variable is created by assigning a value to a name with =:
sales = 1000
region = "North"
tax_rate = 0.08
The name on the left is bound to the value on the right; afterwards, the name stands for the value:
total = sales + sales * tax_rate
print(total) # 1080.0
print(region) # North
The = here is assignment, not mathematical equality ā it means ālet this name
refer to this value,ā a distinction worth keeping clear.
Reassignment#
A variableās value can change ā reassigning binds the name to a new value:
count = 5
count = count + 1 # count is now 6
count += 1 # shorthand for count = count + 1; now 7
The last form, +=, is an augmented assignment ā a common shorthand for updating a
variable based on its current value (-=, *=, /= work similarly). Reassignment
is what lets a variable track a changing value as a program runs.
Why variables matter#
Variables serve the same purposes as naming anything: reuse (compute a value once,
use it many times), clarity (a well-named variable documents what a value means ā
tax_rate is clearer than 0.08 scattered through code), and changeability
(update a value in one place and everything using it updates). This is the
abstraction-and-naming principle from the foundations, in code: meaningful names for
values make programs readable and maintainable, exactly as meaningful column names make
data readable.
The caveat#
Variables have subtleties that catch beginners. A variable must be assigned before it
is used ā referencing a name Python has not seen raises an error. Reassignment means a
variableās value depends on when you look (its value is whatever was last assigned),
which matters especially in notebooks where cells run out of order ā a variable can hold
a surprising value if cells ran in an unexpected sequence. And Python variables are
case-sensitive (Sales ā sales), a frequent source of āundefined nameā errors.
These are learned by writing code and reading the errors, which say precisely what went
wrong. The next lesson covers naming variables well.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/variables-in-python/ (insightful-data-lab.com).