Data science book1
Data science book1
Since you are just starting out, we’ll begin with foundational topics and gradually build up to
more advanced concepts.
---
Do you want to start with Python basics or dive into a specific module first?
Great! Let’s dive into **Python** for data science. We’ll cover the basics first, and then move
into the libraries essential for data manipulation and analysis.
Example:
```python
x = 10 # integer
y = 3.14 # float
name = "Liz" # string
is_happy = True # boolean
```
**If-Else Statement**:
```python
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
```
**For Loop**:
Loops help you execute the same block of code repeatedly.
```python
for i in range(5): # Loops 5 times, from 0 to 4
print(i)
```
**While Loop**:
```python
count = 0
while count < 5:
print(count)
count += 1 # Increment count
```
Example:
```python
def greet(name):
return "Hello, " + name
print(greet("Liz"))
```
**List Example**:
```python
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits.append("orange") # Add item to the list
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
```
**Dictionary Example**:
```python
student = {"name": "Liz", "age": 30, "country": "Uganda"}
print(student["name"]) # Output: Liz
```
---
# Creating arrays
arr = np.array([1, 2, 3, 4])
print(arr)
# Operations on arrays
arr2 = arr * 2 # Multiplies each element by 2
print(arr2) # Output: [2, 4, 6, 8]
# Accessing elements
print(matrix[0, 1]) # Output: 2 (element in row 0, column 1)
```
```python
import pandas as pd
df = pd.DataFrame(data)
# Descriptive statistics
print(df.describe()) # Shows stats like mean, min, max
```
---
Would you like some specific exercises to practice, or shall we move forward with data
exploration using Pandas?
Great! Here are a few exercises to help you practice Python basics, NumPy, and Pandas. These
will solidify your understanding and prepare you for more complex data manipulation tasks.
2. **Control Structures**
Write a Python function `check_number` that takes an integer as input and checks if the
number is positive, negative, or zero. Use `if-else` statements.
```python
def check_number(num):
# Your code here
```
3. **Loops**
Write a `for` loop to print the squares of numbers from 1 to 10.
4. **Functions**
Create a function `greet_user` that takes a name as input and returns a greeting message.
```python
def greet_user(name):
# Your code here
```
2. **Dictionaries**
Create a dictionary with the following key-value pairs:
- 'name': 'Liz'
- 'age': 30
- 'country': 'Uganda'
2. **Array Reshaping**
- Create a 1D array with numbers from 1 to 12.
-