Basic Syntax and Your First Programs
You've created variables. Now you'll write full programs—code that does something and shows results.
In this lesson, you'll learn the syntax that makes Python unique: indentation, comments, how to print output, and how to format text. Most importantly, you'll learn the most important Python skill: saving code to a file and running it.
Indentation: Python's Unique Syntax
Python is unusual among programming languages in one fundamental way: it uses indentation (spaces) to show code structure.
Unlike languages like JavaScript or Java that use curly braces {}, Python uses spaces.
Loading Python environment...
This program is fine because both lines are at the beginning (no indentation).
Later, when you learn control flow (if statements, loops) in Chapters 16–17, you'll use indentation to show which code "belongs inside" the if statement or loop.
The Standard: Use 4 spaces per indentation level. Not 2 spaces, not tabs, not 3 spaces. Four spaces. This is the Python standard (PEP 8).
Pro Tip: Configure your code editor to convert tabs to spaces automatically. Most modern editors (VS Code, Cursor, etc.) do this by default. If you accidentally mix tabs and spaces, Python will complain with an IndentationError.
Comments: Explaining Your Code
A comment is a note in your code that Python ignores. Comments are for humans, not computers.
Comments start with #:
Loading Python environment...
Comments should explain why the code does something, not what it does. The code itself shows what it does.
Good comment:
Loading Python environment...
Poor comment:
Loading Python environment...
The poor comment just repeats what the code obviously does. The good comment explains the reasoning.
Philosophy: Code is for computers; comments are for humans (including your future self). Write comments you'd want to read in six months when you've forgotten why you wrote something.
Print Function for Output
The print() function displays text and data to your terminal. It's how you see what your program is doing.
Loading Python environment...
Output:
Hello, World!
You can print variables:
Loading Python environment...
You can print multiple items:
Loading Python environment...
print() is the primary way you'll validate what your program is doing throughout Part 4.
F-Strings: Modern Text Formatting
F-strings (formatted string literals) let you insert variables into text cleanly.
Loading Python environment...
The f before the quote says "this is a formatted string." Variables go inside {} brackets.
Why f-strings instead of the old way?
Old way (not recommended):
Loading Python environment...
Modern way (f-strings):
Loading Python environment...
F-strings are cleaner, easier to read, and more professional. This is what modern Python developers use.
💬 AI Colearning Prompt
"Explain why Python uses indentation (whitespace) for code blocks instead of curly braces like JavaScript or C++. What's the advantage for beginners? What's one potential problem?"
🎓 Expert Insight
In AI-native development, syntax is cheap—semantics is gold. We teach f-strings (not .format() or concatenation) because they're current. But syntax evolves: Python 2→3 changed dramatically, f-strings might be replaced in Python 3.20. Don't memorize syntax—understand patterns. "I want to combine text with variables" → ask AI for modern approach. That's the transferable skill.
Creating and Running .py Files
You've been learning concepts, but now you'll create your first real program file.
Step 1: Create a File
Open your text editor (VS Code, Cursor, or any code editor). Create a new file named hello.py.
The .py extension tells Python "this is a Python file."
Step 2: Write Code
Loading Python environment...
Save the file as hello.py in a folder you can find (like your Desktop or Documents).
Step 3: Run the Program
Open a terminal and navigate to the folder where you saved hello.py.
Type:
python hello.py
or on Mac/Linux:
python3 hello.py
Press Enter. Your program runs and displays:
Hello, Python!
Congratulations—you've written and executed your first Python program.
Code Examples
Example 1: Hello World with Variables
Loading Python environment...
Output:
Hello, Python!
Alice
Example 2: Variables and F-Strings
Loading Python environment...
Output:
My name is Bob
I'm 30 years old
I live in Portland
Example 3: Calculations and Output
Loading Python environment...
Output:
Price per item: $19.99
Quantity: 3
Total: $59.97
Tips
When you see an error you don't recognize, copy the error message and ask your AI: "What does this error mean?" This is a professional debugging skill.
Indentation errors are frustrating but common. They usually mean tabs and spaces got mixed. Use a text editor that shows whitespace (VS Code, Cursor). Your AI can help if you're stuck.
🤝 Practice Exercise
Ask your AI: "Create a Python program that displays a mini 'About Me' card using: (1) typed variables for name, age, city, (2) f-strings for formatted output, (3) decorative borders using string multiplication (like '=' * 40), (4) comments explaining each section. Then explain why comments should describe 'why' not 'what'."
Expected Outcome: You'll practice combining syntax elements (variables, type hints, f-strings, comments, print statements), understand professional commenting practices, and see how string multiplication creates visual formatting.
Common Mistakes
Mistake 1: Indentation errors (mixing tabs and spaces)
Symptom: IndentationError: unexpected indent
Solution: Use only spaces (not tabs). Configure your editor to show whitespace so you can see what's happening.
Mistake 2: Forgetting quotes around text
Loading Python environment...
Quotes tell Python "this is text, not a variable."
Mistake 3: Confusing print() with variable assignment
Loading Python environment...
print() displays output. Assignment stores data. Different purposes.
Mistake 4: Using old string formatting
Loading Python environment...
F-strings are cleaner and professional.
Try With AI
Can you write a complete program and improve it through code review?
🔍 Explore Code Review Process:
"Review this product information program for: correct type hints, proper indentation, good comments, and professional f-string usage. Then explain: What makes a comment GOOD (explains why) vs BAD (repeats what the code does)? [Paste your program code]"
🧪 Test Comment Quality:
"Compare these two comment styles:
price: float = 29.99 # Set price to 29.99vsprice: float = 29.99 # Launch price includes 25% promotional discount. Which is better and why? Then review the comments in my program—are they explaining WHY or just repeating WHAT? Suggest improvements."
🎯 Practice Program Enhancement:
"Improve this product display program by adding: a formatted header/footer using string multiplication (like
'=' * 40), better visual spacing, currency formatting for price (show $ symbol), and boolean to 'Yes'/'No' text conversion. Show me the enhanced version and explain what each improvement does."
🚀 Debug Broken Code:
"Find and fix ALL errors in this code:
product_name str = 'Keyboard'andquantity int = 20. Explain WHY each is an error (missing colon? wrong syntax?) and show the corrected version. What Python syntax rules do these violations break?"