First Python Program
Python, Hello World
On this page
The traditional first program in any programming language
# Your first Python program
print("Hello, World!")
Output
Hello, World!
Key Points
print()
is a built-in function- Text must be in quotes (“” or ”)
- Case-sensitive
Python Syntax
Python syntax refers to the set of rules that define how Python programs are written and structured.
Indentation
Python uses indentation (whitespace) to define code blocks instead of curly braces:
# Correct indentation
if 5 > 2:
print("Five is greater than two!") # 4 spaces indent
# Incorrect indentation
if 5 > 2:
print("This will cause an error!")
Important: Maintain consistent indentation (4 spaces recommended by PEP 8)
Case Sensitivity
Python is case-sensitive:
Variable
, variable
, and VARIABLE
are different
Line Endings
Statements end with newlines (no semicolons needed):
# Correct indentation
if 5 > 2:# Preferred
print("Hello")
print("World")
# Also valid but not recommended
print("Hello"); print("World")
Comments
Comments are explanatory notes ignored by the Python interpreter
Single-line Comments
Start with #
symbol:
# This is a single-line comment
print("Hello") # This comment is after code
Multi-line Comments
Use triple quotes (”’ or “””) or multiple # symbols:
'''
This is a multi-line
comment using triple quotes
'''
# Alternative using multiple #
# This is a long comment
# spanning multiple lines
Comment Uses
Best Practices
- Explain complex logic
- Document function purposes
- Temporarily disable code
Avoid
- Obvious statements
- Redundant explanations
- Outdated comments
Pro Tip: Use comments to write documentation strings (docstrings) for functions and classes