Lesson Objectives
By the end of this lesson, you will be able to:
- Understand what variables are and why they matter
- Assign and reassign variables correctly
- Follow Python naming conventions
- Read and write clean, predictable Python code
These concepts are non-negotiable for any serious use of Python in statistics, data analysis, or machine learning.
What Is a Variable in Python
A variable is a name bound to a value.
In Python, variables are created when you assign a value to them.
age = 40
This line means:
- create a name:
age - bind it to the value:
40
Python does not require prior type declaration.
Variable Assignment
Basic Assignment
name = "Michele"
height = 1.78
is_doctor = True
Python automatically infers the type.
Reassignment
Variables can be reassigned at any time:
age = 40
age = 41
The previous value is overwritten.
This flexibility is powerful, but requires discipline.
Dynamic Typing (Important Concept)
Python is dynamically typed:
x = 10
x = "ten"
This is allowed.
However, changing meaning mid-code is bad practice in analytical workflows.
Rule of thumb:
A variable name should always represent the same concept.
Naming Rules (Syntax-Level Rules)
Python variable names must:
- start with a letter or
_ - contain only letters, numbers,
_ - be case-sensitive
Valid Names
age
patient_age
_age
age2
Invalid Names
2age
patient-age
patient age
Naming Conventions (Professional Rules)
Python follows snake_case by convention:
patient_age
mean_value
total_score
Avoid:
PatientAge
patientAge
x1
tmp
In data analysis and ML, clear variable names are critical for reproducibility.
Comments in Python
Comments are ignored by Python and used to explain code.
Single-line Comment
# This is a comment
age = 40
Why Comments Matter
- explain why, not what
- document assumptions
- clarify non-obvious logic
Bad comment:
age = 40 # assign age
Good comment:
age = 40 # age at admission
Inspecting Variables
You can print variables:
print(age)
print(name)
You can also inspect their type:
type(age)
Expected output:
<class 'int'>
This becomes important when debugging analytical code.
Common Beginner Mistakes
Using reserved keywords
class = 10
Error:
SyntaxError
Python has reserved keywords (if, for, class, def, etc.).
Ambiguous variable names
x = 120
Later, no one remembers what x means — including you.
How This Applies to Data Analysis
In real-world analytical code, variables often represent:
- patient-level features
- statistical parameters
- model outputs
- intermediate transformations
Clear variable naming directly impacts:
- readability
- correctness
- reproducibility
FAQ — Frequently Asked Questions
Q: Do I need to declare variable types?
A: No. Python infers them automatically.
Q: Can I change a variable’s type?
A: Yes, but you usually shouldn’t in analytical code.
Q: Are variables copied or referenced?
A: Assignment binds names to objects. This matters later with collections.
Q: Why is snake_case important?
A: Consistency improves readability and collaboration.
Exercises (10)
Exercise 1
Create a variable called age and assign it a number.
Exercise 2
Create a variable name with your name.
Exercise 3
Print both variables.
Exercise 4
Reassign age with a new value.
Exercise 5
Create a variable height_m.
Exercise 6
Check the type of height_m.
Exercise 7
Create a boolean variable.
Exercise 8
Write a meaningful comment for a variable.
Exercise 9
Create two variables and swap their values.
Exercise 10
Predict the output before running:
x = 5
x = x + 2
print(x)
Solutions
Exercise 1
age = 40
Exercise 2
name = "Michele"
Exercise 3
print(age)
print(name)
Exercise 4
age = 41
Exercise 5
height_m = 1.78
Exercise 6
type(height_m)
Exercise 7
is_active = True
Exercise 8
patient_id = 1023 # unique identifier
Exercise 9
a = 1
b = 2
a, b = b, a
Exercise 10
# Output: 7
Next Lesson Preview
In Lesson 04, you will learn:
- core Python data types (
int,float,str,bool) - type conversion
- why types matter in numerical computation