Update page 'Day 1 Homework - Karen'

2023-03-30 22:40:03 +02:00
parent 0ee0850606
commit daadb1e077
+95 -2
@@ -9,9 +9,102 @@ The IEEE754 standard defines several different floating-point formats and the mo
**3. Does your computer support all those formats? Can you think of an easy way to check with only one line of Python?**
I can easily use `numpy.finfo` to return information about the floating-point data type used by my system, and it shows that my computer uses 64 bits by default and can also use float16 and float32. However, a quadruple-precision format is not supported by my system as it returns a warning.
![Floating-point formats](https://github.com/Karenzky/PythonBootcamp/blob/main/float%20formats.png)
![Floating-point formats](https://github.com/Karenzky/PythonBootcamp/blob/d4642e18971e820c07c72839abae6fc69f374037/float%20formats.png)
**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?**
```ruby
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)
```
It's because that the result of the multiplication operation on two integers is also an integer while the multiplication on two columns in pandas dataframe returns a new column of the same type as the input columns. To be more specific, the multiplication of two 64-bit integers (num1 & num2 here in this first example) can result in a number that requires more than 64 bits to represent, and the result reports a value error that high is out of bounds for int64 when I run this script; however, an integer column here using the pandas multiplication operator will have a maximum size of 64 bits and will cause a loss of precision.
**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 doesn't change, because the floating-point numbers are stored in a fixed number of bytes regardless of their specific value. In python, a floact object always takes up 8 bytes of memory and it will not change no matter for how large a Python object is.
![Floating-point change check](https://github.com/Karenzky/PythonBootcamp/blob/e70b05bcf39dc7efcf93c90322912ef9aff2a257/sys.png)
**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?**
Firstly, a large amount of US Dollars can be created by multiplying the numbers 1 through 1000000 by 10 billion that returns results ranging from 10 billion to 10 trillion dollars (I am sure it should be a large amount lol), and then Dollars column can be created using Pandas to store those dollar values.
```ruby
import pandas as pd
dollars = [n * 10**10 for n in range(1, 1000000)]
df = pd.DataFrame({'Dollars': dollars})
```
As for the next step to find the smallest number of dollars where adding one cent begins to yield wrong results, we can iterate over the DataFrame and add one cent to each dollar amount, and then compare the result to the original amount. The smallest value will be found once the new amount does not equal to the original amount plus one cent.
```ruby
for index, row in df.iterrows():
original_dollars = row['Dollars']
new_dollars = original_dollars + 0.01
if new_dollars != original_dollars + 0.01:
print(f"Smallest amount: {original_dollars:.2f}")
break
```
**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?**
* Representation: strings are represented using quotes, while bytes use a prefix of `b` followed by quotes.
* Encoding: strings can have a wide range of characters from various writing systems, while bytes use to store non-textual data or text encoded only in a specific character encoding such as UTF-8.
* Mutability: stings can not be modified so that a new string need to be created every time a change is needed. On the other hand, bytes are mutable and can be modified in place.
Emojis do change the result, supposing it is occurred because the encoding used by emoji. In UTF-8 encoding, it are represented using multiple bytes, so it may cause larger size.
```ruby
import sys
x = 'pizza'
y = b'pizza'
z = '🍕'
print(sys.getsizeof(x))
print(sys.getsizeof(y))
print(sys.getsizeof(z))
#The results are shown as
#>>pandas) PS C:\Users\Karen\pandas> & C:/Users/Karen/GTSP_DA_Intern/pandas/Scripts/python.exe c:/Users/Karen/pandas/hm.py
#54
#38
#80
```
![Emoji](https://github.com/Karenzky/PythonBootcamp/blob/cb586168d33823d79c54380925eaae2901c2c4c2/emoji.png)
**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.**
**8. Prevent that nuclear war by modifying `read_tomato_sales` to perform better data validation.**
**9. 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.**