Python Basics

Python Data Types

Explore all Python data types including strings, numbers, lists, tuples, dicts, and sets.

Python Data Types

In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.

Python has the following built-in data types:

CategoryTypes
Textstr
Numericint, float, complex
Sequencelist, tuple, range
Mappingdict
Setset, frozenset
Booleanbool
Binarybytes, bytearray, memoryview
NoneNoneType

Type Conversion

You can convert from one type to another using built-in functions: int(), float(), str(), list(), tuple(), set(), dict().

Example

python
# Strings
name = "Python"
multiline = """Line 1
Line 2
Line 3"""

# Numbers
age = 30          # int
price = 19.99     # float
z = 1+2j          # complex

# Boolean
is_valid = True
is_empty = False

# List (mutable, ordered)
fruits = ["apple", "banana", "cherry"]
fruits.append("date")

# Tuple (immutable, ordered)
coordinates = (10.5, 20.3)

# Dictionary (key-value pairs)
person = {
    "name": "Alice",
    "age": 28,
    "skills": ["Python", "ML"]
}

# Set (unique, unordered)
unique_nums = {1, 2, 3, 2, 1}  # {1, 2, 3}

# Type conversion
num_str = "42"
num_int = int(num_str)
num_float = float(num_str)
print(type(num_int))   # <class 'int'>
Try it yourself — PYTHON