1.9 KiB
1.9 KiB
TASK: Create a CSV 'test_data.csv' with 100 rows of random numbers, calculate mean and standard deviation using Python, and save results to 'stats_summary.txt'.
----------------------------------------
Loading...
┌─── Python Source Code ─────────────────────────────────────
│ 1 | import csv, random, statistics
│ 2 | # Generate CSV data
│ 3 | rows = [[random.uniform(0, 100) for _ in range(10)] for _ in range(100)]
│ 4 | with open('test_data.csv', 'w', newline='') as f:
│ 5 | writer = csv.writer(f)
│ 6 | writer.writerows(rows)
│ 7 | # Calculate mean and standard deviation
│ 8 | flat_list = [item for sublist in rows for item in sublist]
│ 9 | mean_value = statistics.mean(flat_list)
│ 10 | std_dev = statistics.stdev(flat_list)
│ 11 | # Save results
│ 12 | with open('stats_summary.txt', 'w') as f:
│ 13 | f.write(f'Mean: {mean_value}\n')
│ 14 | f.write(f'Standard Deviation: {std_dev}\n')
└────────────────────────────────────────────────────────────
The CSV file 'test_data.csv' with 100 rows of random numbers has been created. The mean and standard deviation have been calculated and saved to 'stats_summary.txt'.
----------------------------------------
Loading...
┌─── Python Source Code ─────────────────────────────────────
│ 1 | import csv, random, statistics
│ 2 | # Generate CSV data
│ 3 | rows = [[random.uniform(0, 100) for _ in range(10)] for _ in range(100)]
│ 4 | with open('test_data.csv', 'w', newline='') as f:
│ 5 | writer = csv.writer(f)
│ 6 | writer.writerows(rows)
│ 7 | # Calculate mean and standard deviation
│ 8 | flat_list = [item for sublist in rows for item in sublist]
│ 9 | mean_value = statistics.mean(flat_list)
│ 10 | std_dev = statistics.stdev(flat_list)
│ 11 | # Save results
│ 12 | with open('stats_summary.txt', 'w') as f:
│ 13 | f.write(f'Mean: {mean_value}\n')
│ 14 | f.write(f'Standard Deviation: {std_dev}\n')
└────────────────────────────────────────────────────────────
The CSV file 'test_data.csv' with 100 rows of random numbers has been created. The mean and standard deviation have been calculated and saved to 'stats_summary.txt'.