When learning Python, two of the first concepts you need to understand are variables and data types.
Variables allow you to store information and use it later in your program, while data types tell Python what kind of information you are working with.
What Is a Variable?
A variable is a name given to a piece of information so you can use it later in your program.
Imagine you track your daily steps. You walked 9,200 steps today. Without a variable, you would have to type 9200 every time your program needed that number.
With a variable, you store the value once and use its name everywhere else:
steps = 9200
You can think of a variable like a labelled jar.
- The label is
steps - The value inside is
9200
If you walk 10,400 steps tomorrow, you can simply update the value:
steps = 10400
The variable name stays the same, but its value changes.
Variable Assignment
The basic syntax for creating a variable is:
variable_name = value
For example:
steps = 9200
name = "Melvin"
body_weight = 82.5
workout_done = True
The variable name is on the left, the value is on the right, and = assigns the value to the variable.
In Python,
=means assignment. It does not mean that the two sides are mathematically equal.
For example:
steps = 9200
means:
Store the value
9200in the variable calledsteps.
Naming Your Variables
Python gives you flexibility when naming variables, but there are some important rules and conventions.
| Rule | Good | Bad |
|---|---|---|
| No spaces | water_glasses |
water glasses |
| Cannot start with a number | day1_steps |
1steps |
| Avoid special characters | sleep_hours |
sleep-hours |
| Use descriptive names | bench_press_kg |
x |
Python variable names are case-sensitive.
steps = 9200
Steps = 10000
These are two different variables.
The standard Python convention is to use lowercase letters with underscores, known as snake_case:
daily_steps = 9200
body_weight_kg = 82.5
workout_completed = True
Good variable names make your code easier to understand.
What Are Data Types?
Not all information is the same.
For example:
-
9200is a number -
"Monday"is text -
82.5is a decimal number -
Truerepresents a yes/no condition
Python uses data types to identify what kind of value a variable contains.
Four data types you will use frequently are:
-
int- whole numbers -
float- decimal numbers -
str- text -
bool- True or False
1. int - Whole Numbers
An int (integer) represents a whole number without a decimal point.
steps = 9200
water_glasses = 8
pages_read = 30
sleep_hours = 7
bench_press_reps = 12
Integers are commonly used for things such as counts, quantities, and measurements that don't require decimals.
2. float - Decimal Numbers
A float represents a number containing a decimal point.
body_weight_kg = 82.5
km_walked = 7.3
body_fat_percent = 14.2
Floats are useful when precision is required, such as measurements, percentages, prices, and distances.
3. str - Text
A str (string) represents text.
Strings must be enclosed in quotation marks. You can use either single or double quotes.
fasting_protocol = "OMAD"
skill_of_the_day = "Welding"
name = "Melvin"
morning_goal = "8,000 steps before 9 AM"
Python treats the text inside the quotes as a string.
4. bool - True or False
A bool (Boolean) represents one of two values:
TrueFalse
For example:
cold_shower = True
workout_done = False
fasting_active = True
Booleans are especially useful when working with conditions and decision-making in programs.
Remember that
TrueandFalsemust begin with capital letters.trueandfalseare not valid Python Boolean values.
Checking the Data Type
Python provides a built-in function called type() that allows you to check the type of a variable.
steps = 9200
body_weight = 82.5
name = "Melvin"
workout_done = True
print(type(steps))
print(type(body_weight))
print(type(name))
print(type(workout_done))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
The type() function is particularly useful when debugging your programs and checking whether a variable contains the type of data you expect.
Printing Variables with Text
You will often want to display a variable together with some descriptive text.
There are two common approaches.
Using Commas
You can pass multiple values to print() separated by commas:
steps = 9200
print("Steps today:", steps)
Output:
Steps today: 9200
Python automatically adds a space between the values.
Using f-Strings
A more flexible and commonly used approach is an f-string.
Place an f before the string and put variables inside {}:
steps = 9200
name = "Melvin"
print(f"{name} walked {steps} steps today.")
Output:
Melvin walked 9200 steps today.
f-strings make it easy to combine text and variables while giving you control over how the output is formatted.
Putting Everything Together
Here's a simple example using variables, different data types, type(), and an f-string:
name = "Melvin"
steps = 9200
body_weight_kg = 82.5
workout_done = True
print(f"Name: {name}")
print(f"Steps: {steps}")
print(f"Body weight: {body_weight_kg} kg")
print(f"Workout completed: {workout_done}")
print(type(name))
print(type(steps))
print(type(body_weight_kg))
print(type(workout_done))
Understanding variables and data types gives you a foundation for working with conditions, loops, functions, data structures, data analysis, and machine learning in Python.
Key Takeaways
- A variable is a name used to store a value.
- The
=operator assigns a value to a variable. - Python variable names are case-sensitive.
- Use descriptive names such as
daily_stepsinstead of unclear names such asx. -
intstores whole numbers. -
floatstores decimal numbers. -
strstores text. -
boolstoresTrueorFalse. - Use
type()to check a variable's data type. - Use f-strings to format text containing variables.
These concepts may look simple, but they form the foundation of almost everything you will build with Python.
Top comments (0)