Quick reference guide for your first Python concepts. Use this as a cheat sheet when working on problems or projects.
Python is a beginner-friendly programming language. You can run Python interactively (REPL) or write scripts (.py files). The print() function displays output, and comments help document your code.
What it is:
Python can be run in two ways: interactively in the REPL or by executing script files.
Syntax:
# Start interactive Python
python3
# Run a script
python3 filename.pyWhen to use:
- REPL: Quick tests, calculations, experiments
- Scripts: Code you want to save and run repeatedly
What it is:
A built-in function that displays output to the screen.
Syntax:
print(value)
print(value1, value2, ...)Common use cases:
# Print text
print("Hello, World!")
# Print numbers
print(42)
# Print multiple values
print("The answer is:", 42)
# Customize separator and ending
print("A", "B", "C", sep="-") # A-B-C
print("Hello", end=" ")
print("World") # Hello WorldWhen to use:
- Display results to the user
- Debug your code (see what's happening)
- Create output for your programs
What they are:
Notes in your code that Python ignores.
Syntax:
# Single-line comment
"""
Multi-line comment
Can span multiple lines
"""Common use cases:
# Explain what code does
print("Hello") # Inline comment
# Temporarily disable code
# print("This won't run")
"""
Document larger sections
Explain complex logic
Leave notes for future reference
"""When to use:
- Explain why (not just what) your code does something
- Document complex logic
- Leave TODO notes
- Temporarily disable code while testing
# Simple message
print("Welcome to Python!")# Multiple related messages
print("Name: Alice")
print("Age: 25")
print("City: New York")# Aligned output with tabs
print("Item\t\tPrice")
print("Coffee\t\t$3.50")
print("Muffin\t\t$2.75")| Pitfall | Example | Why It's Wrong | Correct Approach |
|---|---|---|---|
| Missing quotes | print(Hello) |
Python looks for a variable | print("Hello") |
| Mismatched quotes | print("Hello') |
Quotes don't match | print("Hello") or print('Hello') |
| Forgetting parentheses | print "Hello" |
Old Python 2 syntax | print("Hello") |
| Wrong command | Using python instead of python3 |
Might use Python 2 | Always use python3 |
-
Use Python 3
- Always use
python3command - Python 2 is outdated
- Always use
-
Add comments
- Explain what your code does
- Your future self will thank you
-
Test frequently
- Run your code often
- Catch errors early
| Character | Effect | Example | Output |
|---|---|---|---|
\n |
New line | print("Hi\nBye") |
Hi Bye |
\t |
Tab | print("Name:\tJohn") |
Name: John |
\" |
Quote mark | print("Say \"Hi\"") |
Say "Hi" |
\\ |
Backslash | print("C:\\Users") |
C:\Users |
| Parameter | Purpose | Example |
|---|---|---|
| (default) | Values to print | print("Hello", 42) |
sep |
Separator between values | print("A", "B", sep="-") → A-B |
end |
What to print at end | print("Hi", end="!") → Hi! |
Use this checklist when writing your first programs:
- Python 3.8+ is installed (
python3 --version) - File ends with
.pyextension - Text is enclosed in quotes
-
print()has parentheses - Quotes match (both single or both double)
- Code runs without errors
- Variables and Data Types - Storing information
- Control Flow - Making decisions in code
💡 Pro Tip: Run your code frequently. Don't write 20 lines and then test—write 2-3 lines, test, then continue. This makes debugging much easier!
🎓 Remember: Everyone starts here. The print() function might seem simple, but it's one of the most used functions in Python!