Lesson 1 of 5

Syntax & Structure

The single biggest culture shock moving from C# to Python: the braces are gone, and they're not coming back. In C#, curly braces { } mark where a block starts and ends — indentation is just a style convention for humans, the compiler doesn't care. In Python, indentation is the syntax — there is no other way to mark a block.

💡 The Bottom Line

Wherever C# uses { } to group statements, Python uses a colon : followed by a consistently indented block. No semicolons, no braces, no parentheses required around conditions. The visual structure of the code is its actual structure — which is exactly why sloppy indentation in Python is a syntax error, not just a style complaint.

The same "if" logic, side by side

C#
if (age >= 18)
{
    Console.WriteLine("Adult");
}
else
{
    Console.WriteLine("Minor");
}
Python
if age >= 18:
    print("Adult")
else:
    print("Minor")

📋 Key differences

  • No parentheses required around the condition (if age >= 18:, not if (age >= 18): — though the latter still works, it's not idiomatic).
  • No braces — the colon opens the block, indentation defines what's inside it.
  • No semicolons at the end of statements.
  • Console.WriteLine(...) becomes the built-in print(...) function.

⚠️ Gotcha

Mixing tabs and spaces in the same Python file will cause an IndentationError or, worse, silently different behavior between editors. Pick spaces (4 is the convention — see PEP 8) and configure your editor to insert spaces when you press Tab. This single habit prevents the most common "why won't this run" moment for C# developers new to Python.

Functions and methods: no explicit return type, and no braces here either

C#
public int Square(int n)
{
    return n * n;
}
Python
def square(n):
    return n * n

C# methods declare an access modifier (public), a return type (int), and parameter types. Python function definitions start with def, skip the return type entirely (Python figures it out at runtime), and — unless you opt into type hints, which is a later lesson — skip parameter types too. There's also no access-modifier keyword; Python signals "this is meant to be private" with a leading underscore (_helper) by convention, not by enforcement.

🔧 Try It Yourself

Take a small C# method you've written — anything with an if/else and a loop — and rewrite it in Python using only indentation, colons, and def. Run it (any online Python interpreter works) and deliberately misalign one indented line to see the IndentationError Python throws — that error is your new "the compiler caught a typo" signal, replacing what a missing brace would have caught in C#.

🙋 There Are No Dumb Questions

Q: Can I just add braces anyway, out of habit, and ignore indentation?
A: No — Python doesn't recognize { } as block delimiters at all; they're not part of its statement syntax. Writing them will either cause a syntax error or (if used as a dictionary/set literal in a different context) mean something completely different. There's no opt-out: indentation is the only way to express a block in Python.

File structure: no mandatory class wrapper

Every C# file's code lives inside a class (and usually a namespace) — even a single Main method needs a class to sit in. Python has no such requirement: top-level functions and even loose statements are perfectly valid in a .py file. A "Hello World" Python script can be one line: print("Hello, World!") — no class, no Main, no namespace.

✅ Quick Check

A C# developer copies an if block into Python but forgets to indent the body consistently. What happens?


Next up: syntax is only the surface. Underneath, C#'s static, compiled type system and Python's dynamic one behave very differently — that's where the next lesson picks up.

← Back to
Next →