Type Systems
Type Systems
Introduction
In this lesson, you'll learn about type systems in Python. Coming from C#, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C#, you're familiar with type systems.
Python has its own approach to type systems, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
age = 30
price = 9.99
name = "Alice"
active = True
# Variables can be rebound to any type
age = "hello" # valid at runtime (but bad practice)
# Optional type hints (Python 3.5+)
def greet(name: str) -> str:
return f"Hello, {name}"Comparing to C#
Here's how you might have written similar code in C#:
int age = 30;
double price = 9.99;
string name = "Alice";
bool active = true;
// Type is fixed at compile time
age = "hello"; // compile errorYou may be used to different syntax or behavior.
Python is dynamically typed — no type declarations needed
You may be used to different syntax or behavior.
int/float/str/bool are Python's equivalents of int/double/string/bool
You may be used to different syntax or behavior.
bool values are True/False (capital) in Python
You may be used to different syntax or behavior.
Python type hints are optional annotations, not enforced at runtime
You may be used to different syntax or behavior.
None is Python's equivalent of null
Step-by-Step Breakdown
1. Dynamic Typing
Python variables are untyped containers. The type belongs to the value, not the variable. This means no declarations — just assign.
int x = 42;
string s = "hello";
// x = "hello"; — compile errorx = 42
s = "hello"
x = "hello" # valid — but avoid in practice
# type() lets you inspect at runtime
print(type(x)) # <class 'str'>2. Type Name Differences
The core types have slightly different names. int is the same; string becomes str; bool values are capitalized.
int count = 0;
double pi = 3.14;
string msg = "hi";
bool flag = true;count = 0 # int
pi = 3.14 # float (Python has no 'double')
msg = "hi" # str
flag = True # bool — capital T3. Type Hints
Python 3.5+ supports optional type hints. They are documentation and tooling aids — not enforced at runtime. Use mypy for static checking.
int Add(int a, int b) => a + b;
string Greet(string name) => $"Hello {name}";def add(a: int, b: int) -> int:
return a + b
def greet(name: str) -> str:
return f"Hello {name}"
# Run: mypy script.py — for static analysis4. None vs null
Python's None is the equivalent of C# null. Checking for None uses 'is None' (identity), not == (equality).
string? name = null;
if (name == null) {
Console.WriteLine("empty");
}name = None
if name is None:
print("empty")
# Optional hint
from typing import Optional
def process(name: Optional[str]) -> None:
if name is None:
returnCommon Mistakes
When coming from C#, developers often make these mistakes:
- Python is dynamically typed — no type declarations needed
- int/float/str/bool are Python's equivalents of int/double/string/bool
- bool values are True/False (capital) in Python
Key Takeaways
- No type declarations — Python infers type from the assigned value
- string → str, bool → bool (True/False), double → float
- None replaces null; check with 'is None'
- Add type hints for tooling support; enforce with mypy