Spaces:
Sleeping
Sleeping
| # Python Example: Data Processing and File Operations | |
| import json | |
| # Sample data | |
| data = [ | |
| {"name": "Alice", "age": 25, "city": "New York"}, | |
| {"name": "Bob", "age": 30, "city": "San Francisco"}, | |
| {"name": "Charlie", "age": 35, "city": "Boston"}, | |
| {"name": "Diana", "age": 28, "city": "Chicago"}, | |
| {"name": "Eve", "age": 22, "city": "New York"} | |
| ] | |
| print("=== Data Processing Demo ===\n") | |
| # Filter people over 27 | |
| older_people = [person for person in data if person["age"] > 27] | |
| print(f"People over 27: {len(older_people)}") | |
| # Group by city | |
| cities = {} | |
| for person in data: | |
| city = person["city"] | |
| if city not in cities: | |
| cities[city] = [] | |
| cities[city].append(person) | |
| print("\nPeople by city:") | |
| for city, people in cities.items(): | |
| print(f" {city}: {len(people)} people") | |
| # Calculate average age | |
| total_age = sum(person["age"] for person in data) | |
| avg_age = total_age / len(data) | |
| print(f"\nAverage age: {avg_age:.2f}") | |
| # Find youngest and oldest | |
| youngest = min(data, key=lambda x: x["age"]) | |
| oldest = max(data, key=lambda x: x["age"]) | |
| print(f"Youngest: {youngest['name']} ({youngest['age']})") | |
| print(f"Oldest: {oldest['name']} ({oldest['age']})") |