15 min read Core Concept Lesson 4 of 16
Course 03 · Part 1 — Thinking & Basics

Data Types: What Kind of Data is a Value?

Classifying Information and Governing Legal Operations in Software

In the previous lesson, we learned how to store values inside variables: name = "Salim", age = 25, and price = 99.5. But if you analyze these values closely, they are fundamentally different. "Salim" is human prose (text), 25 is a discrete counting number, and 99.5 is a continuous fractional quantity. Computers treat these distinctions with absolute precision. The framework that defines the identity and permissible actions for any value is its Data Type.

Identity Determines Behavior

In physical reality, you cook rice to eat, but you read a book to learn—you cannot eat a book or read a bowl of rice. In programming, numbers can be multiplied or subtracted, but textual sentences cannot be divided. What you are permitted to do with any piece of data is strictly dictated by its Data Type.

01 What Truly is a Data Type?

Stated simply and definitively:

A Data Type is a classification that tells the compiler or runtime what kind of value an expression represents, how it is stored in physical memory, and which operations can legally be performed upon it.

For example:

age = 25 # Here 25 is an Integer (whole number)
name = "Salim" # Here "Salim" is a String (textual data)

Both values occupy memory in your machine, but their binary encoding and behavioral capabilities are entirely distinct.

02 Why Do Data Types Matter? (25 vs. "25")

Observe these two expressions carefully:

Number (Integer):
25 + 10
Result: 35 (Arithmetic Addition)
Text (String):
"25" + "10"
Result: "2510" (String Concatenation)

The exact same plus operator (+) produces completely different results based on the types involved! For numbers, it executes mathematical addition. For strings, it fuses the text sequences together (concatenation). Failing to track data types is a prime source of critical arithmetic bugs in production software.

03 Python's 5 Fundamental Primitive Data Types

To start building practical applications, you must master these five foundational types:

int (Integer)
Whole numbers with no decimal fraction (e.g., 25, -10, 0).
float (Floating-Point)
Numbers containing fractional decimal parts (e.g., 99.5, 3.1416, -0.5).
str (String)
Literal textual characters enclosed within quotation marks (e.g., "Salim", 'Dhaka').
bool (Boolean)
Logical truth state. Can only be one of two values: True or False.
NoneType (None)
Represents the explicit absence of a value. The variable exists, but currently references no payload (None).

04, 05 & 06 Numbers: Integer vs. Float

Numeric computation in Python divides primarily into discrete counts (int) and real measurements (float):

quantity = 10 # int: positive whole number
temperature = -5 # int: negative whole number
unit_price = 99.50 # float: decimal number
rating = 4.8 # float: fractional rating
Are 10 and 10.0 identical?

While they share mathematical equivalence, their machine representations are completely different! 10 is an <class 'int'>, whereas 10.0 is a <class 'float'>. Floats are structured in memory following the IEEE 754 standard, segmented into sign, exponent, and mantissa bits.

07 & 08 String — Textual Data

Any sequence of characters wrapped within quotation marks is a String. Python supports both single and double quotes interchangeably:

first_name = "Salim"
city = 'Dhaka'
quote = "Python makes coding enjoyable!"
Remember: Quotation marks create a String!

score = 100 is an integer. But if you mistakenly write score = "100", Python considers it plain text. Attempting to add numbers to it directly will cause an immediate crash.

09 & 10 Boolean — Truth and Decision-Making

Software constantly evaluates questions of truth. To represent truth states, Python provides the Boolean type. A boolean can take only two values: True or False (capitalization is mandatory):

is_logged_in = True
has_paid = False
is_student = True

Every modern software system asks questions: Is the session active? Does the user have admin privileges? Has the checkout payment cleared? Each answer maps cleanly to a boolean flag. In upcoming lessons on control flow (if/else), booleans will govern every decision your program makes.

11 & 12 `None` — The Explicit Absence of Value

Often, a variable must be defined before its value is known (e.g., an optional middle name field that the user left blank). Python represents this state using None:

middle_name = None

These 4 values mean completely different things:

None: Total absence of any value
0: A legitimate integer zero
False: A boolean false truth value
"": A string with length zero

13, 14 & 15 The `type()` Function & Dynamic Typing

Python is a Dynamically Typed Language. The runtime automatically infers types from assigned values without explicit type declarations. To inspect the underlying type of any variable or expression, Python offers the built-in type() function:

check_types.py
print(type(25)) # <class 'int'>
print(type(99.5)) # <class 'float'>
print(type("Salim")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>

16 & 17 Incompatible Combinations: `TypeError`

If you attempt an operation between types that have no logical compatibility, Python halts with a TypeError:

# This instruction will trigger an error:
age = 25
message = "I am " + age
TypeError: can only concatenate str (not "int") to str

Python explains clearly: “You cannot concatenate an integer directly to a string!” To resolve this, we employ Type Conversion.

18 to 22 Type Conversion (Type Casting)

Converting a value from one data type into another is known as Type Conversion or Type Casting. Python provides straightforward conversion functions:

1. str() — Convert to String:
age = 25
message = "I am " + str(age)
# Result: "I am 25" (Valid)
2. int() — Convert to Integer:
user_input = "25"
numeric_age = int(user_input)
# numeric_age + 5 produces: 30
3. float() — Convert to Float:
price_str = "99.5"
price = float(price_str)
# Result: 99.5 (float)
4. bool() — Convert to Truth Value:
bool(1) # True
bool(0) # False
bool("") # False (empty string)
bool("hi") # True (non-empty string)
Not all conversions are possible!

Executing int("hello") causes Python to throw ValueError: invalid literal for int() with base 10: 'hello'. You cannot parse arbitrary alphabetic letters into base-10 numbers. The source string must contain valid numeric digits.

23 Real-World Example: Web Form Data Ingestion

When web applications receive user inputs via HTTP forms or REST APIs, the data arrives almost universally as raw Strings:

registration.py
# Raw data delivered from an HTML web form (all strings!)
raw_name = "Salim"
raw_age = "25"
raw_wallet = "1500.50"
# Type conversion into numerical domains
age = int(raw_age)
wallet = float(raw_wallet)
# Arithmetic calculations now succeed seamlessly!
next_year_age = age + 1
updated_wallet = wallet - 200.0
print(raw_name, "next year:", next_year_age)
print("Balance after fee:", updated_wallet)
Output:
Salim next year: 26
Balance after fee: 1300.5

24 Connection to Computer Science Foundations

Recall from Course 01: at the transistor level, hardware recognizes only patterns of 0s and 1s. The CPU does not inherently know whether a sequence of 32 bits is an integer, an IEEE float, a slice of an image, or an ASCII character.

The high-level Data Type abstraction attaches meaning to raw bits, directing the Python VM which machine operations to invoke:

Binary Bits (0/1) → Data Type Abstraction → Human Meaning → Valid Operations

25 & 26 The 5 Core Types at a Glance & A Critical Warning

name = "Salim" # str (Text)
age = 25 # int (Whole Number)
height = 1.75 # float (Decimal Fraction)
is_student = False # bool (Logic State)
middle_name = None # NoneType (Absence of Value)
⚠️ The Golden Triad Comparison:
25 Type: int Computable math (+, -)
25.0 Type: float Decimal precision
"25" Type: str Pure textual character sequence

27 Today's Mental Model

Program
↓
Variables (Labels and Identifiers)
↓
Values (Entities in Memory)
↓
Data Types (Identity & Classification)
↓
Operations (Valid Transformations & Output)

28 Hands-On Practice Challenges

Open datatypes_practice.py in your editor and complete these exercises:

Challenge 1: Type Inspection Declare variables for your name, age, height (meters), student status (True/False), and phone number (None). Print each variable alongside its type().
Challenge 2: Type Casting Experiments Cast these four values into int(), float(), and str(), observing which succeed and which fail:
val1 = "100"
val2 = "50.5"
val3 = 100
val4 = 100.0
Challenge 3: Concatenation vs. Addition Compare print("50" + "50") with print(int("50") + int("50")) and analyze the divergent outcomes.

29 🧠 Key Takeaways & Summary

Data Type: The identity that dictates how values are structured in memory and what operations are valid.
Primitive Types: int (whole numbers), float (decimals), str (text), bool (truth states), and NoneType (absence of value).
type() Inspection: The built-in procedure to diagnose an object's runtime class.
Type Casting: Explicit conversion using int(), float(), str(), or bool().
Dynamic Typing: Python infers types at runtime; variables require no static type declarations.

Next Lesson: Input, Output & Type Conversion

So far, all program data has been hardcoded in source files. How do programs accept live, interactive input from terminal users and transform it for dynamic computations?