C#
PY

C# to Python

10 lessons

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

Functions

Functions

Introduction

In this lesson, you'll learn about functions 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 functions.

PY
In Python:

Python has its own approach to functions, 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
# Standalone functions — no class needed
def add(a, b):
    return a + b

def greet(name="World"):
    return f"Hello, {name}!"

# Lambda (single expression only)
square = lambda x: x * x

Comparing to C#

Here's how you might have written similar code in C#:

C#
C# (What you know)
// C# methods must live inside a class
static int Add(int a, int b) => a + b;

static string Greet(string name = "World") {
    return $"Hello, {name}!";
}

// Lambda
Func<int, int> square = x => x * x;
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Python functions are standalone — no containing class required

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

def keyword replaces the return type + method name syntax

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Default parameter values work the same way

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Python lambda is limited to a single expression; use def for multi-line

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

No return type annotation needed (but recommended with type hints)

Step-by-Step Breakdown

1. Defining Functions

Python uses def followed by the function name and parameters. No return type, no access modifier, no static keyword.

C#
C#
public static int Multiply(int a, int b) {
    return a * b;
}
PY
Python
def multiply(a, b):
    return a * b

# With type hints (recommended):
def multiply(a: int, b: int) -> int:
    return a * b

2. Default and Keyword Arguments

Python default parameters work like C# default values. Python also supports calling by keyword name, which C# only offers via named arguments.

C#
C#
static void Connect(string host, int port = 8080) { }
Connect("localhost");
Connect("localhost", port: 3000); // named
PY
Python
def connect(host, port=8080):
    pass

connect("localhost")
connect("localhost", port=3000)  # keyword arg
connect(port=3000, host="localhost")  # any order!
Rule of Thumb
Use keyword arguments in Python to make call sites self-documenting, especially for functions with many parameters.

3. Variable Arguments

*args collects positional arguments into a tuple (like params in C#). **kwargs collects keyword arguments into a dict.

C#
C#
static int Sum(params int[] nums) =>
    nums.Sum();
PY
Python
def sum_all(*args):       # tuple of positional args
    return sum(args)

def config(**kwargs):      # dict of keyword args
    for k, v in kwargs.items():
        print(f"{k}={v}")

sum_all(1, 2, 3, 4)
config(host="localhost", port=8080)

4. First-Class Functions

Python functions are objects. You can pass them, store them in variables, and return them — same as C# delegates and Func<T>.

C#
C#
Func<int, int> transform = x => x * 2;
List<int> result = list.Select(transform).ToList();
PY
Python
transform = lambda x: x * 2
result = list(map(transform, my_list))

# Or pass a named function
def double(x):
    return x * 2

result = list(map(double, my_list))

Common Mistakes

When coming from C#, developers often make these mistakes:

  • Python functions are standalone — no containing class required
  • def keyword replaces the return type + method name syntax
  • Default parameter values work the same way
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

  • def replaces the return-type + method-name syntax
  • Functions are standalone — no class required
  • Default and keyword arguments work like C# defaults and named args
  • *args and **kwargs handle variable argument lists
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