Installing Python and Setting Up Environment
# Installing Python and Setting Up Environment
Welcome to Chapter 2! Now that you know what Python is and why it's amazing, it's time to install it on your computer and set up a professional development environment. By the end of this chapter, you'll have Python running and will have executed your first script.
---
1. Introduction
Before you can write Python code, you need two things:
- 1. Python itself — the interpreter that runs your code.
- 2. A code editor or IDE — a tool where you write your code comfortably.
Think of it like cooking: Python is the stove, and the IDE is your fully equipped kitchen with cutting boards, knives, and utensils. You *could* cook with just a stove, but a full kitchen makes everything easier and more efficient.
---
2. Learning Objectives
By the end of this chapter, you will be able to:
- Download and install Python on Windows, macOS, and Linux.
- Verify the Python installation via the command line.
- Understand what pip is and how to use it.
- Set up VS Code as your primary Python IDE.
- Understand PyCharm basics.
- Write and run your first Python script.
- Use the Python interactive shell (REPL).
---
3. Installing Python on Windows
Step 1: Download Python
- 1. Go to https://www.python.org/downloads/
- 2. Click the big yellow "Download Python 3.x.x" button.
Step 2: Run the Installer
-
1.
Double-click the downloaded
.exefile.
- 2. ⚠️ CRITICAL: Check the box that says "Add Python to PATH" at the bottom of the installer. This is the most common mistake beginners make!
- 3. Click "Install Now".
Step 3: Verify Installation
Open Command Prompt (search "cmd" in Start menu) and type:Expected Output:
Also verify pip (Python's package manager):
Expected Output:
---
4. Installing Python on macOS
Option 1: Using the Official Installer
- 1. Visit python.org/downloads.
-
2.
Download the macOS installer (
.pkgfile).
- 3. Run the installer and follow the prompts.
Option 2: Using Homebrew (Recommended)
Verify Installation
> Note: On macOS, you may need to use python3 and pip3 instead of python and pip.
---
5. Installing Python on Linux
Most Linux distributions come with Python pre-installed. To check:
If not installed, use your package manager:
Ubuntu / Debian
Fedora
Arch Linux
---
6. Understanding pip
pip stands for "Pip Installs Packages" and is Python's official package manager. It lets you install third-party libraries from the Python Package Index (PyPI).
Common Useful Packages
| Package | Purpose |
|---|---|
| requests | HTTP requests |
| flask | Web development |
| pandas | Data analysis |
| numpy | Numerical computing |
| matplotlib | Data visualization |
| beautifulsoup4 | Web scraping |
---
7. Python IDEs and Code Editors
7.1 VS Code (Recommended for Beginners)
Visual Studio Code is a free, lightweight, and powerful code editor by Microsoft. It's the most popular editor for Python development.
Installation:
- 1. Download from code.visualstudio.com
- 2. Install and open VS Code.
- 3. Go to Extensions (Ctrl+Shift+X).
- 4. Search for "Python" by Microsoft and click Install.
Recommended VS Code Extensions for Python:
- Python (by Microsoft) — Core Python support
- Pylance — Fast, feature-rich language server
- Python Debugger — Debugging support
- Code Runner — Run code with one click
7.2 PyCharm
PyCharm by JetBrains is a full-featured Python IDE.
- Community Edition — Free, open-source (great for learning).
- Professional Edition — Paid, with web development and database tools.
Installation:
- 1. Download from jetbrains.com/pycharm.
- 2. Install the Community Edition.
- 3. Create a new project and start coding.
7.3 Other Options
| Editor | Best For |
|---|---|
| IDLE | Absolute beginners (comes with Python) |
| Jupyter Notebook | Data science, exploration |
| Sublime Text | Lightweight editing |
| Atom | Open-source enthusiasts |
| Thonny | Beginner-focused IDE |
---
8. The Python Interactive Shell (REPL)
Python comes with an interactive shell called the REPL (Read-Eval-Print Loop). It lets you execute Python code line by line — perfect for quick experiments.
How to Open:
Open your terminal and typepython (or python3 on macOS/Linux):
You'll see something like:
Try It:
```python id="py2_repl1" >>> print("Hello from the Python shell!") Hello from the Python shell!
>>> 2 + 2 4
>>> name = "Alice" >>> print(f"Welcome, {name}!") Welcome, Alice!
>>> exit() # To leave the shell
python id="py2_ex1" # hello.py — Your very first Python script! print("Hello, World!") print("I am learning Python!") print("This is my first script! 🎉")
bash python hello.py
Hello, World! I am learning Python! This is my first script! 🎉
Your Python file (.py): ┌──────────────────────────────────┐ │ # Comments (notes for humans) │ │ # They start with # │ │ │ │ import math ← Module imports │ │ │ │ name = "Alice" ← Variables │ │ age = 25 │ │ │ │ def greet(): ← Functions │ │ print(f"Hi, {name}!") │ │ │ │ greet() ← Function call │ └──────────────────────────────────┘
python id="py2_ex2" print("=" * 40) print(" Python Installation Successful!") print("=" * 40)
======================================== Python Installation Successful! ========================================
python id="py2ex3" # mathtest.py import math
radius = 5 area = math.pi * radius ** 2 print(f"Area of circle with radius {radius}: {area:.2f}")
Area of circle with radius 5: 78.54
python id="py2ex4" # environmentinfo.py import sys import platform
print(f"Python Version : {sys.version}")
print(f"Platform : {platform.system()} {platform.release()}")
print(f"Architecture : {platform.architecture()[0]}")
print(f"Processor : {platform.processor()}")
``
---
12. Common Mistakes
-
1.
Not adding Python to PATH: If python --version
doesn't work, you likely forgot to check "Add to PATH" during installation. Reinstall and check the box.
-
2.
Using python
vspython3:On macOS/Linux, pythonmight point to Python 2. Always usepython3.
-
3.
Saving files without .py
extension:Your files must end in .pyfor Python to recognize them.
-
4.
Running code in the wrong directory: Make sure your terminal is in the same directory as your .py
file.
- 5. Not installing the Python VS Code extension: Without it, you won't get syntax highlighting or auto-completion.
---
13. Best Practices
- Always use the latest Python 3 version — Stay updated for security and features.
- Use virtual environments — Isolate project dependencies (covered in Chapter 15).
- Organize your files — Create separate folders for each project.
- Use VS Code with the Python extension — It provides the best balance of simplicity and power.
- Practice in the REPL — Use the interactive shell for quick experiments.
---
14. Exercises
- 1. Install Python on your computer and verify the version using the command line.
- 2. Install VS Code and the Python extension.
-
3.
Create a file called myinfo.py
that prints your name, age, and favorite hobby.
- 4. Open the Python REPL and perform 5 different math operations.
-
5.
Use pip list
to see all installed packages on your system.
---
15. MCQs with Answers
Q1: What is the official website to download Python? A) python.com B) python.org C) python.dev D) getpython.org Answer: B — The official Python website is python.org.
Q2: What must you check during Windows installation? A) Install for all users B) Add Python to PATH C) Use custom installation D) Install documentation Answer: B — Adding Python to PATH is critical for command-line access.
Q3: What is pip? A) Python Integrated Platform B) Python Installation Program C) Pip Installs Packages D) Python Interface Protocol Answer: C — pip stands for "Pip Installs Packages."
Q4: What command checks your Python version?
A) python -v
B) python --version
C) python -check
D) python version
Answer: B — python --version displays the installed version.
Q5: Which VS Code extension is essential for Python? A) Python by Microsoft B) Java Extension Pack C) Live Server D) ESLint Answer: A — The Python extension by Microsoft provides IntelliSense, debugging, and more.
Q6: What is REPL? A) Run-Execute-Process-Loop B) Read-Eval-Print-Loop C) Read-Execute-Parse-Load D) Run-Evaluate-Program-Log Answer: B — REPL stands for Read-Eval-Print-Loop.
Q7: What file extension do Python files use?
A) .python
B) .pt
C) .py
D) .pn
Answer: C — Python files use the .py extension.
Q8: Which command installs a package using pip?
A) pip get requests
B) pip download requests
C) pip install requests
D) pip add requests
Answer: C — pip install packagename is the correct command.
Q9: What is PyCharm? A) A Python library B) A Python IDE by JetBrains C) A Python testing framework D) A Python web framework Answer: B — PyCharm is a full-featured IDE by JetBrains.
Q10: How do you exit the Python REPL?
A) quit
B) stop()
C) exit()
D) close()
Answer: C — Type exit() or quit() to leave the REPL.
---
16. Interview Questions
- 1. How do you install Python on different operating systems?
) or the official installer. On Linux, use the package manager (e.g., sudo apt install python3).
-
2.
What is pip and how do you use it?
*Answer:* pip is Python's package manager that installs third-party libraries from PyPI. Usage: pip install packagename, pip uninstall packagename, pip list.
-
3.
What is a virtual environment and why is it important?
*Answer:* A virtual environment is an isolated Python environment for a project. It prevents package conflicts between different projects by keeping dependencies separate.
-
4.
What is the difference between an IDE and a text editor?
*Answer:* An IDE (like PyCharm) includes built-in tools for debugging, testing, version control, and project management. A text editor (like VS Code with extensions) is lighter but can be extended with plugins to achieve similar functionality.
-
5.
How do you run a Python script from the command line?
*Answer:* Navigate to the directory containing the script and run python script_name.py (or python3 on macOS/Linux).
---
17. FAQs
Q: Do I need to uninstall Python 2 before installing Python 3?
A: No. They can coexist. However, make sure your PATH points to Python 3.
Q: Can I have multiple versions of Python installed?
A: Yes. Use tools like
pyenv to manage multiple Python versions easily.
Q: Is IDLE a good editor for beginners?
A: IDLE is fine for absolute beginners, but VS Code is a much better investment of your time as it scales with your skills.
Q: Do I need internet to run Python?
A: No. Once installed, Python runs completely offline. You only need internet to install packages via pip.
Q: What is the difference between
python and python3 commands?
A: On some systems, python refers to Python 2. The python3 command explicitly calls Python 3. On modern Windows installations, python usually points to Python 3.
---
18. Summary
-
Download Python from python.org and always check "Add to PATH".
-
Use pip to install third-party packages.
-
VS Code with the Python extension is the recommended editor for beginners.
-
The Python REPL (interactive shell) is great for quick experiments.
-
Python files use the
.py extension and are run with python filename.py.
-
Always use Python 3 — Python 2 is no longer maintained.
---
19. Next Chapter Recommendation
You've successfully installed Python and set up your development environment. Now it's time to write real code! In Chapter 3: Python Syntax and First Program, we'll learn Python's syntax rules, understand indentation, explore comments, master the
print()` function, and build a simple calculator. Let's code! 🚀