🏷️ Data Types

Learn about the different types of data you can store and work with in Python.

What you'll learn:

  • The main data types: strings, integers, floats, and booleans
  • How to check what type a value is using type()
  • Casting: converting between types using int(), str(), float()
  • Why data types matter in programming

Learning Path

Follow these activities in order.

Casting (Type Conversion)

Casting means converting data from one type to another. This is essential when working with user input or combining different types of data.

int()

Converts to integer (whole number)

# String to integer
age_str = "25"
age = int(age_str)      # 25

# Float to integer (removes decimal)
price = 9.99
whole = int(price)      # 9

# User input (always a string!)
num = int(input("Enter number: "))
Warning: int("hello") causes an error! Only numeric strings can be converted.

float()

Converts to float (decimal number)

# String to float
price_str = "19.99"
price = float(price_str) # 19.99

# Integer to float
count = 5
decimal = float(count)   # 5.0

# Division result
result = float(7) / 2   # 3.5

str()

Converts to string (text)

# Integer to string
age = 15
age_text = str(age)      # "15"

# Float to string
price = 9.99
price_text = str(price)  # "9.99"

# Concatenating with strings
print("Age: " + str(age))  # "Age: 15"

bool()

Converts to boolean (True/False)

# Numbers to boolean
bool(0)      # False (zero)
bool(1)      # True (non-zero)
bool(-5)     # True (non-zero)

# Strings to boolean
bool("")     # False (empty)
bool("hi")   # True (non-empty)
bool("False") # True (non-empty string!)

Why Do We Need Casting?

User Input is Always a String

# This won't work as expected!
age = input("Enter age: ")  # "25" (string)
if age > 18:                # ERROR! Can't compare string to int

# Fix: Convert to integer first
age = int(input("Enter age: "))  # 25 (integer)
if age > 18:                     # Works!

Combining Text and Numbers

# This causes a TypeError!
score = 95
print("Score: " + score)     # ERROR!

# Fix 1: Convert to string
print("Score: " + str(score)) # "Score: 95"

# Fix 2: Use f-string (recommended)
print(f"Score: {score}")     # "Score: 95"

Key Vocabulary

data type

The category of data a value belongs to. Determines what operations you can perform.

string (str)

Text data, written in quotes. "Hello", 'Python', "123"

integer (int)

A whole number with no decimal. 42, -7, 0

float

A decimal number. 3.14, -0.5, 2.0

boolean (bool)

True or False values. True, False

casting

Converting data from one type to another. Also called type conversion.

int()

Converts a value to an integer. int("5") gives 5

str()

Converts a value to a string. str(42) gives "42"

float()

Converts a value to a float. float("3.14") gives 3.14

Quick Reference

data_types_examples.py
# String - text in quotes
name = "Alice"
message = 'Hello World'

# Integer - whole numbers
age = 15
score = -10

# Float - decimal numbers
price = 9.99
temperature = -2.5

# Boolean - True or False
is_student = True
has_passed = False

# Check data type
print(type(name))    # <class 'str'>
print(type(age))     # <class 'int'>
print(type(price))   # <class 'float'>

# Type conversion
num_str = "42"
num_int = int(num_str)    # String to integer
num_float = float(num_str) # String to float
back_to_str = str(42)     # Integer to string