🟢 Beginner  ·  Lesson 04

Python Syntax, Indentation and Comments

Python Syntax, Indentation और Comments

Python Syntax Basics

Syntax means the grammar rules of a language. Python's syntax is famously clean: no semicolons, no curly braces for blocks. Instead, Python uses indentation (spaces at the start of a line) to group code.

Indentation — The Big Rule

In most languages { } mark a block. In Python, the indentation itself defines the block. Lines indented by the same amount belong together.

⚠️ Important

Use 4 spaces per indentation level. Never mix tabs and spaces — it causes an IndentationError.

Comments

Comments are notes for humans; Python ignores them while running.

  • Single-line: start with #
  • Multi-line: wrap text in triple quotes """ ... """

Program 1: Indentation in Action

Python – indent.py
marks = 75

if marks >= 33:
    print("Pass")          # indented = inside the if
    print("Well done!")    # same block
print("Program over")      # not indented = outside the if
Pass Well done! Program over
  • The two indented lines run only when the condition is true.
  • print("Program over") is not indented, so it always runs.

Program 2: Using Comments

Python – comments.py
# This program calculates the area of a rectangle
length = 10      # length in cm
width = 5        # width in cm

area = length * width   # formula: l x w
print("Area =", area, "sq cm")
Area = 50 sq cm

The # notes explain the code without affecting output. Good comments explain why, not just what.

Program 3: Multi-line Strings as Notes

Python – docstring.py
"""
Program: Greeting
Author : Student
Purpose: Print a welcome message
"""
print("Welcome to CodeKaFunda")
Welcome to CodeKaFunda

Triple-quoted text at the top is often used to describe a program or function (called a docstring).

Common Mistakes

  • Inconsistent indentation (2 spaces here, 4 there).
  • Mixing tabs and spaces.
  • Forgetting the colon : before an indented block.

Practice Tasks

  1. Write an if-block with two indented lines and one line outside it.
  2. Add a comment above every line of a small program.
  3. Add a 3-line docstring at the top of a program.

Summary

  • Python uses indentation (4 spaces) instead of braces to group code.
  • # makes a single-line comment; """ """ makes a multi-line one.
  • A block always follows a colon :.

Python Syntax की मूल बातें

Syntax यानी language के grammar नियम। Python की syntax मशहूर रूप से साफ है: न semicolons, न blocks के लिए curly braces। इसके बजाय Python code को group करने के लिए indentation (line की शुरुआत में spaces) use करता है।

Indentation — सबसे बड़ा नियम

ज़्यादातर languages में { } block बनाते हैं। Python में indentation खुद block तय करता है। समान मात्रा में indent की गई lines एक साथ होती हैं।

⚠️ ज़रूरी

हर indentation level के लिए 4 spaces use करें। tabs और spaces कभी न मिलाएं — IndentationError आता है।

Comments

Comments इंसानों के लिए notes हैं; Python चलाते समय इन्हें ignore करता है।

  • Single-line: # से शुरू
  • Multi-line: text को triple quotes """ ... """ में लपेटें

Program 1: Indentation काम में

Python – indent.py
marks = 75

if marks >= 33:
    print("Pass")          # indented = if के अंदर
    print("Well done!")    # same block
print("Program over")      # बिना indent = if के बाहर
Pass Well done! Program over
  • दोनों indented lines तभी चलती हैं जब condition true हो।
  • print("Program over") indent नहीं है, इसलिए हमेशा चलती है।

Program 2: Comments का Use

Python – comments.py
# यह program rectangle का area निकालता है
length = 10      # length cm में
width = 5        # width cm में

area = length * width   # formula: l x w
print("Area =", area, "sq cm")
Area = 50 sq cm

# notes code समझाते हैं बिना output बदले। अच्छे comments क्यों बताते हैं, सिर्फ क्या नहीं।

Program 3: Multi-line Strings notes के रूप में

Python – docstring.py
"""
Program: Greeting
Author : Student
Purpose: Print a welcome message
"""
print("Welcome to CodeKaFunda")
Welcome to CodeKaFunda

ऊपर triple-quoted text अक्सर program या function describe करने के लिए use होता है (इसे docstring कहते हैं)।

सामान्य गलतियाँ

  • असंगत indentation (कहीं 2 spaces, कहीं 4)।
  • tabs और spaces मिलाना।
  • indented block से पहले colon : भूलना।

Practice Tasks

  1. दो indented lines और एक बाहर वाली line के साथ if-block लिखें।
  2. छोटे program की हर line के ऊपर comment जोड़ें।
  3. Program के ऊपर 3-line docstring जोड़ें।

सारांश

  • Python braces की जगह indentation (4 spaces) से code group करता है।
  • # single-line comment; """ """ multi-line।
  • Block हमेशा colon : के बाद आता है।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp