Compare commits
32
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74545a6ad1 | ||
|
|
b6be9c9488 | ||
|
|
716e254905 | ||
|
|
161a4e28db | ||
|
|
5af112ebae | ||
|
|
5d36a06987 | ||
|
|
b4f174be47 | ||
|
|
46acaf2e75 | ||
|
|
28c277f369 | ||
|
|
02146e348d | ||
|
|
9df129681d | ||
|
|
a566adc1df | ||
|
|
aa1a8fee93 | ||
|
|
285914f044 | ||
|
|
da2c779bcd | ||
|
|
7259509d9e | ||
|
|
b085e13c4e | ||
|
|
59e84426af | ||
|
|
97f93ebd8e | ||
|
|
35c8889e77 | ||
|
|
dca7e9d599 | ||
|
|
ce856e5626 | ||
|
|
072032e80e | ||
|
|
235e7b5268 | ||
|
|
22fa096271 | ||
|
|
13eb12bf69 | ||
|
|
efa189d0b6 | ||
|
|
111f551a39 | ||
|
|
e0fd81dc1d | ||
|
|
420194ef54 | ||
|
|
632448c249 | ||
|
|
c3405d56e5 |
@@ -0,0 +1,28 @@
|
|||||||
|
# Question 1:
|
||||||
|
# The international standard for floating-point representation is
|
||||||
|
# called the "IEEE 754 Standard for Floating-Point Arithmetic."
|
||||||
|
|
||||||
|
# Question 2:
|
||||||
|
# 32-bit and 64-bit formats are the most widely used ones in practice.
|
||||||
|
|
||||||
|
# Question 3:
|
||||||
|
# My computer supports all these formats
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
supported_formats = np.finfo(np.float32).bits, np.finfo(np.float64).bits
|
||||||
|
print(f"Your computer supports the following floating-point formats: {supported_formats}")
|
||||||
|
|
||||||
|
|
||||||
|
# Question 4:
|
||||||
|
# Using the Pandas multiplication operator (df['Number1'] * df['Number2'])
|
||||||
|
# , the result is automatically converted to a floating-point data type.
|
||||||
|
# On the other hand, when I multiply the numbers using the normal Python
|
||||||
|
# multiplication operator (num1 * num2), the result remains an integer
|
||||||
|
# data type.
|
||||||
|
|
||||||
|
# Question 5:
|
||||||
|
# The size of a floating-point number in memory is primarily determined
|
||||||
|
# by the data type and not the actual value it represents. So the size
|
||||||
|
# of a Python floating-point number does not change as it gets larger.
|
||||||
|
import sys
|
||||||
|
print(sys.getsizeof(70))
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
print('Hi')
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
# A quick brain teaser
|
||||||
|
print('What has a head and a tail but no body?')
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# A coin
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
import sys
|
||||||
|
print(sys.float_info)
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
{\rtf1\ansi\ansicpg1252\cocoartf2580
|
||||||
|
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 HelveticaNeue;\f1\fnil\fcharset0 Menlo-Regular;}
|
||||||
|
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue0;}
|
||||||
|
{\*\expandedcolortbl;;\cssrgb\c0\c0\c0\c87059;\cssrgb\c0\c0\c0\c3922;}
|
||||||
|
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
|
||||||
|
\deftab720
|
||||||
|
\pard\pardeftab720\partightenfactor0
|
||||||
|
|
||||||
|
\f0\fs32 \cf2 \expnd0\expndtw0\kerning0
|
||||||
|
1. What is the name of the international standard for floating-point representation?\
|
||||||
|
\
|
||||||
|
The IEEE-754 standard describes floating-point formats, a way to represent real numbers in hardware.\
|
||||||
|
\
|
||||||
|
\
|
||||||
|
2. How many bits are the floating point numbers defined by that standard allowed to be?\
|
||||||
|
\
|
||||||
|
The floating-point numbers defined by the international standard can be allowed to be of different lengths depending on the specific interchange format. For binary interchange, the formats can be 16 bits, 32 bits, 64 bits, and any multiple of 32 bits \uc0\u8805 128. For decimal interchange, the formats can be any multiple of 32 bits.\
|
||||||
|
\
|
||||||
|
\
|
||||||
|
3. Does your computer support all those formats? Can you think of an easy way to check with only one line of Python?\
|
||||||
|
\
|
||||||
|
import sys\
|
||||||
|
print(sys.float_info)\
|
||||||
|
\
|
||||||
|
scott@MacBook-Pro-6 python-bootcamp % python3 Floating_Point_Formats.py \
|
||||||
|
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\
|
||||||
|
\
|
||||||
|
\
|
||||||
|
\pard\pardeftab720\sa320\partightenfactor0
|
||||||
|
\cf2 4. Why doesn't this code give the same answers to the two multiplications?\
|
||||||
|
Because they are different operations. The normal Python multiplication operator will multiply two numbers together and return the product. The Pandas multiplication operator, on the other hand, performs element-wise multiplication on the corresponding columns in a DataFrame. In other words, it multiplies the first element in the first column with the first element in the second column, the second element in the first column with the second element in the second column, and so on. \
|
||||||
|
Floating-point arithmetic has a limited precision due to the finite number of bits used to represent floating-point numbers in binary format. This can lead to rounding errors and loss of precision in arithmetic operations, particularly with very large or very small numbers.\
|
||||||
|
\
|
||||||
|
5. You can use
|
||||||
|
\f1\fs27\fsmilli13600 \cf2 \cb3 sys.getsizeof
|
||||||
|
\f0\fs32 \cf2 \cb1 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. This is because Python uses a fixed number of bytes to represent floating-point numbers, regardless of their value.}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
# 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
|
||||||
|
# Strings are created using Unicode character points.
|
||||||
|
# While bytes are used to represent actual bytes in memory, which can be encoded using ASCII or UTF-8.
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
# Strings are usually used to represent text.
|
||||||
|
# While bytes are used to represent binary data.
|
||||||
|
|
||||||
|
# Memory Size
|
||||||
|
# Strings take up more memory than bytes, because they are Unicode-based and can contain any character.
|
||||||
|
# While bytes are limited to binary data.
|
||||||
|
|
||||||
|
|
||||||
|
print("---------------------------------------------")
|
||||||
|
print("This is the Memory Size test print out:")
|
||||||
|
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
my_string = "Hello, world!"
|
||||||
|
my_bytes = b'\x48\x65\x6c\x6c'
|
||||||
|
|
||||||
|
print(sys.getsizeof(my_string)) # Prints 62
|
||||||
|
print(sys.getsizeof(my_bytes)) # Prints 37
|
||||||
|
|
||||||
|
print()
|
||||||
|
print('Test the emoji:')
|
||||||
|
|
||||||
|
x = 'pizza' # Prints 54
|
||||||
|
y = b'pizza' # Prints 38
|
||||||
|
z = '🍕' #Prints 80
|
||||||
|
|
||||||
|
print(sys.getsizeof(x))
|
||||||
|
print(sys.getsizeof(y))
|
||||||
|
print(sys.getsizeof(z))
|
||||||
|
|
||||||
|
|
||||||
|
print("---------------------------------------------")
|
||||||
|
|
||||||
|
print("This is the String to Sequence of Numbers print out:")
|
||||||
|
|
||||||
|
def String_to_SequenceofNumbers():
|
||||||
|
my_string = input("Enter a string: ")
|
||||||
|
num_sequence = [ord(c) for c in my_string]
|
||||||
|
print(num_sequence)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
String_to_SequenceofNumbers()
|
||||||
|
|
||||||
|
print("---------------------------------------------")
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# Generate two random 64-bit integers
|
||||||
|
num1 = np.random.randint(0, 10 - 1)
|
||||||
|
num2 = np.random.randint(0, 10 - 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)
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import sys
|
||||||
|
num = 99999999999
|
||||||
|
size = sys.getsizeof(num)
|
||||||
|
print(f"The size of the float is {size} bytes.")
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
i=0
|
||||||
|
while True:
|
||||||
|
print(f"\015{i=}", end='')
|
||||||
|
i += 1
|
||||||
|
time.sleep(0.2)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
char1 = "u"
|
||||||
|
result1 = ord(char1)
|
||||||
|
print("ASCII value of the character {} is {}.".format(char1, result1))
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "0593e529",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"from urllib.parse import urlparse\n",
|
||||||
|
"\n",
|
||||||
|
"class JPEGRequestHandler:\n",
|
||||||
|
" def __init__(self, app):\n",
|
||||||
|
" self.app = app\n",
|
||||||
|
"\n",
|
||||||
|
" def __call__(self, environ, start_response):\n",
|
||||||
|
" url = urlparse(environ['REQUEST_URI'])\n",
|
||||||
|
" path = url.path\n",
|
||||||
|
"\n",
|
||||||
|
" if path.endswith('.jpg'):\n",
|
||||||
|
" file_path = path[1:]\n",
|
||||||
|
"\n",
|
||||||
|
" if os.path.isfile(file_path):\n",
|
||||||
|
" with open(file_path, 'rb') as file:\n",
|
||||||
|
" response_body = file.read()\n",
|
||||||
|
" status = '200 OK'\n",
|
||||||
|
" headers = [('Content-type', 'image/jpeg'),\n",
|
||||||
|
" ('Content-Length', str(len(response_body)))]\n",
|
||||||
|
"\n",
|
||||||
|
" start_response(status, headers)\n",
|
||||||
|
" return [response_body]\n",
|
||||||
|
" else:\n",
|
||||||
|
" status = '404 Not Found'\n",
|
||||||
|
" headers = [('Content-type', 'text/plain'),\n",
|
||||||
|
" ('Content-Length', '0')]\n",
|
||||||
|
"\n",
|
||||||
|
" start_response(status, headers)\n",
|
||||||
|
" return []\n",
|
||||||
|
" else:\n",
|
||||||
|
" status = '403 Forbidden'\n",
|
||||||
|
" headers = [('Content-type', 'text/plain'),\n",
|
||||||
|
" ('Content-Length', '0')]\n",
|
||||||
|
"\n",
|
||||||
|
" start_response(status, headers)\n",
|
||||||
|
" return []\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" from werkzeug.serving import run_simple\n",
|
||||||
|
" from werkzeug.middleware.dispatcher import DispatcherMiddleware\n",
|
||||||
|
"\n",
|
||||||
|
" app = DispatcherMiddleware(JPEGRequestHandler(app), {'/': app})\n",
|
||||||
|
" run_simple('0.0.0.0', 8000, app, use_reloader=True)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import os
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
class JPEGRequestHandler:
|
||||||
|
def __init__(self, app):
|
||||||
|
self.app = app
|
||||||
|
|
||||||
|
def __call__(self, environ, start_response):
|
||||||
|
url = urlparse(environ['REQUEST_URI'])
|
||||||
|
path = url.path
|
||||||
|
|
||||||
|
if path.endswith('.jpg'):
|
||||||
|
file_path = path[1:]
|
||||||
|
|
||||||
|
if os.path.isfile(file_path):
|
||||||
|
with open(file_path, 'rb') as file:
|
||||||
|
response_body = file.read()
|
||||||
|
status = '200 OK'
|
||||||
|
headers = [('Content-type', 'image/jpeg'),
|
||||||
|
('Content-Length', str(len(response_body)))]
|
||||||
|
|
||||||
|
start_response(status, headers)
|
||||||
|
return [response_body]
|
||||||
|
else:
|
||||||
|
status = '404 Not Found'
|
||||||
|
headers = [('Content-type', 'text/plain'),
|
||||||
|
('Content-Length', '0')]
|
||||||
|
|
||||||
|
start_response(status, headers)
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
status = '403 Forbidden'
|
||||||
|
headers = [('Content-type', 'text/plain'),
|
||||||
|
('Content-Length', '0')]
|
||||||
|
|
||||||
|
start_response(status, headers)
|
||||||
|
return []
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from werkzeug.serving import run_simple
|
||||||
|
from werkzeug.middleware.dispatcher import DispatcherMiddleware
|
||||||
|
|
||||||
|
app = DispatcherMiddleware(JPEGRequestHandler(app), {'/': app})
|
||||||
|
run_simple('0.0.0.0', 8000, app, use_reloader=True)
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# Question7:
|
||||||
|
# Difference:
|
||||||
|
# 1. Byte objects are sequence of Bytes, whereas Strings are sequence of characters.
|
||||||
|
# 2. Byte objects are in machine readable form internally; Strings are only in human readable form.
|
||||||
|
# 3. strings (str) support various character encodings, such as UTF-8, ASCII, and more. Bytes (bytes), on the other hand, represent raw binary data and can hold any byte values.
|
||||||
|
## Emojis can affect the size difference.
|
||||||
|
import sys
|
||||||
|
string_size = sys.getsizeof("Hello")
|
||||||
|
bytes_size = sys.getsizeof(b"Hello")
|
||||||
|
emoji_size = sys.getsizeof("😀")
|
||||||
|
print(f"String size: {string_size} bytes")
|
||||||
|
print(f"Bytes size: {bytes_size} bytes")
|
||||||
|
print(f"emoji size: {emoji_size} bytes")
|
||||||
|
|
||||||
|
# python routine:
|
||||||
|
def print_string_as_numbers(string):
|
||||||
|
for char in string:
|
||||||
|
print(ord(char))
|
||||||
|
input_string = "Hello, world!"
|
||||||
|
print_string_as_numbers(input_string)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# question 1: The international standard for floating-point representation is called IEEE 754.
|
||||||
|
|
||||||
|
# question 2: The IEEE 754 standard allows floating-point numbers to be represented with different bit sizes. The most commonly used formats specified by the standard are single precision (32 bits) and double precision (64 bits).
|
||||||
|
|
||||||
|
# question 3:
|
||||||
|
import sys
|
||||||
|
print(sys.float_info)
|
||||||
|
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
|
||||||
|
|
||||||
|
# question 4: In the first multiplication (num1 * num2), you are using the normal Python multiplication
|
||||||
|
# operator, which operates on native Python integers. Python integers have arbitrary precision, meaning they can
|
||||||
|
# represent very large numbers accurately. However, when the result exceeds the limit of a 64-bit signed integer
|
||||||
|
# (2^63 - 1), an overflow may occur, and the result may not be accurate.
|
||||||
|
# In the second multiplication (df['Number1'] * df['Number2']), you are using the Pandas multiplication operator
|
||||||
|
# on the columns of a DataFrame. By default, Pandas uses NumPy for numerical computations, and NumPy uses
|
||||||
|
# fixed-size data types. In this case, the integers in the DataFrame are most likely represented as 64-bit
|
||||||
|
# signed integers (int64), which have a maximum value of 2^63 - 1. If the result of the multiplication exceeds
|
||||||
|
# this limit, it will overflow and wrap around, leading to incorrect results.
|
||||||
|
|
||||||
|
# Question5: 5. The size of a Python floating-point number is determined by the implementation and is not influenced by the magnitude of the value it represents. So it does not change as the value of the number gets larger.
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user