Python Data Types

What Are Data Types?

Data types define the kind of value a variable holds. Python has several built-in types:

TypeExampleDescription
intx = 5Whole number
floatpi = 3.14Decimal number
strname = "Alice"Text string
boolis_ready = TrueTrue or False
list[1, 2, 3]Ordered collection
tuple(1, 2)Immutable ordered collection
dict{"id": 1}Key-value mapping
set{'apple', 'banana'}Unique unordered values

How to Check a Type

Use type() to find out the data type of a variable:

print(type("hello"))  # <class 'str'>
print(type(3.14))     # <class 'float'>

Type Conversion (Casting)

You can convert values from one type to another using built-in functions:

FunctionConverts ToExample
int()Integerint("5") → 5
float()Floatfloat("3.14") → 3.14
str()Stringstr(42) → "42"
bool()Booleanbool(0) → False
x = "100"
y = int(x)         # 100
z = float(x)       # 100.0
score = 85
text = str(score)  # "85"

Note: Invalid strings cannot be converted to numbers, e.g., int("five") will throw an error.

Dynamic Typing

Python is dynamically typed, which means you don’t need to declare a variable’s type and it can change at runtime:

x = 10       # int
x = "ten"    # now a str

This adds flexibility but can lead to errors if you're not careful with operations between types:

x = "5"
print(x + 1)  # ❌ TypeError: can't concatenate str and int

More Real-World Examples

name = "Alice"
age = 21
gpa = 3.82
is_graduated = False
grades = [90, 85, 95]
coordinates = (41.40338, 2.17403)
student = {"id": 1001, "name": "Bob", "enrolled": True}
tags = {"python", "react", "flask"}

These types are commonly used in real applications — like storing user data, location, scores, and tags.

Practice Question

What will this print?

Loading...
Output:

Hint: What is the type of x after the last assignment?

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic