In Python, the way you write code is just as important as the code itself. Unlike C, which uses semicolons and curly braces, Python uses indentation and newlines to understand the structure of your program.
1. Indentation (The Golden Rule)
In most languages, indentation is for aesthetics. In Python, it is mandatory. A block of code (like a loop or a function) starts with a colon : and every line within that block must be indented by the same number of spaces (usually 4).
2. Comments
Comments are notes for humans. The Python interpreter completely ignores them.
Single-line: Use the
#symbol.Multi-line: Use triple quotes
"""or'''. (These are technically strings, but when left alone, they act as comments).
3. Case Sensitivity
Python is case-sensitive. myVariable, myvariable, and MYVARIABLE are three completely different things.
# 1. This is a single-line comment
# Variables are assigned using the '=' operator
name = "Gemini"
"""
2. This is a multi-line comment.
The following code demonstrates a basic 'if' block.
Note the colon and the indentation.
"""
if name == "Gemini":
print("Welcome, Gemini!") # Indented by 4 spaces
print("Indentation tells Python this line is inside the 'if'.")
print("This line is NOT indented, so it runs after the 'if' block.")