Lesson Objectives
By the end of this lesson, you will be able to:
- Understand Python’s core data types
- Distinguish between numeric, textual, and logical data
- Perform basic type conversions
- Recognize why data types are critical in statistical and analytical code
This lesson is essential:
most bugs in analytical Python code are type-related.
1️⃣ What Is a Data Type
A data type defines:
- what kind of value a variable holds
- what operations are allowed on that value
In Python, every object has a type.
x = 10
Here:
10is an integerxis bound to an object of typeint
2️⃣ Integer (int)
Integers represent whole numbers, positive or negative.
a = 10
b = -3
Key properties:
- no decimal point
- arbitrary precision (no overflow in practice)
Integers are widely used for:
- counts
- indices
- categorical encodings
3️⃣ Floating Point Numbers (float)
Floats represent real numbers with decimals.
x = 3.14
y = 2.0
Important detail:
- floats are approximations, not exact values
Example:
print(0.1 + 0.2)
Output:
0.30000000000000004
This matters a lot in statistics and numerical analysis.
4️⃣ Strings (str)
Strings represent textual data.
name = "Michele"
city = 'Rome'
Strings can contain:
- letters
- numbers
- symbols
They are not numbers, even if they look like numbers.
x = "10"
This is text, not an integer.
5️⃣ Boolean (bool)
Booleans represent logical values:
is_active = True
is_empty = False
Booleans are fundamental for:
- conditions
- filtering data
- decision logic
Internally, booleans behave like integers:
True == 1
False == 0
This has implications later in modeling and data processing.
6️⃣ Inspecting Types
You can always inspect the type of an object:
type(10)
type(3.14)
type("hello")
type(True)
Expected output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
This is a primary debugging tool.
7️⃣ Type Conversion (Casting)
Python allows explicit type conversion.
Common Conversions
int("10")
float("3.14")
str(100)
bool(1)
Dangerous Conversion
int("ten")
This raises an error.
Type conversion is explicit by design — Python avoids silent errors.
8️⃣ Operations and Types
The result of an operation depends on the involved types.
10 + 2 # int
10 + 2.0 # float
"10" + "2" # string concatenation
Understanding this behavior is essential for numerical correctness.
9️⃣ Why Data Types Matter in Data Analysis
In analytical workflows, data types determine:
- mathematical correctness
- memory usage
- performance
- model behavior
Example:
"5"vs5can silently break an analysis- floats introduce rounding errors
- booleans often encode clinical or experimental conditions
Good analysts are obsessive about types.
FAQ — Frequently Asked Questions
Q: Is Python strongly typed?
A: Yes. Every object has a type, even if you don’t declare it.
Q: Why does Python allow True == 1?
A: For historical and logical consistency, but this must be used carefully.
Q: Should I always use floats for numbers?
A: No. Use integers when values are counts or categories.
Q: Can I mix types freely?
A: Python allows it, but analytical code should minimize ambiguity.
Exercises (10)
Exercise 1
Create an integer variable called n_patients.
Exercise 2
Create a float variable called mean_age.
Exercise 3
Create a string variable called study_name.
Exercise 4
Create a boolean variable called is_valid.
Exercise 5
Check the type of all variables above.
Exercise 6
Convert a string "25" to an integer.
Exercise 7
Convert an integer 5 to a string.
Exercise 8
Predict the output:
print(5 + 2.0)
Exercise 9
Predict the output:
print("5" + "2")
Exercise 10
Explain why this is dangerous in data analysis:
value = "10"
result = value + "5"
Solutions
Exercise 1
n_patients = 120
Exercise 2
mean_age = 67.4
Exercise 3
study_name = "CABG_outcomes"
Exercise 4
is_valid = True
Exercise 5
type(n_patients)
type(mean_age)
type(study_name)
type(is_valid)
Exercise 6
int("25")
Exercise 7
str(5)
Exercise 8
# Output: 7.0
Exercise 9
# Output: "52"
Exercise 10
# String concatenation instead of numeric addition
Next Lesson Preview
In Lesson 05, we will introduce:
- conditional logic (
if,else) - loops (
for,while) - control flow as the basis of algorithmic thinking
