NelsonLabs
Python Basics/Variables & Data Types

Variables & Data Types

Python is dynamically typed โ€” you don't declare types, Python infers them at runtime. But understanding the core types is fundamental to writing correct Python code.

Python data types
python
# int โ€” whole numbers (no size limit in Python)
age     = 26
year    = 2025
big_num = 1_000_000  # underscore makes large numbers readable

# float โ€” decimal numbers
price  = 9.99
pi     = 3.14159
rate   = 0.075

# str โ€” text (single or double quotes โ€” both work)
name      = "Nelson Njihia"
language  = 'Python'
multiline = """
This string
spans multiple
lines.
"""

# bool โ€” True or False (capital T and F)
is_active = True
is_admin  = False

# None โ€” the absence of a value (like null in other languages)
result = None

# Check types
print(type(name))    # <class 'str'>
print(type(age))     # <class 'int'>
print(type(pi))      # <class 'float'>
print(type(True))    # <class 'bool'>
print(type(None))    # <class 'NoneType'>

# Type conversion
str(42)       # "42"
int("42")     # 42
float("3.14") # 3.14
bool(0)       # False
bool(1)       # True

NOTE

Python's truthiness rules. In Python, these values are falsy: False, None, 0, 0.0, "" (empty string), [] (empty list), {} (empty dict), () (empty tuple). Everything else is truthy. This makes conditions like if username: or if records: clean and idiomatic.