python-backend
/

Python Syntax – The Rules of the Road

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Python Syntax – The Rules of the Road

The Philosophy of Python Syntax

Python's syntax is famously clean and minimalist. Unlike languages that use curly braces {} or semicolons ; to define structure, Python relies on indentation. This isn't just a style choice—it is a requirement of the language that enforces readable code.

  1. Indentation is Mandatory

In Python, a block of code (like the body of a loop or function) starts with a colon : and must be indented. While you can use any number of spaces, the standard (PEP 8) is 4 spaces per level.

PythonRead-only
1
def check_status(is_active):
    if is_active:
        print("System is Online") # Indented 8 spaces
    else:
        print("System is Offline") # Indented 8 spaces
    print("Check complete.") # Indented 4 spaces (back in function scope)

  1. Comments & Docstrings

Comments are essential for documenting logic. Python supports single-line comments and multi-line documentation strings (docstrings).

  • Single-line: Use the hash # symbol.
  • Multi-line / Docstrings: Use triple quotes """ or '''. These are often used at the start of functions to explain their purpose.
PythonRead-only
1
# This is a single-line comment

def calculate_roi(investment, return_val):
    """
    Calculates the Return on Investment.
    Args: investment (int), return_val (int)
    """
    return (return_val - investment) / investment

  1. Statement Termination

In Python, the end of a line marks the end of a statement. You don't need semicolons. If a statement is very long, you can use a backslash \ to continue it on the next line, though surrounding the code in parentheses () is the preferred way.

Syntax Comparison

FeatureDart / JavaScriptPython
Code BlocksCurly braces { }Colon : and Indentation
Statement EndSemicolon ;New line
Main Functionvoid main() { }if __name__ == "__main__":
Comments// or /* */# or """ """

Try it yourself

# Fix the indentation to make this code work!
def welcome_message(name):
if name == "Kishore":
print("Welcome back, Lead!")
else:
print(f"Hello, {name}!")

welcome_message("Kishore")

Test Your Knowledge

Q1
of 3

How does Python define a block of code?

A
Using curly braces {}
B
Using parentheses ()
C
Using colons and indentation
D
Using the 'begin' and 'end' keywords
Q2
of 3

What is the PEP 8 recommended number of spaces for indentation?

A
2 spaces
B
4 spaces
C
8 spaces
D
1 tab
Q3
of 3

Which character is used to continue a statement onto a new line?

A
/
B
\
C
&
D
|

Frequently Asked Questions

What happens if I mix tabs and spaces?

Python 3 does not allow mixing tabs and spaces for indentation. Doing so will result in an 'IndentationError'. It is best to set your editor to convert tabs to 4 spaces.

Are semicolons allowed at all?

Technically, yes. You can use a semicolon to put multiple statements on one line (e.g., x=5; y=10), but this is highly discouraged as it violates Python's readability standards.

What is PEP 8?

PEP 8 is the official Style Guide for Python Code. it provides conventions for naming, spacing, and overall code structure.

Previous

python setup

Next

python variables

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.