Skip to main content

⚙️ 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.

graph TD A["🌐 Visit python.org"] --> B["⬇️ Download Python"] B --> C["▶️ Run Installer"] C --> D["✅ Python Installed!"] D --> E["🚀 Ready to Code"]

Step-by-Step Installation Guide

🪟 For Windows:

  1. Go to python.org
  2. Click the big yellow "Download Python" button (it will show the latest version)
  3. Save the installer file
  4. Run the installer
  5. ⚠️ IMPORTANT: Check the box that says "Add Python to PATH" at the bottom!
  6. Click "Install Now"
  7. Wait for installation to complete
  8. Click "Close" when done

🍎 For Mac:

  1. Go to python.org
  2. Click "Download Python" (it will detect you're on Mac)
  3. Open the downloaded .pkg file
  4. Follow the installer steps
  5. Python will be installed in your Applications folder
  6. 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:

  1. Open Terminal
  2. Type: python3 --version
  3. 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:

  1. Go to colab.research.google.com
  2. Sign in with a Google account
  3. Click "New Notebook"
  4. 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:

  1. Open Command Prompt (Windows) or Terminal (Mac/Linux)
  2. Type: python --version or python3 --version
  3. You should see something like: Python 3.12.0
  4. 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:

🖥️ Interactive Mode (IDLE / Python Shell) 🧮 Like a calculator ⚡ Instant results 🎓 Great for learning 🔬 Quick experiments VS 📄 Script Mode (.py files) 📓 Like a notebook 💾 Save your work 🏗️ Build full programs 🔄 Run again & again
Figure 1: Interactive mode is for quick experiments; script mode is for saving complete programs.

💡 Modern Alternative: Visual Studio Code

While IDLE is great for beginners, many programmers prefer VS Code:

  1. Download from code.visualstudio.com
  2. Install the Python extension (it will prompt you)
  3. Create a new file with .py extension
  4. Press Ctrl+F5 (Windows) or Cmd+F5 (Mac) to run

Benefits: Better code completion, integrated terminal, debugging tools, Git integration

Opening IDLE

  1. Look for "IDLE" in your programs (it comes with Python)
  2. 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("Hello, World!") ⚡ Function name 📦 Parentheses 📝 Text (string) The command Hold the input Quotes = text data
Figure 2: Anatomy of the 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
+Addition5 + 38
-Subtraction100 - 4258
*Multiplication7 * 642
/Division20 / 45.0
**Power2 ** 38
//Floor Division7 // 23
%Modulo (remainder)7 % 21

🔤 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:

graph LR A["📝 Open Text Editor"] --> B["⌨️ Write Python Code"] B --> C["💾 Save as .py file"] C --> D["▶️ Run the Script"] D --> E["🎉 See Results!"]

Step-by-Step Script Creation

  1. Open a text editor (Notepad on Windows, TextEdit on Mac, or IDLE's File → New File)
  2. 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
  1. Save the file as my_first_program.py (the .py tells the computer it's Python)
  2. 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."

🔴 SyntaxError Typo or grammar mistake in your code print("Hello" ← missing ) 💡 Fix: Check for missing quotes, parentheses, or colons 🟡 NameError Using a variable or name that doesn't exist print(hello) ← needs quotes 💡 Fix: Put text in quotes — "hello" not hello 🟣 IndentationError Unexpected spaces at the start of a line print("Oops") ← extra spaces 💡 Fix: Remove extra spaces at the beginning of the line
Figure 3: The three most common beginner errors — and how to fix them.

🔍 Error Detective Guide

When you see an error, follow these steps:

  1. Read the error type (SyntaxError, NameError, etc.)
  2. Look at the line number (Python tells you where the problem is!)
  3. Check for common issues: missing quotes, forgotten parentheses, typos in command names, wrong indentation
  4. 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:

  1. How many hours are in a week?
  2. If you save $5 every day, how much will you have in a year?
  3. 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:

  1. Print your name 10 times using *
  2. Combine three words with + to make a sentence
  3. 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:

  1. Prints a welcome message
  2. Prints your name
  3. Prints your favorite number multiplied by 10
  4. Prints your favorite food in ALL CAPS
  5. 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!