Update page 'Day 1 homework'

2023-03-30 23:54:56 +02:00
parent 72ec10a3e6
commit d6c45add24
+29 -29
@@ -3,52 +3,52 @@
3. Does your computer support all those formats? Can you think of an easy way to check with only one line of Python? 3. Does your computer support all those formats? Can you think of an easy way to check with only one line of Python?
4. Why doesn't this code give the same answers to the two multiplications? 4. Why doesn't this code give the same answers to the two multiplications?
``` ```
import pandas as pd import pandas as pd
import numpy as np import numpy as np
# Generate two random 64-bit integers # Generate two random 64-bit integers
num1 = np.random.randint(0, 2**63 - 1) num1 = np.random.randint(0, 2**63 - 1)
num2 = np.random.randint(0, 2**63 - 1) num2 = np.random.randint(0, 2**63 - 1)
# Multiply them together using the normal Python multiplication operator # Multiply them together using the normal Python multiplication operator
print(f"num1 * num2 = {num1 * num2}") print(f"num1 * num2 = {num1 * num2}")
# Create a Pandas DataFrame with the generated numbers # Create a Pandas DataFrame with the generated numbers
data = {'Number1': [num1], 'Number2': [num2]} data = {'Number1': [num1], 'Number2': [num2]}
df = pd.DataFrame(data) df = pd.DataFrame(data)
# Multiply the numbers together using the Pandas multiplication operator # Multiply the numbers together using the Pandas multiplication operator
df['Product'] = df['Number1'] * df['Number2'] df['Product'] = df['Number1'] * df['Number2']
print(df) print(df)
``` ```
5. You can use `sys.getsizeof` to see how large a Python object is. Does the size of a Python floating-point number change as it gets larger? Why or why not? 5. You can use `sys.getsizeof` to see how large a Python object is. Does the size of a Python floating-point number change as it gets larger? Why or why not?
6. We already know that the precision of floating-point numbers decreases as their magnitude increases. Using Pandas, create a dataframe representing a large amount of US Dollars. What is the smallest number of dollars where adding one cent begins to yield wrong results? 6. We already know that the precision of floating-point numbers decreases as their magnitude increases. Using Pandas, create a dataframe representing a large amount of US Dollars. What is the smallest number of dollars where adding one cent begins to yield wrong results?
7. Name three practical differences between Python strings and bytes. Using `sys.getsizeof`, see how the sizes may be different. Do emoji change this answer? 7. Name three practical differences between Python strings and bytes. Using `sys.getsizeof`, see how the sizes may be different. Do emoji change this answer?
8. Given this sample CSV file (tomato_sales.csv): 8. Given this sample CSV file (tomato_sales.csv):
``` ```
Store,Region,Tomato_Sales Store,Region,Tomato_Sales
Store1,East,300 Store1,East,300
Store2,West,450 Store2,West,450
Store3,North,500 Store3,North,500
Store4,South,350 Store4,South,350
Store5,East,400 Store5,East,400
``` ```
and this Python file to do a simple accounting function: and this Python file to do a simple accounting function:
```python ```python
def read_tomato_sales(file_name): def read_tomato_sales(file_name):
data = pd.read_csv(file_name) data = pd.read_csv(file_name)
total_sales = data["Tomato_Sales"].sum() # This will raise an error total_sales = data["Tomato_Sales"].sum() # This will raise an error
print(f"Total tomato sales: {total_sales}") print(f"Total tomato sales: {total_sales}")
read_tomato_sales("tomato_sales_error.csv") read_tomato_sales("tomato_sales_error.csv")
``` ```
describe a hypothetical situation where modifying the CSV file to create a type confusion error could cause nuclear war due to AIs taking over the food supply chain. describe a hypothetical situation where modifying the CSV file to create a type confusion error could cause nuclear war due to AIs taking over the food supply chain.
9. Prevent that nuclear war by modifying `read_tomato_sales` to perform better data validation. 9. Prevent that nuclear war by modifying `read_tomato_sales` to perform better data validation.
10. Given today's conversation about character encodings, describe why you would want to use bytes instead of strings when processing data containing multiple human languages at the same time. Write a small Python function to demonstrate this. 10. Given today's conversation about character encodings, describe why you would want to use bytes instead of strings when processing data containing multiple human languages at the same time. Write a small Python function to demonstrate this.