micheledpierri.com

  • HOME
    • Python
    • Statistics
    • Data Analysis
    • Machine Learning
  • WRITINGS
  • VISIONS
  • ABOUT
Home / Python / Core Data Types in Python
Children dressed in lab coats are studying a python

Core Data Types in Python

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:

  • 10 is an integer
  • x is bound to an object of type int

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" vs 5 can 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

This is where Python starts to reason, not just store data.

Cite this article

Pierri, M. D. (2026). Core Data Types in Python. micheledpierri.com. Permalink

Share:Email·LinkedIn
Previous← Variables, Naming Rules, and Basic SyntaxNextControl Flow: Conditions and Loops →
Python
  1. Why Python
  2. Python & VS Code Setup (From Zero to a Professional Environment)
  3. Variables, Naming Rules, and Basic Syntax
  4. Core Data Types in Python
  5. Control Flow: Conditions and Loops
  6. Functions and Code Reusability
  7. Collections: Lists, Tuples, Sets, and Dictionaries
  8. Modules, Packages, and File Handling
  9. Errors, Exceptions, and Robust Code
  10. Object-Oriented Programming (OOP) in Python
  11. Intermediate Python: Writing Clean, Pythonic Code
  12. Python Wrap-Up Lesson: A Mini Cardiology Risk-Factor Audit (Step-by-Step)
© 2024–2026 micheledpierri.com · Privacy Policy · Impressum