Python Syntax
2 min read ·
Python syntax is simple, clean, and easy to read.
Unlike many programming languages, Python relies on indentation and readable structure instead of brackets or semicolons.
Python Indentation
Indentation in Python is mandatory.
It defines blocks of code such as loops, conditions, and functions.
If indentation is incorrect, Python raises an error.
Important
Python uses spaces or tabs for indentation, but spaces are recommended.
Code Example 1: Correct Indentation
Code Example 2: Indentation in Loop
Code Example 3: Indentation Error Example
Python Variables
Variables are used to store data values in Python.
Python creates variables automatically when you assign a value.
Python variables:
- Do not need type declaration
- Can change type dynamically
Key Rule
A variable name must start with a letter or underscore.
Code Example 1: Creating Variables
Code Example 2: Dynamic Typing
Code Example 3: Multiple Assignments
Python Comments
Comments are used to explain code and make it more readable.
Python ignores comments during execution.
Python supports:
- Single-line comments
- Multi-line comments (using triple quotes)
Best Practice
Write comments to explain why the code exists, not what it does.