Qendrim's Directory

Python Syntax

Python DSA

Python MySQL

Design Patterns

Solved Problems

Python SyntaxGet Started & Syntax

Get Started & Syntax

Python is a readable, high-level language. Unlike C++ or Java, it handles memory management for you and emphasizes code readability.

Indentation is Semantics

In many languages (JS, C++), indentation is just for style. In Python, **indentation is logic**. It defines the scope of loops, functions, and classes. If you mess up whitespace, your code crashes (`IndentationError`).

python
if 5 > 2:
    print("Inside the if-block") # Indented
print("Outside the if-block")    # Unindented

Pro Tip

Standard convention is 4 spaces. Do not mix tabs and spaces. In interviews, consistency matters more than width.

Comments & Docstrings

Comments (`#`) are ignored by the interpreter. **Docstrings** (`"""`) are retained at runtime and are used for documentation generation. Use docstrings for functions/classes to explain *what* they do (Input/Output), and comments to explain *why* complex logic exists.

python
def add(a, b):
    """
    Adds two numbers and returns the result.
    Input: int/float
    Output: int/float
    """
    # We don't validate types here for performance reasons
    return a + b

The 'Pass' Keyword

Python requires blocks (like `if`, `def`, `class`) to not be empty. If you are sketching out code or defining an abstract interface, use `pass` as a placeholder.

python
def implementation_later():
    pass # Prevents SyntaxError

Knowledge Check

Question 1 of 5Score: 0

What happens if you skip indentation in a loop?