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:
| Category | Types |
|---|---|
| Text | str |
| Numeric | int, float, complex |
| Sequence | list, tuple, range |
| Mapping | dict |
| Set | set, frozenset |
| Boolean | bool |
| Binary | bytes, bytearray, memoryview |
| None | NoneType |
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