Numeric Types
Python has three built-in numeric types: int, float, and complex. int has unlimited precision.
# int - whole numbers, unlimited size
x = 42
y = -100
big = 1_000_000_000 # underscores for readability (Python 3.6+)
binary = 0b1010 # binary literal -> 10
octal = 0o17 # octal literal -> 15
hexadec = 0xFF # hex literal -> 255
print(type(x)) # <class 'int'>
# float - 64-bit IEEE 754 double precision
pi = 3.14159
sci = 1.5e10 # scientific notation -> 15000000000.0
small = 2.5e-4 # -> 0.00025
print(type(pi)) # <class 'float'>
# Division always returns float
print(10 / 3) # 3.3333333333333335
print(10 / 2) # 5.0 (float, not 5)
# Floor division returns int (or float if either operand is float)
print(10 // 3) # 3
print(10.0 // 3) # 3.0
# Float precision issue
print(0.1 + 0.2) # 0.30000000000000004
import math
print(math.isclose(0.1 + 0.2, 0.3)) # True - proper float comparison
# complex - real + imaginary
c = 2 + 3j
print(c.real) # 2.0
print(c.imag) # 3.0
Strings (str)
Strings are sequences of Unicode characters. They are immutable - you cannot change a character in place. A full lesson covers strings in depth.
# Single or double quotes - equivalent
a = 'hello'
b = "world"
# Triple quotes - multiline strings
multi = """Line one
Line two
Line three"""
# f-strings - format values inline (Python 3.6+)
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}") # Name: Alice, Age: 30
print(f"2 + 2 = {2 + 2}") # 2 + 2 = 4
print(f"{name.upper()!r}") # 'ALICE' (with repr)
print(f"{3.14159:.2f}") # 3.14 (format spec)
# Raw strings - backslashes not treated as escape sequences
path = r"C:\Users\Alice\Documents" # \ is literal
print(path) # C:\Users\Alice\Documents
# Escape sequences (in regular strings)
print("Line 1\nLine 2") # \n = newline
print("Tab\there") # \t = tab
print("Quote: \"hi\"") # \" = literal quote
Booleans (bool)
Python booleans are True and False (capitalized). Booleans are a subclass of int - True == 1 and False == 0.
active = True
disabled = False
print(type(True)) # <class 'bool'>
print(True + True) # 2 (bool is a subclass of int)
print(True * 5) # 5
# Truthiness - values that evaluate to False in boolean context
# Falsy: False, None, 0, 0.0, "", [], {}, (), set()
# Everything else is truthy
print(bool(0)) # False
print(bool("")) # False
print(bool([])) # False
print(bool(None)) # False
print(bool(1)) # True
print(bool("hi")) # True
print(bool([0])) # True (non-empty list, even if content is falsy)
# Using truthiness in if statements
items = []
if items:
print("has items")
else:
print("empty list") # this runs
name = "Alice"
if name:
print(f"Name is {name}") # runs - non-empty string is truthy
None
None is Python's null value - it represents the absence of a value. It is the only instance of the NoneType class.
result = None
print(result) # None
print(type(result)) # <class 'NoneType'>
# Functions without a return statement return None
def do_nothing():
pass
print(do_nothing()) # None
# Check for None with 'is' (not ==)
if result is None:
print("No result yet")
if result is not None:
print("We have a result")
# Common pattern: None as default + overwrite
def greet(name=None):
if name is None:
name = "World"
return f"Hello, {name}!"
print(greet()) # Hello, World!
print(greet("Alice")) # Hello, Alice!
# None vs False vs 0 - all falsy but different
print(None == False) # False
print(None == 0) # False
print(None is None) # True
type() and isinstance()
# type() - returns the exact type
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
print(type([])) # <class 'list'>
# Compare type
if type(x) == int: # works but not recommended
pass
# isinstance() - preferred; handles inheritance
print(isinstance(42, int)) # True
print(isinstance(42, float)) # False
print(isinstance(True, int)) # True! (bool is subclass of int)
print(isinstance(True, bool)) # True
print(isinstance(3.14, (int, float)))# True - check against tuple of types
class Animal: pass
class Dog(Animal): pass
d = Dog()
print(isinstance(d, Dog)) # True
print(isinstance(d, Animal)) # True (inheritance!)
print(type(d) == Animal) # False (type() doesn't check inheritance)
Type Casting
# int() - convert to integer
print(int("42")) # 42
print(int(3.9)) # 3 (truncates, does NOT round)
print(int(True)) # 1
print(int(False)) # 0
# int("3.14") # ValueError - can't convert "3.14" directly
print(int(float("3.14"))) # 3 (two-step conversion)
# float() - convert to float
print(float("3.14")) # 3.14
print(float(42)) # 42.0
print(float("inf")) # inf (positive infinity)
# str() - convert to string
print(str(42)) # '42'
print(str(3.14)) # '3.14'
print(str(True)) # 'True'
print(str(None)) # 'None'
# bool() - convert to boolean
print(bool(1)) # True
print(bool(0)) # False
print(bool("text")) # True
print(bool("")) # False
# Handling ValueError from bad input
user_input = "not a number"
try:
value = int(user_input)
except ValueError:
print(f"Cannot convert '{user_input}' to int")
value = 0
Mutable vs Immutable
| Immutable (cannot change in place) | Mutable (can change in place) |
|---|---|
| int, float, complex | list |
| bool | dict |
| str | set |
| tuple | bytearray |
| frozenset | user-defined classes (usually) |
# Immutable - "changing" creates a new object
s = "hello"
s += " world" # creates new string, rebinds s
# s[0] = "H" # TypeError: 'str' object does not support item assignment
# Mutable - changes happen in place
my_list = [1, 2, 3]
my_list.append(4) # modifies the same list object
my_list[0] = 99 # modifies in place
print(my_list) # [99, 2, 3, 4]
# This matters when passing to functions
def add_item(lst, item):
lst.append(item) # modifies the original list!
numbers = [1, 2, 3]
add_item(numbers, 4)
print(numbers) # [1, 2, 3, 4] - function modified the original
# To avoid modifying the original, pass a copy
add_item(numbers.copy(), 5)
print(numbers) # [1, 2, 3, 4] - unchanged
Built-in Types Reference
| Type | Keyword | Example | Mutable? | Notes |
|---|---|---|---|---|
| Integer | int | 42 | No | Unlimited precision |
| Float | float | 3.14 | No | 64-bit IEEE 754 |
| Complex | complex | 2+3j | No | Real + imaginary |
| String | str | "hello" | No | Unicode sequence |
| Boolean | bool | True | No | Subclass of int |
| None | NoneType | None | No | Null value singleton |
| List | list | [1, 2, 3] | Yes | Ordered, indexed |
| Tuple | tuple | (1, 2, 3) | No | Ordered, fixed |
| Dictionary | dict | {"a": 1} | Yes | Key-value pairs |
| Set | set | {1, 2, 3} | Yes | Unique unordered |