Installing Python, IDEs, and Running Scripts

25 minâ€ĸtext

Theory & Concepts

Getting Your Python Environment Ready

Before you can start writing Python code, you need to set up your development environment properly. This lesson will guide you through installing Python, selecting an IDE (Integrated Development Environment), and running your first scripts.

💡 Welcome!: "This is your first step into programming with Python. Take your time with the installation-a proper setup now will save you hours of troubleshooting later!"

Why Installation Matters

A proper setup ensures:

  • Consistency: Your code runs the same way every time
  • Efficiency: Good tools make you productive from day one
  • Confidence: You know exactly where Python lives and how to access it
  • Future-Proofing: Easy to upgrade and manage packages

What You'll Learn

  1. How to install Python on Windows, macOS, and Linux
  2. Verifying your Python installation works correctly
  3. Choosing and setting up the best IDE for beginners
  4. Running Python in three different ways (shell, scripts, IDE)
  5. Troubleshooting common installation problems
  6. Best practices for organizing your Python projects

Installing Python

Python runs on all major operating systems. Follow the instructions for your platform.

Windows Installation

Step-by-Step Guide:

  1. Download Python:

    • Visit python.org/downloads
    • Click "Download Python 3.12" (or latest version)
    • Download the Windows installer (64-bit recommended)
  2. Run the Installer:

âš ī¸ CRITICAL: Before clicking "Install Now", check the box that says "Add Python to PATH". This is the #1 source of installation problems!

  • Double-click the downloaded installer
  • ✅ Check "Add Python to PATH" (very important!)
  • Click "Install Now" for standard installation
  • Wait 2-3 minutes for installation to complete
  • Click "Close" when finished
  1. Verify Installation:

    • Press Windows Key + R, type cmd, press Enter
    • In Command Prompt, type:
    bash
    python --version
    • Expected output: Python 3.12.0 (or your version)

✅ Success Check: If you see the Python version, congratulations! Python is installed correctly. If you get an error, see the "Common Pitfalls" section below.

macOS Installation

Step-by-Step Guide:

â„šī¸ Note: macOS comes with Python 2.7 pre-installed, but you need Python 3.x for modern development. Don't use the built-in Python 2!

Option 1: Official Installer (Easier)

  1. Visit python.org/downloads
  2. Download the macOS installer
  3. Open the .pkg file
  4. Follow the installation wizard
  5. Enter your password when prompted

Option 2: Homebrew (Recommended for Developers)

Homebrew is a package manager that makes installing tools easier.

  1. Install Homebrew (if not installed):

    • Open Terminal (Applications → Utilities → Terminal)
    • Visit brew.sh and follow instructions
    • Or run:
    bash
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Python via Homebrew:

    bash
    brew install python3
  3. Verify Installation:

    bash
    python3 --version

    Expected: Python 3.12.0

💡 Tip: On macOS, always use python3 (not python) to ensure you're using Python 3, not the old Python 2.

Linux Installation

Most Linux distributions include Python, but you may need to install Python 3.

Ubuntu/Debian:

bash
# Update package list
sudo apt update
# Install Python 3 and pip (package manager)
sudo apt install python3 python3-pip python3-venv
# Verify installation
python3 --version

Fedora/RHEL/CentOS:

bash
# Install Python 3
sudo dnf install python3 python3-pip
# Verify
python3 --version

Arch Linux:

bash
# Install Python 3
sudo pacman -S python python-pip
# Verify
python --version

✅ Success Check: If python3 --version displays Python 3.8 or higher, you're ready to go!


Choosing an IDE or Code Editor

An IDE (Integrated Development Environment) provides tools to write, run, and debug code efficiently. Here are the best options for Python beginners:

1. VS Code (Highly Recommended for Beginners)

Why VS Code?

  • ✅ Free and open-source
  • ✅ Lightweight but powerful
  • ✅ Excellent Python extension
  • ✅ Integrated terminal
  • ✅ Auto-completion and IntelliSense
  • ✅ Used by professionals worldwide

Installation Steps:

  1. Download from code.visualstudio.com
  2. Install VS Code (follow platform-specific instructions)
  3. Launch VS Code
  4. Click Extensions icon (left sidebar) or press Ctrl+Shift+X
  5. Search for "Python"
  6. Install "Python" extension by Microsoft (has 50M+ downloads)
  7. Reload VS Code if prompted

First-Time Setup:

  1. Create a new file: File → New File
  2. Save it as hello.py (the .py extension activates Python features)
  3. VS Code will prompt you to select a Python interpreter
  4. Choose the Python 3.x version you installed

💡 Tip: VS Code shows the active Python interpreter in the bottom-left corner. Click it to change interpreters if needed.

2. PyCharm Community Edition (Feature-Rich)

Why PyCharm?

  • ✅ Purpose-built for Python development
  • ✅ Intelligent code completion
  • ✅ Powerful built-in debugger
  • ✅ Project management
  • ✅ Free Community Edition

Installation Steps:

  1. Download from jetbrains.com/pycharm/download
  2. Choose "Community Edition" (free)
  3. Install and launch PyCharm
  4. Create a new project
  5. Select Python interpreter (PyCharm auto-detects)

â„šī¸ Note: PyCharm is heavier than VS Code but offers more out-of-the-box features. Great if you have a powerful computer and want an all-in-one solution.

3. IDLE (Perfect for Absolute Beginners)

Why IDLE?

  • ✅ Comes pre-installed with Python
  • ✅ Zero setup required
  • ✅ Simple and distraction-free
  • ✅ Interactive shell built-in

How to Launch:

  • Windows: Search "IDLE" in Start menu
  • macOS: Open Terminal, type idle3
  • Linux: Type idle3 or idle in terminal

💡 Beginner Tip: Start with IDLE to focus on learning Python itself. Once comfortable, graduate to VS Code for professional projects.

4. Jupyter Notebook (For Interactive Learning)

Why Jupyter?

  • ✅ Run code in cells (experiment easily)
  • ✅ Mix code, text, and visuals
  • ✅ Popular in data science
  • ✅ Great for tutorials and learning

Installation:

bash
# Install Jupyter
pip install jupyter
# Launch Jupyter Notebook
jupyter notebook

â„šī¸ Note: Jupyter opens in your web browser. Perfect for learning Python alongside documentation and notes!


Running Python Code - Three Methods

Understanding different ways to run Python helps you work efficiently in various scenarios.

Method 1: Interactive Python Shell (REPL)

The Python shell lets you type code and see results instantly-perfect for learning and experimenting.

How to Access the REPL:

bash
# Windows
python
# macOS/Linux
python3

What You'll See:

python
Python 3.12.0 (main, Oct 2024)
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> prompt means Python is waiting for your command.

Try These Examples:

python
>>> print("Hello, Python!")
Hello, Python!
>>> 2 + 2
4
>>> name = "Alex"
>>> print(f"Hello, {name}!")
Hello, Alex!
>>> # You can use Python as a calculator
>>> 15 * 7
105
>>> # Get help on any function
>>> help(print)

💡 Interactive Tip: In the REPL, you don't need print() for single expressions-they display automatically! But always use print() in script files.

Exit the REPL:

  • Type exit() and press Enter
  • Or press Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows)

When to Use the REPL:

  • ✅ Quick calculations and math
  • ✅ Testing syntax you're unsure about
  • ✅ Experimenting with new functions
  • ✅ Learning Python interactively

Method 2: Running Script Files (.py)

Scripts are files containing Python code that you can run repeatedly. This is how most Python programs work.

Creating Your First Script:

  1. Create a file called hello.py:
python
# hello.py - My first Python script
print("Hello, World!")
print("Welcome to Python programming!")
# Variables work in scripts too
name = "Learner"
print(f"Hello, {name}!")
# Basic calculation
result = 10 + 5
print(f"10 + 5 = {result}")
  1. Save the file with a .py extension

  2. Navigate to the file's directory:

bash
# Windows
cd Documents\PythonProjects
# macOS/Linux
cd ~/Documents/PythonProjects
  1. Run the script:
bash
# Windows
python hello.py
# macOS/Linux
python3 hello.py

Expected Output:

Hello, World!
Welcome to Python programming!
Hello, Learner!
10 + 5 = 15

✅ Success! If you see this output, you've successfully run your first Python script!

When to Use Scripts:

  • ✅ Programs you want to save and reuse
  • ✅ Automation tasks
  • ✅ Complete applications
  • ✅ Production code

Method 3: Running Code in an IDE

IDEs make running code as simple as clicking a button.

In VS Code:

  1. Open or create a .py file
  2. Write your code
  3. Click the â–ļī¸ (Run) button in the top-right
  4. Output appears in the integrated terminal

💡 VS Code Shortcut: Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS), then type "Run Python File"

In PyCharm:

  1. Open or create a .py file
  2. Write your code
  3. Right-click anywhere in the file
  4. Select "Run 'filename'" or press Shift+F10

In IDLE:

  1. Create a new file: File → New File
  2. Write your code
  3. Save the file (Ctrl+S)
  4. Press F5 or click Run → Run Module

When to Use IDE:

  • ✅ During active development
  • ✅ When using debugging tools
  • ✅ Working on large projects
  • ✅ Want integrated features (auto-complete, error detection)

Understanding Python Versions

Python 2 vs Python 3

âš ī¸ Important: Python 2 reached end-of-life in 2020. Always use Python 3 for new projects!

Key Differences:

  • Python 2: print "Hello" (no parentheses)
  • Python 3: print("Hello") (function call)

How to Check Which Version You're Using:

bash
python --version

If it shows Python 2.x, use python3 instead:

bash
python3 --version

Minor Versions (3.8, 3.9, 3.10, 3.11, 3.12)

Recommendation: Use Python 3.10 or newer for the best features and performance.

Checking Your Version:

python
import sys
print(f"Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")

Project Organization Best Practices

Recommended Folder Structure

Documents/
└── PythonProjects/
├── lesson01/
│ ├── hello.py
│ └── check_installation.py
├── lesson02/
│ └── variables.py
└── practice/
└── experiments.py

💡 Organization Tip: Create a new folder for each lesson or project. This keeps your work organized and makes it easy to find your code later!

Naming Conventions

Python File Names:

  • ✅ Use lowercase: calculator.py
  • ✅ Use underscores for spaces: student_grades.py
  • ✅ Be descriptive: temperature_converter.py
  • ❌ Avoid: Test1.py, new.py, asdf.py

Common Installation Problems & Solutions

Problem 1: "Python is not recognized as a command"

Symptom: When you type python --version in Command Prompt, you get:

'python' is not recognized as an internal or external command

Cause: Python wasn't added to your system PATH

Solution (Windows):

  1. Search for "Environment Variables" in the Start menu
  2. Click "Environment Variables"
  3. Under "System variables", find and select "Path"
  4. Click "Edit"
  5. Click "New" and add: C:\Users\YourUsername\AppData\Local\Programs\Python\Python312
  6. Click "OK" on all windows
  7. Close and reopen Command Prompt
  8. Try python --version again

💡 Quick Fix: Uninstall Python and reinstall, making sure to check "Add Python to PATH" during installation.

Problem 2: "python3: command not found" (macOS/Linux)

Cause: Python 3 isn't installed or uses a different command name

Solution:

bash
# Check if python3 exists
which python3
# If not found, install it
# macOS:
brew install python3
# Ubuntu/Debian:
sudo apt install python3
# Try again:
python3 --version

Problem 3: Code Runs But Shows No Output

Cause: Forgot to use print() in your script file

Wrong:

python
# script.py
2 + 2 # This calculates but doesn't display

Correct:

python
# script.py
result = 2 + 2
print(result) # Now it displays!

â„šī¸ Note: In the interactive REPL, expressions display automatically. In script files, always use print() to show output!

Problem 4: "Cannot Find File or Directory"

Cause: Your terminal is in the wrong folder

Solution:

bash
# Find your current location
pwd # macOS/Linux
cd # Windows (shows current directory)
# Navigate to your script's location
cd Documents/PythonProjects
# List files to verify your script is there
ls # macOS/Linux
dir # Windows
# Now run your script
python3 hello.py

💡 Tip: In VS Code or PyCharm, the terminal automatically opens in your project folder, avoiding this issue entirely!


Quick Reference Commands

Essential Commands

bash
# Check Python version
python --version # Windows
python3 --version # macOS/Linux
# Launch interactive Python shell
python # Windows
python3 # macOS/Linux
# Run a Python script
python script.py # Windows
python3 script.py # macOS/Linux
# Install a package
pip install package_name # Windows
pip3 install package_name # macOS/Linux
# Upgrade pip (Python's package installer)
python -m pip install --upgrade pip # Windows
python3 -m pip install --upgrade pip # macOS/Linux

Terminal Navigation

bash
# Show current directory
pwd # macOS/Linux
cd # Windows
# Change directory
cd folder_name
# Go up one level
cd ..
# List files in current directory
ls # macOS/Linux
dir # Windows

What's Next?

Congratulations on setting up Python! Now you're ready to:

  1. ✅ Write your first Python programs
  2. ✅ Experiment in the interactive shell
  3. ✅ Use your IDE's features
  4. ✅ Start learning Python syntax

✅ You're Ready!: The hardest part is done. From here on, you can focus entirely on learning Python itself. Everything is set up and working!

Your Next Steps

  1. Practice running code in all three methods (REPL, scripts, IDE)
  2. Get familiar with your IDE - explore the features
  3. Create a project folder to organize your learning
  4. Move to the next lesson to start writing real Python code!

The setup might feel overwhelming at first, but you only do it once. From now on, it's all about learning to code! 🚀

Lesson Content

Set up your Python development environment from scratch. Learn how to install Python on Windows, macOS, and Linux, choose the right IDE, and run your first Python scripts with confidence.

Code Example

python
# ============================================
# LESSON 1.1: Installation & Running Scripts
# ============================================
# This file demonstrates different ways to verify
# your Python installation and run code.
# ----------------------
# 1. VERIFICATION SCRIPT
# ----------------------
# Save this as: check_installation.py
# Run it to verify Python is working correctly
import sys
import platform
print("=" * 50)
print("PYTHON INSTALLATION VERIFICATION")
print("=" * 50)
# Display Python version
print(f"
Python Version: {sys.version}")
print(f"Version Info: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
# Display system information
print(f"
Operating System: {platform.system()}")
print(f"OS Version: {platform.version()}")
print(f"Machine Type: {platform.machine()}")
# Display Python executable location
print(f"
Python Executable: {sys.executable}")
# Test basic functionality
print("
" + "=" * 50)
print("BASIC FUNCTIONALITY TEST")
print("=" * 50)
# Test arithmetic
result = 2 + 2
print(f"
✓ Arithmetic works: 2 + 2 = {result}")
# Test string manipulation
greeting = "Hello, Python!"
print(f"✓ Strings work: {greeting}")
# Test list operations
numbers = [1, 2, 3, 4, 5]
print(f"✓ Lists work: {numbers}")
print(f"✓ Sum function works: sum = {sum(numbers)}")
# Test loops
print("
✓ Loops work:")
for i in range(1, 4):
print(f" - Iteration {i}")
# Test functions
def greet(name):
"""Test function to demonstrate Python is working."""
return f"Welcome, {name}!"
print(f"
✓ Functions work: {greet('Learner')}")
print("
" + "=" * 50)
print("✅ ALL TESTS PASSED!")
print("Your Python installation is working correctly.")
print("=" * 50)
# ----------------------
# 2. HELLO WORLD SCRIPT
# ----------------------
# The traditional first program
# Save as: hello_world.py
print("
--- Hello World Example ---")
print("Hello, World!")
print("Welcome to Python programming!")
# ----------------------
# 3. INTERACTIVE SHELL DEMO
# ----------------------
# Try these commands in the Python REPL (interactive shell):
#
# >>> print("Hello from the shell!")
# >>> 5 + 3
# >>> name = "Alex"
# >>> print(f"Hi, {name}!")
# >>> help(print)
# >>> exit()
# ----------------------
# 4. RUNNING SCRIPTS - PRACTICE
# ----------------------
# Save this as: my_first_script.py
# Then run it using one of these methods:
#
# Method 1 - Command Line:
# Windows: python my_first_script.py
# macOS/Linux: python3 my_first_script.py
#
# Method 2 - IDE:
# - Click the Run button (â–ļī¸)
# - Or press F5
#
# Method 3 - IDLE:
# - Open the file in IDLE
# - Press F5 or Run → Run Module
print("
--- Script Execution Example ---")
print("This script is running successfully!")
print("You can see this output because you executed the .py file.")
# ----------------------
# 5. PATH DEMONSTRATION
# ----------------------
# Understanding where Python looks for modules
import os
print("
--- Current Working Directory ---")
print(f"Your script is running from: {os.getcwd()}")
# ----------------------
# 6. MULTIPLE WAYS TO PRINT
# ----------------------
# All of these display output
print("
--- Different Print Methods ---")
# Basic print
print("Method 1: Basic print")
# Multiple arguments
print("Method 2:", "Multiple", "arguments")
# f-strings (modern and recommended)
language = "Python"
version = 3.12
print(f"Method 3: I'm learning {language} version {version}")
# Concatenation
print("Method 4: " + "String " + "concatenation")
# Print with end parameter (no new line)
print("Same line", end=" ")
print("continued here")
# ----------------------
# 7. COMMENTS AND DOCUMENTATION
# ----------------------
# Good code explains itself
# This is a single-line comment
print("Comments help explain your code") # Inline comment
"""
This is a multi-line comment.
Also called a docstring when used to document functions.
You can write multiple lines of explanation here.
"""
def documented_function():
"""
This docstring explains what the function does.
Good practice: Always document your functions!
"""
return "I'm a well-documented function"
print(documented_function())
# ----------------------
# 8. ERROR HANDLING PREVIEW
# ----------------------
# What happens when something goes wrong?
print("
--- Error Handling Demo ---")
# This will work
try:
result = 10 / 2
print(f"Division successful: {result}")
except ZeroDivisionError:
print("Cannot divide by zero!")
# Intentional error handling (uncomment to see error)
# try:
# result = 10 / 0 # This will cause an error
# except ZeroDivisionError:
# print("Oops! Cannot divide by zero.")
# ----------------------
# 9. GETTING INPUT (Preview)
# ----------------------
# Uncomment these lines to make the script interactive
# (Note: Input only works when running as a script, not in interactive mode)
# print("
--- Interactive Input Demo ---")
# user_name = input("What's your name? ")
# print(f"Nice to meet you, {user_name}!")
# ----------------------
# 10. FINAL MESSAGE
# ----------------------
print("
" + "=" * 50)
print("🎉 CONGRATULATIONS!")
print("=" * 50)
print("You've successfully:")
print(" ✅ Installed Python")
print(" ✅ Set up your development environment")
print(" ✅ Run your first Python script")
print("
You're ready to start your Python journey!")
print("=" * 50)
# ----------------------
# PRACTICE EXERCISES
# ----------------------
# Try creating these scripts on your own:
#
# Exercise 1: Create a script that prints your name and age
# Exercise 2: Create a script that calculates 23 * 45 and prints the result
# Exercise 3: Create a script that prints your favorite quote
# Exercise 4: Modify this script to add your own print statements
#
# Remember: Save your files with the .py extension!
Section 1 of 12 â€ĸ Lesson 1 of 4