Posts

Showing posts from 2026

Python Indentation

  🧠 Python Indentation (very important in Python ) In Python, indentation is not optional —it defines the structure of your code (instead of {} like in Java/C++).  What is indentation? Indentation means spaces at the beginning of a line . Python uses indentation to define blocks of code (like inside if , for , functions, etc.). 📏 Rules of indentation Use  4 spaces  (standard practice) Be consistent (don’t mix tabs and spaces) All lines in same block must have same indentation 🧠 Simple understanding Tab = shortcut key for indentation Spaces = actual characters (recommended in Python) 🔹 Basic Example if True:     print("Hello")   # indented → inside if block print("Outside")     # not indented → outside block ❌ Wrong (will give error) if True: print("Hello")   # ❌ no indentation 👉 Error: IndentationError 🔹 Example with loop for i in range(3):     print(i)     print("Loop running") print("...

Comments in Python

  📝 What are comments in Python ? Comments are lines in code that are ignored by the interpreter . They are used to explain code for humans.  1. Single-line comments Use # # This is a comment print("Hello")  # This is also a comment ✔ Used for short explanations ✔ Most common type 2. Multi-line comments (not official, but used) Python doesn’t have real multi-line comment syntax like other languages. Method 1: Multiple # # This is line 1 # This is line 2 # This is line 3 Method 2: Triple quotes (doc-style, not true comments) """ This looks like a comment but actually it's a string """ ⚠ This works only if not assigned to a variable (otherwise it's a string). 3. Docstrings (special comments for documentation) Used inside functions, classes, modules: def add(a, b):     """This function returns sum of two numbers"""     return a + b