Update page 'Day 1 homework - Xuanyi Wang (Sloetjes)'
@@ -59,7 +59,7 @@ print(df)
|
||||
|
||||
**`num1`** and **`num2` **are both 64-bit integers, which means that **` num1 * num2`** is performed using 64-bit integer arithmetic. However,**`df[‘Number1’] `** and **`df[‘Number2’] `** are Pandas Series objects and they are 64-bit floating point numbers so **`df[‘Number1’] * df[‘Number2’] `** is performed using floating-point arithmetic which involves approximations and rounding errors.
|
||||
|
||||
#### 1. 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?
|
||||
|
||||
**The size of a Python floating-point number does not change as it gets larger.**
|
||||
|
||||
@@ -70,14 +70,14 @@ Regardless of the magnitude of the float, the number of bytes used to represent
|
||||
|
||||
However, it's worth noting that as the magnitude of a floating-point number grows larger, the number of significant digits it can represent decreases. This is due to the limited precision of floating-point arithmetic, and can result in rounding errors when performing calculations with very large or very small floating-point numbers.
|
||||
|
||||
#### 2. 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?
|
||||
|
||||
Assuming double-precision floating-point numbers (64 bits), the smallest number of dollars that can be represented is $0.01 divided by 2 to the power of 52 (the number of bits used to represent the fraction part of a double-precision number). This equals approximately **$0.0000000000002384**.
|
||||
|
||||
Therefore, any calculation involving adding 0.01 to a dollar amount smaller than **$0.0000000000002384** may yield wrong results due to the limited precision of floating-point arithmetic.
|
||||
|
||||
|
||||
#### 3. 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?
|
||||
|
||||
** Encoding**: Python strings are used to represent text data, while bytes are used to represent binary data. Strings are typically encoded using a character encoding such as UTF-8 or ASCII, which maps each character to a sequence of bytes, while bytes represent raw binary data that may not necessarily correspond to any particular character encoding.
|
||||
|
||||
@@ -113,7 +113,7 @@ Size of s2: 104
|
||||
```
|
||||
|
||||
|
||||
#### 4. Given this sample CSV file (tomato_sales.csv):
|
||||
#### 8. Given this sample CSV file (tomato_sales.csv):
|
||||
```
|
||||
Store,Region,Tomato_Sales
|
||||
Store1,East,300
|
||||
@@ -141,7 +141,7 @@ describe a hypothetical situation where modifying the CSV file to create a type
|
||||
A hypothetical situation where modifying the CSV file would create a type confusion error is that if the initial information that has been written into the CSV is modified through **string slicing**, for example, “Store4South350” sliced into “Store3, South, 350”. Though “350” looks like a numeric input, it remains a string. And this kind of input would cause type error in the read\_tomato\_sales().
|
||||
|
||||
|
||||
#### 1. 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.
|
||||
|
||||
```
|
||||
def read_tomato_sales(file_name):
|
||||
@@ -152,7 +152,7 @@ def read_tomato_sales(file_name):
|
||||
read_tomato_sales("tomato_sales_error.csv")
|
||||
```
|
||||
|
||||
#### 2. 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.
|
||||
|
||||
When processing data containing multiple human languages at the same time, it's often better to use bytes instead of strings. The reason for this is that **bytes represent the raw data in its original form, while strings represent a specific encoding of the data.** When working with multiple languages, it's possible that the data may contain characters that are not supported by the encoding used by the string. **This can result in data loss or incorrect character conversion.**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user