# Python for Data Analysis - Chapter 2: Python Language Basics, IPython, and Jupyter Notebooks
## 1. Overview
This chapter explains:
- How to run Python interactively or from scripts
- Using IPython and Jupyter for interactive analysis
- The fundamental syntax and structures of Python
---
## 2. The Python Interpreter
- **Interpreter**: Reads and executes Python code.
- Run interactively by typing `python` in the terminal.
- Run scripts with: `python script.py`
- In data analysis, interactive sessions are often more useful than running entire scripts.
---
## 3. IPython Basics
- **Enhanced Python Shell** with extra features for exploration.
- **Tab Completion**: Suggests variables/methods.
- **Introspection (`?`)**: Shows documentation for functions and objects.
- **Magic Commands**:
- `%time` to time execution
- `%matplotlib inline` for inline plots in notebooks
- `%run script.py` to run external scripts
---
## 4. Jupyter Notebooks
- Web-based interface for writing and running Python in cells.
- Combine code, results, and markdown documentation.
- Common for exploratory data analysis (EDA).
---
## 5. Python Language Basics
### Variables
- Assign with `=`
- Dynamic typing - no need to declare variable type
### Scalar Types
- `int`, `float`, `str`, `bool`, and `None`
- Example:
```python
x = 10
y = 3.14
z = "hello"
flag = True
nothing = None
```
### Strings
- Use single or double quotes
- Triple quotes for multi-line strings
- Common methods: `.upper()`, `.lower()`, `.replace()`
### Control Flow
- `if/elif/else` for conditional execution
- `for` loops for iteration
- `while` loops for repeated execution until a condition is false
### Functions
- Define with `def`
- Return values with `return`
- Default arguments possible
---
## 6. Project Applications
- Use IPython/Jupyter to test data cleaning functions.
- Write Python scripts to automate repetitive analysis steps.
- Apply loops and conditionals to process datasets.
---
## 7. Exercises
**From Book Concepts:**
1. Start IPython and try tab completion for a list object.
2. Use `%time` to measure a simple calculation.
3. Write a function to compute the mean of a list.
4. Create a Jupyter Notebook that prints "Hello, Data Analysis".
5. Use `if/else` to classify a number as positive, negative, or zero.
**Extra Practice:**
- Create a Python script that reads a CSV file and prints the first 5 rows.
- Make a loop that prints all even numbers from 1 to 50.
---
## 8. Quick Recap
- IPython/Jupyter speed up interactive data exploration.
- Python basics like variables, loops, and functions form the foundation for pandas and NumPy.