C#
PY

C# to Python

10 lessons

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

Introduction

Introduction

Introduction

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

PY
In Python:

Python has its own approach to introduction, 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
print("Hello, World!")

Comparing to C#

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

C#
C# (What you know)
using System;

class Hello {
    static void Main() {
        Console.WriteLine("Hello, World!");
    }
}
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

No class, no Main, no braces — Python runs top-level code directly

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Indentation (4 spaces) defines code blocks instead of curly braces

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

Python is interpreted — no compile step; run with 'python file.py'

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

No semicolons at line endings

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

PY
In Python:

print() replaces Console.WriteLine()

Step-by-Step Breakdown

1. No Entry Point Class

C# requires all code to live inside a class and a Main method. Python executes top-level statements directly. The 'if __name__ == __main__' guard is optional but conventional.

C#
C#
class Program {
    static void Main() {
        Console.WriteLine("Hello");
    }
}
PY
Python
# Top-level code runs directly
print("Hello")

# Conventional guard for reusable scripts:
if __name__ == "__main__":
    print("Running as script")

2. Indentation Instead of Braces

Python uses consistent indentation (PEP 8 recommends 4 spaces) to define blocks. Mixing tabs and spaces is a syntax error.

C#
C#
if (x > 0) {
    Console.WriteLine("positive");
    x++;
}
PY
Python
if x > 0:
    print("positive")
    x += 1
Common Pitfall
IndentationError is one of the most common Python errors for C# developers. Configure your editor to use spaces, not tabs.

3. print() vs Console.WriteLine

print() is a built-in function. It prints to stdout with a trailing newline by default. You can change the separator and end characters.

C#
C#
Console.WriteLine("Hello");
Console.Write("no newline");
Console.WriteLine($"x = {x}");
PY
Python
print("Hello")
print("no newline", end="")
print(f"x = {x}")  # f-string interpolation
Rule of Thumb
Use f-strings (f"...") for string interpolation — they are the Python equivalent of C# $"...".

4. Running Python Code

Python is interpreted — there is no compile step. Just run the file directly with the Python interpreter.

C#
C#
// C#: dotnet build && dotnet run
// or: csc Program.cs && Program.exe
PY
Python
# Python: just run it
# python hello.py
# python3 hello.py   (on some systems)

# Interactive REPL
# python
# >>> print("Hello")

Common Mistakes

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

  • No class, no Main, no braces — Python runs top-level code directly
  • Indentation (4 spaces) defines code blocks instead of curly braces
  • Python is interpreted — no compile step; run with 'python file.py'
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 class or Main required — top-level code runs directly
  • Indentation (4 spaces) replaces curly braces
  • print() replaces Console.WriteLine(); f-strings replace $"..."
  • No compile step — run with 'python file.py'
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C# code in Python to practice these concepts.
OverviewNext