Python Data Types

Data types define the kind of value a variable can store. Python has several built-in data types categorized as:

Numeric Types: int, float, complex
Text Type: str (string)
Sequence Types: list, tuple, range
Set Types: set, frozenset
Mapping Type: dict (dictionary)
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview

1. Numeric Data Types

1.1 Integer (int)

Used for whole numbers (positive, negative, or zero).

age = 25
year = 2024
balance = -1000
print(type(age))  # Output: <class 'int'>

Try It Now

1.2 Float (float)

Used for decimal numbers.

price = 49.99
pi = 3.14159
print(type(price))  # Output: <class 'float'>

Try It Now

1.3 Complex (complex)

Used for complex numbers (a + bj).

z = 3 + 4j
print(type(z))  # Output: <class 'complex'>

Try It Now

2. String Data Type (str)

A string is a collection of characters enclosed in single (') or double (") quotes.

name = "Alice"
greeting = 'Hello, World!'
print(type(name))  # Output: <class 'str'>

Try It Now

Multiline Strings

Use triple quotes (""" """ or ''' ''') for multiline strings.

message = """Hello,
This is a multiline string."""

Try It Now

String Operations

text = "Python"
print(text.upper())     # Convert to uppercase
print(text.lower())     # Convert to lowercase
print(len(text))        # Get string length
print(text[0])         # Access first character (P)

Try It Now

3. Boolean Data Type (bool)

Boolean values are True or False (case-sensitive).

is_active = True
is_closed = False
print(type(is_active))  # Output: <class 'bool'>

Try It Now

4. List (list) – Ordered, Mutable Collection

A list is an ordered collection that allows modifications.

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")  # Add item
fruits.remove("banana")  # Remove item
print(fruits[0])  # Access first item

Try It Now

5. Tuple (tuple) – Ordered, Immutable Collection

A tuple is similar to a list but cannot be changed after creation.

coordinates = (10, 20, 30)
print(coordinates[1])  # Output: 20
# coordinates[0] = 50  # ❌ Error: Tuples are immutable

Try It Now

6. Dictionary (dict) – Key-Value Pairs

A dictionary stores data in key-value pairs.

person = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}
print(person["name"])  # Output: Alice
person["age"] = 26  # Update value

Try It Now

7. Set (set) – Unordered, Unique Items

A set contains unique, unordered items.

numbers = {1, 2, 3, 4, 4}  # Duplicate 4 is ignored
numbers.add(5)
numbers.remove(3)
print(numbers)

Try It Now

Checking Data Type with type()

x = 10
print(type(x))  # Output: <class 'int'>

y = "Hello"
print(type(y))  # Output: <class 'str'>

Try It Now