1.1 Setting Up the Python Data Science Lab
Python Data Science Lab Setup
Before learning Python for Data Science and Automation, you need a reliable development environment. A properly configured environment allows you to write, execute, test, and manage Python programs and third-party libraries efficiently.
- What Python environments are
- Installing Python with Anaconda
- Understanding Anaconda and Anaconda Navigator
- Working with Jupyter Notebook
- Installing packages with
pip - Managing environments with
conda - Creating an isolated Data Science environment
- Verifying the installation
1. Why Do We Need a Python Environment?
A Python environment provides the tools required to write and run Python programs. In Data Science, a project normally requires additional libraries such as NumPy, pandas, Matplotlib, and Jupyter.
Different projects may require different versions of Python or different versions of packages. Environment management prevents these dependencies from interfering with one another.
| Component | Purpose |
|---|---|
| Python | Programming language used to write and execute Python programs. |
| Anaconda | Python distribution designed especially for scientific computing and Data Science. |
| Jupyter Notebook | Interactive environment for writing and executing Python code together with explanations and output. |
| pip | Python package installer used to install Python packages. |
| conda | Package and environment management system commonly used with the Anaconda ecosystem. |
2. What is Anaconda?
Anaconda is a Python distribution designed for data science, scientific computing, and related workflows.
It provides Python together with tools and packages commonly required for scientific and Data Science work.
Why Is Anaconda Popular for Data Science?
- Simplifies Python installation.
- Provides package and environment management.
- Works well with scientific computing libraries.
- Can be used with Jupyter Notebook.
- Helps maintain isolated project environments.
Beginners who are specifically learning Python for Data
Science can use Anaconda as a convenient starting environment.
Experienced developers may also choose a standard Python
installation with venv and pip.
3. Anaconda vs Anaconda Navigator vs conda
These terms are related but they do not mean exactly the same thing.
| Term | Meaning |
|---|---|
| Anaconda | A Python distribution containing Python and a collection of tools and packages for data science. |
| Anaconda Navigator | A graphical interface for launching applications and managing environments. |
| conda | A command-line package and environment manager. |
4. Installing Anaconda
Download the appropriate Anaconda distribution for your operating system and follow the installation instructions provided by the installer.
General Installation Steps
- Download the appropriate Anaconda installer.
- Start the installer.
- Accept the licence terms.
- Select the installation location.
- Complete the installation.
- Open Anaconda Navigator or the Anaconda Prompt/terminal.
- Verify that the environment is working.
Avoid installing multiple independent Python distributions
without understanding how their PATH and environment settings
interact. This can cause commands such as
python and pip to refer to different
installations.
5. Verify the Python Installation
After installation, verify that Python is available from the command line.
Check Python Version
python --version
On some systems, the Python launcher may be accessed using:
python3 --version
A successful command should display the installed Python version.
Check conda
conda --version
If the command is not recognised, check whether Anaconda or your Python installation was installed correctly and whether the appropriate environment or terminal is being used.
6. What is Jupyter Notebook?
Jupyter Notebook is an interactive environment that allows you to combine executable code, output, explanations, mathematical expressions, and visualisations in a notebook document.
It is widely used for experimentation, Data Science, education, analysis, visualisation, and demonstrations.
Why Use Jupyter Notebook?
- Execute code in small sections.
- Immediately view the output.
- Combine code and explanations.
- Display tables and visualisations.
- Experiment with datasets interactively.
- Document the reasoning behind an analysis.
7. Understanding a Jupyter Notebook
A Jupyter Notebook is organised into cells. Each cell can contain a particular type of content.
| Cell Type | Purpose |
|---|---|
| Code | Contains executable Python code. |
| Markdown | Contains explanations, headings, lists, links, and formatted documentation. |
| Raw | Contains content that is not interpreted as standard executable or Markdown content. |
Example Code Cell
name = "Alex"
print("Welcome,", name)
Example Markdown Cell
# Python Data Science
This notebook demonstrates
basic Python operations.
8. Launching Jupyter Notebook
If Jupyter is installed in the active environment, it can generally be launched from a terminal using:
jupyter notebook
Depending on the installation, the command may open the Jupyter interface in a web browser.
Alternative
If using Anaconda Navigator, Jupyter applications can also be launched through its graphical interface.
9. Create Your First Jupyter Notebook
- Launch Jupyter Notebook.
- Navigate to your working folder.
- Create a new Python notebook.
- Rename the notebook.
- Create a code cell.
- Enter a Python statement.
- Run the cell.
First Program
print("Hello, Data Science!")
The output should be:
Hello, Data Science!
10. What is pip?
pip is the package installer commonly used for installing Python packages from the Python Package Index and other supported package sources.
Check pip
pip --version
Install a Package
pip install pandas
Install Multiple Packages
pip install numpy pandas matplotlib
Upgrade a Package
pip install --upgrade pandas
Remove a Package
pip uninstall pandas
View Installed Packages
pip list
11. What is conda?
conda is a package and environment management system. It can create isolated environments and install packages into those environments.
Check conda
conda --version
List Environments
conda env list
List Packages
conda list
12. Why Use Virtual Environments?
A virtual environment creates an isolated space for a project's Python interpreter and packages.
Consider two projects:
- Project A requires one version of a package.
- Project B requires another version.
Installing everything globally can create dependency conflicts. Separate environments help keep project dependencies isolated.
| Without Environments | With Environments |
|---|---|
| Packages may conflict. | Project dependencies remain isolated. |
| Difficult to reproduce projects. | Easier to reproduce project setups. |
| Global installation can become cluttered. | Each project can have its own environment. |
13. Create a Data Science Environment with conda
Create an isolated environment specifically for this course.
conda create --name datascience python=3.12
The environment can then be activated.
conda activate datascience
Once activated, packages installed into the environment are associated with that environment.
Deactivate the Environment
conda deactivate
Remove an Environment
conda remove --name datascience --all
Do not delete an environment simply because a package is missing. First activate the environment and install the required dependency.
14. Install Essential Data Science Packages
After activating the environment, install the libraries required for the upcoming modules.
conda activate datascience
Packages can be installed with conda:
conda install numpy pandas matplotlib seaborn jupyter
Packages can also be installed using pip when appropriate:
pip install numpy pandas matplotlib seaborn jupyter
In a managed conda environment, understand which package manager you are using and avoid randomly mixing package sources when dependency compatibility is important.
15. Verify the Data Science Environment
Create a small Python program to confirm that the important libraries can be imported.
import numpy
import pandas
import matplotlib
import seaborn
print("NumPy:", numpy.__version__)
print("pandas:", pandas.__version__)
print("Matplotlib:", matplotlib.__version__)
print("Seaborn:", seaborn.__version__)
If the program executes successfully, the libraries are available in the current Python environment.
16. Find Which Python Installation Is Running
When multiple Python installations exist, checking the executable location can help identify which interpreter is active.
Inside Python
import sys
print(sys.executable)
This displays the path to the Python interpreter being used by the current program or notebook.
If a package appears to be installed but Python reports
ModuleNotFoundError, one of the first things to
check is whether the package was installed into the same
environment whose Python interpreter is running the code.
17. pip vs conda
| Feature | pip | conda |
|---|---|---|
| Primary Purpose | Python package installation. | Package and environment management. |
| Python Packages | Yes. | Yes. |
| Environment Management |
Commonly used with tools such as
venv; pip itself is not primarily
an environment manager.
|
Built-in environment management. |
| Typical Command |
pip install package
|
conda install package
|
| Common Usage | General Python development. | Data Science and scientific computing workflows where conda environments are useful. |
pip and conda are not simply two
different spellings of the same command. They are different
tools with overlapping package-management capabilities.
18. Alternative: Google Colab
Google Colab provides a hosted notebook environment that allows learners to execute Python code without setting up a complete local Python environment.
It is particularly useful when:
- You cannot install software on your computer.
- You want to experiment quickly.
- You want to share notebooks.
- You want a browser-based development environment.
A local Anaconda/Jupyter setup gives you direct control over your environment and files. A hosted notebook such as Colab reduces local setup requirements.
19. First Data Science Lab Exercise
Create a notebook and execute the following program.
import pandas as pd
data = {
"Name": ["Alex", "Jordan", "Taylor"],
"Score": [82, 91, 76]
}
df = pd.DataFrame(data)
print(df)
This simple exercise confirms that Python can import pandas, create structured data, and display a DataFrame.
20. Common Setup Errors and Solutions
| Error / Problem | Possible Cause | What to Check |
|---|---|---|
python not recognised
|
Python is not available in the current PATH or environment. | Verify installation and active environment. |
conda not recognised
|
Conda is unavailable in the current shell. | Use an appropriate Anaconda terminal or verify installation. |
ModuleNotFoundError
|
Package may not be installed in the active environment. | Check the environment and install the package. |
| Wrong Python version | Another Python installation may be active. |
Check python --version and
sys.executable.
|
| Jupyter cannot find a package | Notebook may be using a different environment. | Verify the Python interpreter used by the notebook. |
21. Python Environment Best Practices
- Create separate environments for major projects.
- Keep project dependencies documented.
- Avoid unnecessary global package installations.
- Check the active environment before installing packages.
- Keep Python and packages reasonably up to date.
- Avoid blindly copying installation commands from unknown websites.
- Use official documentation when troubleshooting package installation.
- Test the environment before beginning a major project.
22. Interview Questions
Q1. What is Anaconda?
View Answer
Anaconda is a Python distribution commonly used for Data Science and scientific computing. It provides Python together with tools and packages useful for these workflows.
Q2. What is Jupyter Notebook?
View Answer
Jupyter Notebook is an interactive environment in which users can combine executable code, output, explanations, and visualisations within notebook documents.
Q3. What is the difference between pip and conda?
View Answer
pip is primarily a Python package installer, whereas conda provides package and environment management and is commonly used to manage isolated Data Science environments.
Q4. Why are virtual environments important?
View Answer
They isolate project dependencies so that different projects can use different package versions without unnecessarily interfering with one another.
Q5. How can you determine which Python interpreter is being used?
View Answer
Use sys.executable from within Python to
display the path of the active Python interpreter.
23. Examination Questions
Multiple Choice Questions
Q1. Which tool is commonly used to install Python packages?
- pip
- HTML
- SQL
- CSS
Answer: A — pip
Q2. Which tool provides environment management in the Anaconda ecosystem?
- conda
- JupyterLab HTML
- pipfile
Answer: A — conda
Q3. Which environment is especially useful for interactive Data Science analysis?
- Jupyter Notebook
- Text editor only
- HTML validator
- Image editor
Answer: A — Jupyter Notebook
Short Answer Questions
- Define a Python virtual environment.
- State two advantages of using Anaconda for Data Science.
- Differentiate between pip and conda.
- What is the purpose of Jupyter Notebook?
- Write the command used to create a conda environment.
- Write the command used to activate a conda environment.
24. Practical Lab Task
Task: Build Your Data Science Environment
- Install Anaconda.
- Verify Python.
- Verify conda.
-
Create an environment named
datascience. - Activate the environment.
- Install NumPy, pandas, Matplotlib, Seaborn, and Jupyter.
- Launch Jupyter Notebook.
-
Create a notebook named
01_environment_setup.ipynb. - Import the installed libraries.
- Display their versions.
- Create a simple pandas DataFrame.
- Save the notebook.
25. Lab Completion Checklist
| Task | Completed |
|---|---|
| Anaconda installed | ☐ |
| Python version verified | ☐ |
| conda version verified | ☐ |
| Data Science environment created | ☐ |
| Environment activated | ☐ |
| NumPy installed | ☐ |
| pandas installed | ☐ |
| Matplotlib installed | ☐ |
| Seaborn installed | ☐ |
| Jupyter installed | ☐ |
| Jupyter Notebook launched | ☐ |
| First notebook created | ☐ |
| Libraries imported successfully | ☐ |
| First DataFrame created | ☐ |
26. Python Environment Quick Cheatsheet
| Task | Command |
|---|---|
| Check Python |
python --version
|
| Check conda |
conda --version
|
| Check pip |
pip --version
|
| List conda environments |
conda env list
|
| Create environment |
conda create --name datascience python=3.12
|
| Activate environment |
conda activate datascience
|
| Deactivate environment |
conda deactivate
|
| Install package with pip |
pip install package_name
|
| Install package with conda |
conda install package_name
|
| List pip packages |
pip list
|
| List conda packages |
conda list
|
| Launch Jupyter Notebook |
jupyter notebook
|
27. Key Takeaways
- Python is the programming language used throughout this course.
- Anaconda provides a convenient Python distribution for Data Science workflows.
- Jupyter Notebook provides an interactive environment for code, documentation, and analysis.
- pip is primarily a Python package installer.
- conda manages packages and isolated environments.
- Virtual environments help prevent dependency conflicts.
- Always verify which Python interpreter and environment are actually running your code.
Before debugging your Python code, first make sure you are running the code in the environment where the required packages are installed.