Episode 5: Understanding Variables in Python


Welcome back to The Medical Coder’s Path!
In the previous episode, you wrote your very first lines of Python code. You learned how to use the print()
function, create basic variables, and write comments.
Now, we will dive a bit deeper into an important building block of Python: the variable.
🧠 What You’ll Learn in This Episode:
What is a variable in Python, and how does it work
What is the Assignment Operator in Python
Rules for Naming Variables in Python
Best practices for naming variables
🔡 What is a Variable?
A variable is a name you assign to a piece of information so Python can store and use it later.
Think of it like a labeled box that holds a value.
patient_name = "Fatima"
Here:
patient_name
is the variable"Fatima"
is the value stored inside it
Variables are essential in programming because they make your code:
1/ Accessible
When you assign a value to a variable, you can reuse that value anywhere in your program without needing to retype it.
In simple words, Variables act like labels, so you don’t have to remember or rewrite the actual value.
📌 Example:
hospital_name = "Green Valley Clinic"
print("Welcome to", hospital_name)
print("Thank you for visiting", hospital_name)
Instead of writing "Green Valley Clinic"
again and again, you just write the variable hospital_name
.
This approach makes your code easier to read, faster to write, and simpler to update later.
2/ Give values context
By naming a variable, you give meaning to the value it holds.
In simple words, the name of a variable explains what the value is and why it matters.
📌 Example:
temp = 37.5
This is acceptable, but it lacks clarity.
However, this is much better:
patient_temperature = 37.5
Now, anyone reading the code knows this value represents a patient’s temperature, not just a random number.
Giving context helps you understand your code later, allows others to read your code more easily, and reduces mistakes in large projects.
What is the Assignment Operator in Python?
In Python, the =
symbol is called the assignment operator. It is used to assign a value to a variable.
📌 Example:
patient_age = 45
Here, you are telling Python: “Store the value 45
inside the variable called patient_age
.”
So now, whenever you use patient_age
, Python will know that it means 45
.
But what is the difference between the equal sign and the assignment operator?
In mathematics, the equal sign =
means both sides are exactly equal.
For example:
3 + 2 = 5
This is a statement of equality.
But in Python (and most programming languages), =
doesn’t mean “equal to.” It means:
“Take the value on the right and store it in the variable on the left.”
So:
x = 10
Means: “Let the variable x
hold the value 10
.”
This is a one-way operation, not a comparison.
⚠️ Common Mistake:
People often confuse the assignment operator (=
) with the comparison operator (==
).
=
is used to assign values to variables==
is used to compare values (we’ll learn this in future episodes)
🏷️ Rules for Naming Variables in Python
When you create a variable in Python, you can choose the name, but it must follow some rules so Python can understand and process it correctly.
Here are the most important rules and best practices:
1. Variable names must start with a letter or an underscore (_
)
Valid:
name = "Ali" _age = 30
❌ Invalid if you start the variable name with a number:
1name = "Sara" # Error! Starts with a number
2. The name can include letters, numbers, and underscores
Valid:
patient1 = "Mariam" patient_name = "Hassan"
❌ Invalid if you use any other special characters like
-
or#
:patient-name = "Amir" # Error! Hyphens are not allowed
3. Variable names are case-sensitive
Name = "Zainab"
name = "Ahmed"
These are two different variables (Name
and name
) because Python treats uppercase and lowercase letters as different.
4. You cannot use Python’s reserved words as variable names
Python's reserved words, like if
, for
, True
, and print
, are special keywords used for its operations and cannot be used as variable names.
❌ Invalid:
if = 5 # ❌ Error: "if" is a reserved word
✅ Instead, use something like:
if_value = 5
💡 Best Practices:
Use clear, meaningful names:
Good: patient_name
, temperature
, heart_rate
Bad: a
, b
, x1
(not descriptive)
Use lowercase letters and underscores (snake_case)
In Python, the standard way to name variables is to use lowercase letters with words separated by underscores.
✅ Recommended:
patient_name = "Ali"
blood_pressure = 120
This style is called “snake_case” and is the preferred naming convention in Python.
❌ Not recommended:
PatientName = "Ali" # CamelCase – more common in other languages like Java
Avoid using very long names unless necessary
🧪 Practice Exercise: Create a Simple Hospital Intake Form
Scenario:
You’re working at the reception desk of a hospital. Create a Python program that stores a new patient’s basic information using variables.
Your task:
Create 5 variables to store:
Patient name
Patient ID number
Department (e.g., “Radiology”)
Doctor’s name
Room number
Use
print()
to display the patient’s intake form in a clean formatAdd comments to explain what each line is doing
📝 Example Output:
Patient Intake Form
---------------------
Name: Huda Kareem
Patient ID: 10432
Department: Radiology
Doctor: Dr. Salim
Room No: 12B
Use clear and meaningful variable names.
After you finish, send your answer to the following email: saja@sajamedtech.com
📩 Coming Up Next in Episode 6:
In the next episode, we will explore data types, how Python understands the kind of data you are working with (like text, numbers, or true/false values), and why it matters.
See you tomorrow!
Subscribe to my newsletter
Read articles from Saja Ahmed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
