2 Getting Started with Python
2.1 The Wall
AI gave you a Python script. You copied it into a file, double-clicked it, and nothing happened. Or you pasted it into a terminal and got SyntaxError: invalid syntax. Or it ran but the output disappeared instantly.
You asked AI how to run Python code and got a wall of text about interpreters, virtual environments, IDEs, and Jupyter Notebooks. You just wanted to run the file.
This chapter fixes that. You will learn how Python code runs, how to read the structure AI gives you, and why indentation and naming actually matter when you are trying to modify what AI produces.
2.2 Thinking Session
2.2.1 Getting Oriented
I have a Python file that AI generated for me. When I try to run it, I get errors or nothing happens. Can you explain the different ways to run Python code? I want to understand: what is the difference between running code in a Jupyter Notebook, in a terminal, and in a script file? When would I use each one?
Your AI should explain three main approaches: the interactive interpreter (type code line by line), script files (save and run a .py file), and Jupyter Notebooks (cells that mix code and output). If it jumps straight to IDE setup, pull it back to the basics first.
2.2.2 Go Deeper
When I look at AI-generated Python code, I notice it uses indentation to structure things, unlike other languages that use curly braces. Why does Python do this? What happens if I get the indentation wrong? Show me an example of code that breaks because of indentation.
Your AI should explain that Python uses indentation to define code blocks. It is not decorative, it is structural. A misplaced space causes an IndentationError. This matters because when you copy AI-generated code and reformat it, you can accidentally break the structure.
AI-generated code often has comments like # This is a comment and names like user_name or MAX_RETRIES. Are there rules for how to name things in Python? What conventions do Python programmers follow, and why should I care when I am mostly reading AI-generated code?
The naming conventions matter because they carry meaning. When you see MAX_RETRIES, the uppercase tells you it is a constant. When you see user_name, the snake_case tells you it is a variable. When you see UserAccount, the PascalCase tells you it is a class. If you do not know these conventions, AI-generated code looks like a random collection of names.
2.2.3 Challenge It
Here is some Python code with several problems. Can you identify all of them?
if x > 10
Print("x is big")
print("done")
message = "Hello, world!'
Four errors: missing colon after if x > 10, capital P in Print (Python is case-sensitive), inconsistent indentation on print("done"), and mismatched quotes on the string. These are exactly the errors you will see when you edit AI-generated code carelessly.
2.2.4 What You Should Have Learned
After your Thinking Session, you should be able to:
- Run Python code three ways: interpreter, script, notebook
- Explain why indentation matters in Python (it defines structure, not style)
- Read Python naming conventions: snake_case, PascalCase, UPPER_CASE
- Spot common syntax errors: missing colons, wrong case, bad indentation, mismatched quotes
If any of these are unclear, continue the conversation with your AI before moving on.
2.3 The Gap
You now know how to run Python and how to read the structure of Python code. When AI generates code with indented blocks, comments, and named variables, you understand what those structural elements mean, even before you understand what the code does.
In the Building Session, you will set up your Python environment and add proper structure to the chatbot from Chapter 1: comments, named constants, and clear formatting.
2.4 Building Session
2.4.1 The Spec
Take the chatbot skeleton from Chapter 1 and add proper Python structure:
- Add a descriptive comment at the top explaining what the code does
- Use named constants for the bot name and version (UPPER_CASE)
- Add inline comments on any non-obvious lines
- Make sure indentation is consistent (4 spaces)
2.4.2 Prompt It
Here is my chatbot from Chapter 1:
bot_name = "PyBot"
print(f"Hello! I'm {bot_name}. Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == "quit":
print(f"{bot_name}: Goodbye!")
break
print(f"{bot_name}: You said '{user_input}'")Improve it by adding: - A module docstring at the top describing the program - BOT_NAME and VERSION as constants (uppercase) - Inline comments explaining the while loop and the break - Keep it simple. No functions, no imports
2.4.3 Read the Code
Your AI will produce something like this:
"""
PyBot: A simple terminal chatbot.
Greets the user and echoes their messages.
Type 'quit' to exit.
"""
# Configuration
BOT_NAME = "PyBot"
VERSION = "0.2"
print(f"Hello! I'm {BOT_NAME} v{VERSION}.")
print("Type 'quit' to exit.")
# Main conversation loop (runs until user quits)
while True:
user_input = input("You: ")
if user_input.lower() == "quit":
print(f"{BOT_NAME}: Goodbye!")
break # Exit the loop
print(f"{BOT_NAME}: You said '{user_input}'")The docstring at the top (triple quotes) describes the program. BOT_NAME and VERSION are uppercase. The naming convention signals these are constants that should not change. The comments explain why (the loop runs until quit) not what (print prints). The break comment is useful because break is a keyword you might not recognise yet.
2.4.4 Stretch It
Add a comment block after the constants that lists what the chatbot can do in this version. Format it as a multi-line comment. Also add a # TODO: comment listing two features we will add in future chapters.
Read the result. # TODO: comments are a convention for marking planned work. You will see these frequently in AI-generated code and in professional projects.
2.5 Your Chatbot So Far
After this chapter, your chatbot can:
- Print a greeting with the bot’s name and version
- Accept user messages in a loop
- Echo messages back
- Exit when the user types “quit”
- Has proper structure: docstring, constants, comments
2.6 Quick Reference
# Comment styles
# Single-line comment
x = 5 # Inline comment
"""Docstring for documentation."""
# Naming conventions
user_name = "alice" # snake_case: variables
MAX_RETRIES = 3 # UPPER_CASE: constants
class UserAccount: # PascalCase: classes
pass
# Indentation: 4 spaces per level
if True:
print("indented")
# Running Python
# Terminal: python3 my_script.py
# Notebook: Shift+Enter to run cell