# Syntax and Indentation

Python is a great programming language for beginners because it’s easy to understand, but there’s one important thing you need to learn before you dive into it: <mark style="color:yellow;">**indentation**</mark>. In Python, how you organize your code with spaces matters — a lot. So let’s break it down in a simple way, and also take a look at how Python compares to C when it comes to indentation.

## What is Indentation in Python?

In Python, indentation refers to the spaces you put at the beginning of a line of code to indicate that it’s part of a specific block. Unlike many programming languages that use curly braces `{}` to mark blocks of code (like C), **Python relies on indentation** to define blocks. So, indentation is not just for making your code look pretty; it’s a key part of Python’s syntax.

For example, let's say you want to check if a number is positive or negative:

```python
x = 5
if x > 0:
    print("x is positive")
```

Notice the indentation before the `print("x is positive")` line. The indentation tells Python that this `print()` statement belongs to the `if` block, meaning it will only run if `x > 0`.

## Why is Indentation Important?

If you forget to indent your code properly, Python will raise an error. Unlike C, which uses curly braces `{}` to mark code blocks, Python uses indentation to define the structure of your program.

Here’s an example of **incorrect indentation**:

```python
x = 5
if x > 0:
print("x is positive")  # This will raise an IndentationError
```

Python will give you an **IndentationError** because it doesn’t know whether the `print()` statement belongs inside the `if` block or not.

## Python vs C: How They Handle Code Blocks

Let’s compare how Python and C handle code blocks to understand why indentation matters so much in Python.

### **C Code (Using Curly Braces)**

In C, you use curly braces `{}` to define the start and end of code blocks. For example:

```c
#include <stdio.h>

int main() {
    int x = 5;
    if (x > 0) {
        printf("x is positive\n");
    } else {
        printf("x is not positive\n");
    }
    return 0;
}
```

In C, you can use any amount of indentation (though we usually use 4 spaces), and it doesn’t affect how the code runs. The **curly braces `{}`** are what matter for defining code blocks. The indentation is only for readability.

**Python Code (Using Indentation)**

In Python, curly braces are **not used**. Instead, indentation is used to define blocks of code. Here’s the same example written in Python:

```python
x = 5
if x > 0:
    print("x is positive")
else:
    print("x is not positive")
```

Notice how the code is indented after the `if` and `else` statements. Python uses this indentation to understand which code belongs to which block.

{% hint style="info" %}
**Why Python Does This:** Python’s way of using indentation instead of curly braces might seem a little strange at first, but it actually makes code more readable. With proper indentation, you don’t have to worry about missing curly braces or getting them in the wrong place.
{% endhint %}

## Indentation Rules in Python

Here are some rules to follow when writing Python code:

1. **Consistency is Key**: If you start with spaces for indentation, stick to spaces. If you start with tabs, use tabs. Mixing spaces and tabs is a no-no in Python.
2. **Indent by 4 Spaces**: The Python community uses 4 spaces per indentation level, and this is the standard you should follow.
3. **Indentation Defines Structure**: In Python, indentation actually **defines** the structure of your program. Without proper indentation, your code will either raise errors or not run the way you expect.

## Example: Indentation in Action

Let’s look at an example with a function in Python. A function is a block of code that performs a specific task. Here's how you would define a function in Python:

```python
def greet(name):
    print(f"Hello, {name}!")
    print("Welcome to Nepal.")

greet("User")
```

The two `print()` statements are indented under the `greet()` function. This tells Python that these lines of code are part of the function. Without the indentation, Python wouldn’t know which code belongs inside the function.

If you removed the indentation, you’d get an error:

```python
def greet(name):
print(f"Hello, {name}!")  # This will raise an IndentationError
```

## Indentation Tips

* **Use a Text Editor or IDE**: Many code editors like VSCode, PyCharm, or even Sublime Text can help you by automatically indenting your code as you type. This helps avoid indentation errors and keeps your code neat.
* **Don’t Mix Tabs and Spaces**: This is a common pitfall. Make sure you use either spaces **or** tabs, but not both. Most Python developers use 4 spaces per indentation level, so stick to that.

## The Bottom Line

In Python, **indentation is everything**. It tells the interpreter how to structure your code. If you’re coming from a language like C, it can feel a bit odd at first, but once you get used to it, you’ll appreciate how much clearer and cleaner Python code is.

So, remember:

* **Indentation = structure**
* Python uses indentation instead of curly braces to mark code blocks
* Consistent indentation is key to avoiding errors


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.ncateam.xyz/fundamentals/python/basics-of-python/syntax-and-indentation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
