
đ Lesson 3: Python Syntax
1. What is syntax?
Short answer â Syntax refers to the set of rules for writing Python code.
In other words â Syntax is the set of rules that defines how Python code must be written.
2. Indentation (using spaces/tabs)
Instead of using {}, Python uses space/tab indentation to define a block.
â ď¸ Get the indentation wrong and you'll hit an IndentationError.
3. Key Python Syntax Rules
| Rule | áážááşá¸áááşá¸ááťááş |
|---|---|
| Case-sensitive | Variable ááŹáááşáá˝áąáážáŹ áĄááźáŽá¸/áĄááąá¸ áá˝á˛ááŹá¸áááş (name â Name) |
| Indentation | Code block áá˝áąááᯠspace/tab áá˛áˇ áá˝á˛áááş |
| Line Continuation | \ ááŻáśá¸ááźáŽá¸ ááźáąáŹááşá¸áááşááąá¸áááŻááşáááş |
| Comments | # áá˛áˇ á áá˛áˇ á áŹááźáąáŹááşá¸ â Python ááááşáᲠáážááşááťááşáĄááąáá˛áˇááŹá¸áááş |
4. Summary
â Python syntax = indentation matters
â It's case-sensitive
â Write one statement per line
â
Write comments with #
python
# ===== 1. áážááşáááşááąáŹ Indentation =====
if 5 > 2:
print("Five is greater than two!") # â
Correct
# ===== 2. Case-sensitive Example =====
name = "Sai"
Name = "Aye"
print(name) # Sai
print(Name) # Aye
# ===== 3. Line Continuation =====
total = 1 + 2 + 3 + \
4 + 5 + 6
print(f"Total: {total}")
# ===== 4. Multiple statements in one line =====
x = 5; y = 10; print(f"x + y = {x + y}")You should see
Five is greater than two! Sai Aye Total: 21 x + y = 15