⚙️ Lesson 2: Setting Up Python & Your First Program
Get Python running on your computer and write your very first program! Learn about different ways to write Python code and make friends with error messages.
🎯 Learning Objectives
By the end of this lesson, you will be able to:
- Install Python on your computer (Windows, Mac, or Linux)
- Verify that Python is installed correctly
- Use Python's interactive mode (IDLE) for quick experiments
- Create and run a Python script file (.py)
- Identify and fix common beginner errors
Estimated Time: 45 minutes
Project: Create a personal introduction script that displays your info using print()
In This Lesson
📥 Getting Python on Your Computer
Think of Python as a new language your computer needs to learn. Just like you might install a Spanish dictionary app to understand Spanish, you need to install Python for your computer to understand Python code.
Step-by-Step Installation Guide
🪟 For Windows:
- Go to python.org
- Click the big yellow "Download Python" button (it will show the latest version)
- Save the installer file
- Run the installer
- ⚠️ IMPORTANT: Check the box that says "Add Python to PATH" at the bottom!
- Click "Install Now"
- Wait for installation to complete
- Click "Close" when done
🍎 For Mac:
- Go to python.org
- Click "Download Python" (it will detect you're on Mac)
- Open the downloaded
.pkgfile - Follow the installer steps
- Python will be installed in your Applications folder
- The installer will also add Python to your system PATH automatically
Note: Mac may come with an older Python — we want Python 3.x for this course!
🐧 For Linux:
Most Linux distributions come with Python pre-installed! To check:
- Open Terminal
- Type:
python3 --version - If not installed, use your package manager:
# Ubuntu/Debian
sudo apt-get install python3
# Fedora
sudo dnf install python3
# Arch
sudo pacman -S python
🌐 Can't Install Locally? Use Google Colab!
If you're having trouble installing Python or using a restricted computer, you can use Google Colab — a free online Python environment:
- Go to colab.research.google.com
- Sign in with a Google account
- Click "New Notebook"
- Start coding immediately — no installation needed!
Pros: No installation, works on any device with internet, free GPU access
Cons: Requires internet connection, files don't save locally
✅ Verify Your Installation
Let's make sure Python is properly installed:
- Open Command Prompt (Windows) or Terminal (Mac/Linux)
- Type:
python --versionorpython3 --version - You should see something like:
Python 3.12.0 - If you see an error, try restarting your computer and trying again
⚠️ Common Installation Issues & Solutions
- "Python not recognized" error: Make sure you checked "Add Python to PATH" during installation
- Permission denied: Run installer as administrator (Windows) or use
sudo(Mac/Linux) - Wrong version installed: Uninstall old versions first through Control Panel/Settings
- Can't find IDLE: Look in Start Menu (Windows) or Applications (Mac)
- Installation freezes: Temporarily disable antivirus during installation
✌️ Two Ways to Write Python
You have two main options for writing Python code, like choosing between a calculator and a notebook:
💡 Modern Alternative: Visual Studio Code
While IDLE is great for beginners, many programmers prefer VS Code:
- Download from code.visualstudio.com
- Install the Python extension (it will prompt you)
- Create a new file with
.pyextension - Press Ctrl+F5 (Windows) or Cmd+F5 (Mac) to run
Benefits: Better code completion, integrated terminal, debugging tools, Git integration
Opening IDLE
- Look for "IDLE" in your programs (it comes with Python)
- You'll see a window with
>>>— this is Python's way of saying "I'm listening!"
👋 Hello, World! — Your First Program
In programming tradition, the first program everyone writes displays "Hello, World!" It's like a programmer's way of saying "Hi!" to their computer.
>>> print("Hello, World!")
Hello, World!
Let's break this down:
print() function — every piece has a purpose.📖 Key Terms
Function: A built-in command that performs an action. print() displays output.
String: Text data enclosed in quotes — "like this" or 'like this'.
Argument: The value you pass inside the parentheses for the function to work with.
🧮 Python as Your Personal Calculator
Try these in IDLE:
>>> 5 + 3 # Addition
8
>>> 100 - 42 # Subtraction
58
>>> 7 * 6 # Multiplication
42
>>> 20 / 4 # Division (always returns a decimal)
5.0
>>> 2 ** 3 # Exponentiation (2 to the power of 3)
8
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 100 - 42 | 58 |
* | Multiplication | 7 * 6 | 42 |
/ | Division | 20 / 4 | 5.0 |
** | Power | 2 ** 3 | 8 |
// | Floor Division | 7 // 2 | 3 |
% | Modulo (remainder) | 7 % 2 | 1 |
🔤 Playing with Text
Python can work with words too!
>>> "Python" + " is" + " fun!" # Concatenation — joining strings
'Python is fun!'
>>> "Ha" * 5 # Repetition — repeat a string
'HaHaHaHaHa'
>>> len("Hello") # len() counts characters
5
✅ Pro Tip
You can use either single quotes 'like this' or double quotes "like this" for strings. Python treats them the same. Just be consistent!
📄 Creating Your First Script File
Now let's save a program so we can run it again later:
Step-by-Step Script Creation
- Open a text editor (Notepad on Windows, TextEdit on Mac, or IDLE's File → New File)
- Type this code:
# My first Python script!
# The # symbol creates comments — notes for humans that Python ignores
print("Welcome to Python programming!")
print("Let me do some math for you:")
print("10 + 5 =", 10 + 5)
print("50 * 2 =", 50 * 2)
print() # Empty print creates a blank line
print("I can work with text too:")
print("Your name backwards is:", "Anna"[::-1])
print("SHOUTING IN CAPS:", "hello".upper())
print("whispering in lowercase:", "HELLO".lower())
Output:
Welcome to Python programming!
Let me do some math for you:
10 + 5 = 15
50 * 2 = 100
I can work with text too:
Your name backwards is: annA
SHOUTING IN CAPS: HELLO
whispering in lowercase: hello
- Save the file as
my_first_program.py(the.pytells the computer it's Python) - Run it:
- In IDLE: Press F5 or Run → Run Module
- In VS Code: Press Ctrl+F5
- In terminal:
python my_first_program.py
⚠️ Important Note: Always save your file with the .py extension. Without it, your computer won't know it's a Python program, and your editor won't give you helpful color-coding (syntax highlighting).
🐛 Understanding Errors — Your New Friends!
Errors aren't bad — they're Python trying to help you! Think of them as a helpful friend saying "Hey, I think you meant something else here."
🔍 Error Detective Guide
When you see an error, follow these steps:
- Read the error type (SyntaxError, NameError, etc.)
- Look at the line number (Python tells you where the problem is!)
- Check for common issues: missing quotes, forgotten parentheses, typos in command names, wrong indentation
- Test a simpler version to isolate the problem
✅ Pro Tip
Professional programmers encounter errors every single day. The difference isn't that they avoid errors — it's that they've gotten fast at reading and fixing them. Every error you fix teaches you something!
🏋️ Practice Exercises
🏋️ Exercise 1: Calculator Challenge
Objective: Use Python's interactive mode as a calculator.
Instructions:
In IDLE, calculate:
- How many hours are in a week?
- If you save $5 every day, how much will you have in a year?
- What's 15% of 80?
💡 Hint
Remember the operators: * for multiplication, and percentages are just decimals (15% = 0.15).
✅ Solution
>>> 24 * 7 # Hours in a week
168
>>> 5 * 365 # $5/day for a year
1825
>>> 80 * 0.15 # 15% of 80
12.0
🏋️ Exercise 2: Text Play
Objective: Practice string operations in IDLE.
Instructions:
- Print your name 10 times using
* - Combine three words with
+to make a sentence - Find the length of your full name using
len()
✅ Solution
>>> "Alice " * 10
'Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice '
>>> "I" + " love" + " Python!"
'I love Python!'
>>> len("Alice Johnson")
13
🏋️ Exercise 3: Personal Introduction Script
Objective: Create a complete .py script file that displays info about you.
Instructions:
Create a new .py file that:
- Prints a welcome message
- Prints your name
- Prints your favorite number multiplied by 10
- Prints your favorite food in ALL CAPS
- Calculates and prints your age in days (approximately)
💡 Hint
Use .upper() to convert text to uppercase, and remember there are about 365 days in a year.
✅ Solution
# My Personal Introduction
print("=== Welcome to My Page! ===")
print("Name: Alice")
print("Favorite number times 10:", 7 * 10)
print("Favorite food:", "pizza".upper())
print("Age in days (approx):", 25 * 365)
Output:
=== Welcome to My Page! ===
Name: Alice
Favorite number times 10: 70
Favorite food: PIZZA
Age in days (approx): 9125
🎯 Quick Quiz
Question 1: What does the print() function do?
Question 2: What does a SyntaxError mean?
Question 3: What is the difference between interactive mode and script mode?
Summary
🎉 Key Takeaways
- IDLE is great for quick experiments and learning
- Script files (
.py) let you save and share programs print()is your way to display output- Python can calculate math and manipulate text
- Errors are helpful feedback, not failures — read them!
- Comments (with
#) help explain your code to humans
🚀 What's Next?
In our next lesson, we'll learn about variables — Python's way of remembering information. Think of them as labeled boxes where you can store data for later use!
🎉 Congratulations!
You've written your first Python programs! You're officially a programmer now. Every expert started exactly where you are right now. The key is to keep practicing and stay curious!