Data Representation in Python Flashcards
How can an integer in python be represented as binary, octal, hexadecimal
:
{num}
{num : b}
{num : x}
{num : o}
How can you store integers using different bases
using prefixes
[19, 0b1010, 0xf35, 0o17]
all stored as integers NOT strings
what displays the maximum number of digits
that can be stored in an integer object under a specific system.
sys.get_int_max_str_digits()
how to convert 0b11101 into float
float(0b11101) -> 29.0
There is a limitation to the maximum number that can be stored in a float object. This can
be retrieved using
sys.float_info
how is infinity and - infinity represented in python
float(‘inf’) and float(‘-inf’)
no infinite object for integers
If a string contains all numerical characters, then it can be converted into an integer, using
the command
int(val) for decimal otherwise specify base
int(‘0b110111’,2) → 55
int(‘0xf35’,16) → 3893
int(‘0o17’,8) → 15
float(‘0b110111’,2)
float(‘0xf35’,16)
float(‘0o17’,8)
output?
TypeError
Output of
int(‘34bc1’)
float(‘34/2’)
ValueError exception
What functions are used to
convert any integer into a string using the appropriate base.
bin()
hex()
oct()
To create a bytes object, we can use three different methods:
using a string with b’ ‘
using bytes() command w/ number of bytes
using bytes() w/ string and encoding
for the first method of byte representation
b’ ‘
what should the characters be
characters must be ASCII
for non-printable use hexadecimal format like b’\x15st’
if you provide the entire string in hex it would only convert printable characters
list encoding types
‘ascii’
‘utf-8’
‘utf-16’
‘utf-32’
x = b’ab’
y = [i for i in x] → [97,98]
list(x) → [97,98]
is this valid ?
yes bytes data type is iterable and sub-scriptable
However immutable
can’t do x[0] = b’z’
What if I want a mutable bytes?
use bytearray data type
There are two ways in which computer machines store sequence of bytes
little (host order) and big endian (network order)
To figure out the byte order in your machine, use the Python command:
print(sys.byteorder)
For instance, the byte sequence: b’\x61\x62\x63\x64’,
equivalent to b’abcd’, is stored as:
b’\x61\x62\x63\x64’(big endian)
b’\x64\x63\x62\x61’ (little endian)
Converting From Integers to Bytes:
(int obj).to_bytes(#bytes, byteorder)
How to convert from bytes to an integer
int.from_bytes(bytes, byteorder)
Description of ascii encoding
characters are stored in a single byte (fixed length). Only the least 7 bits are used
the first 32 characters are non-printable and for control only
UTF-8 description
characters are stored in
1, 2, 3 or 4 bytes
(var length system) used in 95% of websites
compatible with ASCII
utf-16 description
characters are stored in 2 or 4 bytes
(variable length system)
MS API and java
utf-32 description
characters are stored in 4 bytes (fixed length system)
used by internal APIs in some progslangs and few UNIX programs