C#
PY

C# to Python

10 lessons

Progress0%
1Introduction2Type Systems3Functions4Collections5Object-Oriented Programming6LINQ to Comprehensions7Async Programming8Ecosystem9Decorators and Metaprogramming10Error Handling
All Mirror Courses
C#
PY
Type Systems
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
C#
From C#:

In C#, you're familiar with type systems.

PY
In Python:

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:

PY
Python 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#:

C#
C# (What you know)
int age = 30;
double price = 9.99;
string name = "Alice";
bool active = true;

// Type is fixed at compile time
age = "hello"; // compile error
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Python is dynamically typed — no type declarations needed

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

int/float/str/bool are Python's equivalents of int/double/string/bool

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

bool values are True/False (capital) in Python

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Python type hints are optional annotations, not enforced at runtime

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

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.

C#
C#
int x = 42;
string s = "hello";
// x = "hello"; — compile error
PY
Python
x = 42
s = "hello"
x = "hello"  # valid — but avoid in practice

# type() lets you inspect at runtime
print(type(x))  # <class 'str'>
Common Pitfall
Dynamic typing shifts errors from compile time to runtime. Type hints + mypy bring back static checking without changing runtime behavior.

2. Type Name Differences

The core types have slightly different names. int is the same; string becomes str; bool values are capitalized.

C#
C#
int count = 0;
double pi = 3.14;
string msg = "hi";
bool flag = true;
PY
Python
count = 0      # int
pi = 3.14      # float (Python has no 'double')
msg = "hi"     # str
flag = True    # bool — capital T
Common Pitfall
Python's bool is a subclass of int. True == 1 and False == 0, which can cause surprising behavior in arithmetic.

3. Type Hints

Python 3.5+ supports optional type hints. They are documentation and tooling aids — not enforced at runtime. Use mypy for static checking.

C#
C#
int Add(int a, int b) => a + b;
string Greet(string name) => $"Hello {name}";
PY
Python
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 analysis
Rule of Thumb
Always add type hints to public function signatures. They serve as inline documentation and enable IDE autocompletion.

4. None vs null

Python's None is the equivalent of C# null. Checking for None uses 'is None' (identity), not == (equality).

C#
C#
string? name = null;
if (name == null) {
    Console.WriteLine("empty");
}
PY
Python
name = None
if name is None:
    print("empty")

# Optional hint
from typing import Optional
def process(name: Optional[str]) -> None:
    if name is None:
        return
Common Pitfall
Use 'is None' not '== None'. Some objects override __eq__ and '== None' can return unexpected results.

Common 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
Common Pitfall
Don't assume Python works exactly like C#. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C# code in Python to practice these concepts.
PreviousNext