From 5efb942978f96660c7c8c870a7fa6d2300cf7a0a Mon Sep 17 00:00:00 2001 From: "scott.liu" Date: Wed, 31 May 2023 03:15:55 -0700 Subject: [PATCH] This is HW3 for May30 --- Scott_Submissions/HW3.py | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Scott_Submissions/HW3.py diff --git a/Scott_Submissions/HW3.py b/Scott_Submissions/HW3.py new file mode 100644 index 0000000..2a6a163 --- /dev/null +++ b/Scott_Submissions/HW3.py @@ -0,0 +1,63 @@ +# 1. Why does ord("😀") give a different number than 0xf09f9880, which is what the hexdump of the same character yields? +# +# The ord() function returns the Unicode code point of a character as an integer, which is 128512 for the smiley face. +# However, the hexdump command, returns the UTF-8 encoding of character. +# The hexadecimal representation of 128512 is 0x1F600. +# The UTF-8 encoding of the "😀" character is represented by the sequence of bytes F0 9F 98 80 in hexadecimal. +# UTF-8 is one of the encoding schemes used to represent Unicode characters as binary data. +# +# +# 8. Hypothetical Situation: If the dataframe is not formatted or cleaned, while the column storing the tomoto sales data isn't named exactly same as 'Tomato_Sales' +# It will raise a KeyError instead. +# +# +# 9. The solution of the hypothetical situation from my question 8: + +def read_tomato_sales(file_name): + data = pd.read_csv(file_name) + if not pd.api.types.is_numeric_dtype(data["Tomato_Sales"]): + raise TypeError("Tomato_Sales column should contain only numeric values") + total_sales = data.iloc[:, -1].sum() # This is the key + print(f"Total tomato sales: {total_sales}") +# read_tomato_sales("tomato_sales_error.csv") + + +# 10. Different human languages can use various character encodings such as UTF-8, UTF-16, or ASCII. +# While using bytes, you can work with raw data without worrying about specific encodings. +# This allows us to avoid encoding-related errors and ensures compatibility across different language texts. + +def encode_decode_data(): + print('-' * 20) + data = input("Enter a sentence using any language: ") + print('-' * 20) + print("The sentence you entered is: {}".format(data)) + print('-' * 20) + encoded_data = data.encode('utf-8') + print("This sentence after encoded is: {}".format(encoded_data)) + print('-' * 20) + byte_length = len(encoded_data) + print("The byte length of this sentence is: {}".format(byte_length)) + print('-' * 20) + decoded_data = encoded_data.decode() + print("This sentence after decoded is: {}".format(decoded_data)) + print('-' * 20) + +if __name__ == "__main__": + run_function = 'function1' + + if run_function == 'function1': + encode_decode_data() + elif run_function == 'function2': + read_tomato_sales(file_name) + + +# 2.5. REPL stands for Read-Eval-Print Loop. +# A REPL is an interactive programming environment that allows you to read input, evaluate expressions or statements, print output, and repeat the process. +# It provides a quick way to experiment with code, test small snippets, and get immediate feedback. +# The Python REPL allows you to interactively enter Python code and see the results immediately without needing to write a full script and execute it separately. + + +# 3. The '_' is a convention that holds the value of the last printed expression or the result of the last evaluated expression. +# The actual type of the "_" variable depends on the value assigned to it. + +