10
Day 1 homework
Karenzz edited this page 2023-03-30 23:54:56 +02:00
  1. What is the name of the international standard for floating-point representation?

  2. How many bits are the floating point numbers defined by that standard allowed to be?

  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?

    import pandas as pd
    import numpy as np
    
    # Generate two random 64-bit integers
    num1 = np.random.randint(0, 2**63 - 1)
    num2 = np.random.randint(0, 2**63 - 1)
    
    # Multiply them together using the normal Python multiplication operator
    print(f"num1 * num2 = {num1 * num2}")
    
    # Create a Pandas DataFrame with the generated numbers
    data = {'Number1': [num1], 'Number2': [num2]}
    df = pd.DataFrame(data)
    
    # Multiply the numbers together using the Pandas multiplication operator
    df['Product'] = df['Number1'] * df['Number2']
    
    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?

  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?

  8. Given this sample CSV file (tomato_sales.csv):

    Store,Region,Tomato_Sales
    Store1,East,300
    Store2,West,450
    Store3,North,500
    Store4,South,350
    Store5,East,400
    

    and this Python file to do a simple accounting function:

    def read_tomato_sales(file_name):
        data = pd.read_csv(file_name)
        total_sales = data["Tomato_Sales"].sum()  # This will raise an error
        print(f"Total tomato sales: {total_sales}")
    
    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.

  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.