Python data types define the kind of value a variable holds and the operations that can be performed on it. Python is dynamically typed, so you don’t need to declare variable types explicitly.
name = "Viet" # strage = 25 # intprice = 99.99 # floatis_active = True # boolresult = None # NoneType
Use type() to inspect a value’s type:
print(type(age)) # <class 'int'>
Python provides four common collection types:
| Type | Characteristics |
|---|---|
list | Ordered, mutable, allows duplicates |
tuple | Ordered, immutable, allows duplicates |
set | Unordered, unique elements |
dict | Key-value pairs, mutable, unique keys |
numbers = [1, 2, 2, 3] # listcoordinates = (10, 20) # tupleunique_numbers = {1, 2, 3} # setuser = {"name": "Viet", "age": 25} # dict
Convert values using built-in functions such as int(), float(), str(), and bool().
age = int("25")price = float("19.99")message = str(100)
Invalid conversions can raise exceptions, such as ValueError.
Python allows a variable to reference values of different types.
value = 10value = "hello"
Type hints document expected types and support static analysis, but Python does not enforce them automatically at runtime.
def add(a: int, b: int) -> int:return a + b