PYTHON BASICS • LESSON 19

Working with Python Data

Learn how to combine Python's core data structures, loops, conditions, and functions to inspect, filter, transform, and summarize simple datasets.

1. Working with Data

Data analytics often starts with raw values. Python can store these values in lists, dictionaries, tuples, and sets.

sales = [12000, 18000, 9500, 22000]

print(sales)

A list is useful when you need an ordered collection of values.

CHECK YOUR UNDERSTANDING

Which Python structure is used in the sales example?

2. Inspecting a Dataset

Before analyzing data, it is useful to inspect its size and individual values.

sales = [12000, 18000, 9500, 22000]

print(len(sales))
print(sales[0])

len() tells us how many values are in the list.

CHECK YOUR UNDERSTANDING

What does len(sales) return for [12000, 18000, 9500, 22000]?

3. Loop Through Data

A for loop allows us to process every value in a dataset.

sales = [12000, 18000, 9500]

for sale in sales:
    print(sale)

CHECK YOUR UNDERSTANDING

How many times will print(sale) execute?

4. Summarizing Data

Python provides useful functions such as sum(),min(), and max().

sales = [12000, 18000, 9500, 22000]

print(sum(sales))
print(min(sales))
print(max(sales))

CHECK YOUR UNDERSTANDING

What is the total sales amount?

CHECK YOUR UNDERSTANDING

What is the correct total of [12000, 18000, 9500, 22000]?

5. Calculating an Average

A simple average can be calculated by dividing the total by the number of values.

sales = [10000, 20000, 30000]

average = sum(sales) / len(sales)

print(average)

CHECK YOUR UNDERSTANDING

What is the average of 10000, 20000 and 30000?

Fill in the Blank

Average = sum(data) / ___(data)

6. Filtering Data

Conditions allow us to select only values that meet a specific rule.

sales = [50000, 120000, 75000, 150000]

for sale in sales:
    if sale >= 100000:
        print(sale)

CHECK YOUR UNDERSTANDING

Which values will be printed?

7. Counting Matching Records

We can use a counter to count how many records satisfy a condition.

sales = [50000, 120000, 75000, 150000]

count = 0

for sale in sales:
    if sale >= 100000:
        count += 1

print(count)

CHECK YOUR UNDERSTANDING

How many sales are at least ₹100,000?

8. Creating a Filtered Dataset

sales = [50000, 120000, 75000, 150000]

high_sales = []

for sale in sales:
    if sale >= 100000:
        high_sales.append(sale)

print(high_sales)

The new list contains only the records that satisfy the condition.

CHECK YOUR UNDERSTANDING

What will high_sales contain?

9. Working with Dictionary Records

Dictionaries are useful for representing individual records with named fields.

student = {
    "name": "Aman",
    "course": "Python",
    "score": 88
}

print(student["score"])

CHECK YOUR UNDERSTANDING

What value will student['score'] return?

10. A Dataset as a List of Dictionaries

A common way to represent simple tabular data in Python is a list containing dictionaries.

students = [
    {"name": "Aman", "score": 88},
    {"name": "Riya", "score": 92},
    {"name": "Karan", "score": 76}
]

for student in students:
    print(student["name"], student["score"])

CHECK YOUR UNDERSTANDING

How many student records are in the dataset?

11. Filtering Records

students = [
    {"name": "Aman", "score": 88},
    {"name": "Riya", "score": 92},
    {"name": "Karan", "score": 76}
]

for student in students:
    if student["score"] >= 80:
        print(student["name"])

CHECK YOUR UNDERSTANDING

Which students will be printed?

12. Building Totals with a Loop

sales = [10000, 20000, 15000]

total = 0

for sale in sales:
    total += sale

print(total)

The accumulator pattern is useful when you need to build a total step by step.

CHECK YOUR UNDERSTANDING

What is the final value of total?

13. Combining Functions and Data

def average(values):
    return sum(values) / len(values)

scores = [80, 90, 70, 100]

result = average(scores)

print(result)

CHECK YOUR UNDERSTANDING

What is the average score?

14. Basic Data Cleaning

Raw data can contain unwanted spaces or inconsistent text. Python string methods can help clean values before analysis.

city = "  Dehradun  "

clean_city = city.strip()

print(clean_city)

strip() removes whitespace from the beginning and end of a string.

CHECK YOUR UNDERSTANDING

Which method removes leading and trailing whitespace?

MINI CHALLENGE

Find High-Value Transactions

You have these transactions:

transactions = [45000, 125000, 80000, 175000, 95000]

Count how many transactions are at least ₹100,000.

CHECK YOUR UNDERSTANDING

How many transactions are at least ₹100,000?

Fill in the Blank

Use ___(data) to find the number of values in a dataset.

FINAL CHALLENGE

Analyze a Sales Dataset

A business has the following sales values:

sales = [85000, 125000, 140000, 45000, 210000]

You want to count the number of transactions greater than or equal to ₹100,000.

CHECK YOUR UNDERSTANDING

How many transactions meet the ₹100,000 threshold?

15. Think Like a Data Analyst

A Python data workflow often follows a simple pattern:

  1. Load or create the data.
  2. Inspect the data.
  3. Clean the data.
  4. Filter records.
  5. Calculate useful metrics.
  6. Interpret the result.

Lesson 19 Recap

  • ✓ Lists can store collections of analytical values.
  • ✓ len() helps inspect dataset size.
  • ✓ Loops allow repeated processing of records.
  • ✓ sum(), min(), and max() summarize numeric data.
  • ✓ Conditions can filter records.
  • ✓ Dictionaries can represent individual records.
  • ✓ Lists of dictionaries can represent simple datasets.
  • ✓ Functions make repeated analysis reusable.
  • ✓ Basic cleaning improves data quality.