Data Representation in Python Flashcards

1
Q

How can an integer in python be represented as binary, octal, hexadecimal

A

:
{num}
{num : b}
{num : x}
{num : o}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you store integers using different bases

A

using prefixes
[19, 0b1010, 0xf35, 0o17]

all stored as integers NOT strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what displays the maximum number of digits
that can be stored in an integer object under a specific system.

A

sys.get_int_max_str_digits()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to convert 0b11101 into float

A

float(0b11101) -> 29.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

There is a limitation to the maximum number that can be stored in a float object. This can
be retrieved using

A

sys.float_info

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how is infinity and - infinity represented in python

A

float(‘inf’) and float(‘-inf’)

no infinite object for integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If a string contains all numerical characters, then it can be converted into an integer, using
the command

A

int(val) for decimal otherwise specify base

int(‘0b110111’,2) → 55
int(‘0xf35’,16) → 3893
int(‘0o17’,8) → 15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

float(‘0b110111’,2)
float(‘0xf35’,16)
float(‘0o17’,8)
output?

A

TypeError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Output of
int(‘34bc1’)
float(‘34/2’)

A

ValueError exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What functions are used to
convert any integer into a string using the appropriate base.

A

bin()
hex()
oct()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To create a bytes object, we can use three different methods:

A

using a string with b’ ‘

using bytes() command w/ number of bytes

using bytes() w/ string and encoding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

for the first method of byte representation
b’ ‘
what should the characters be

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

list encoding types

A

‘ascii’
‘utf-8’
‘utf-16’
‘utf-32’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

x = b’ab’
y = [i for i in x] → [97,98]
list(x) → [97,98]

is this valid ?

A

yes bytes data type is iterable and sub-scriptable
However immutable
can’t do x[0] = b’z’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What if I want a mutable bytes?

A

use bytearray data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

There are two ways in which computer machines store sequence of bytes

A

little (host order) and big endian (network order)

17
Q

To figure out the byte order in your machine, use the Python command:

A

print(sys.byteorder)

18
Q

For instance, the byte sequence: b’\x61\x62\x63\x64’,
equivalent to b’abcd’, is stored as:

A

b’\x61\x62\x63\x64’(big endian)
b’\x64\x63\x62\x61’ (little endian)

18
Q

Converting From Integers to Bytes:

A

(int obj).to_bytes(#bytes, byteorder)

18
Q

How to convert from bytes to an integer

A

int.from_bytes(bytes, byteorder)

18
Q

Description of ascii encoding

A

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

19
Q

UTF-8 description

A

characters are stored in
1, 2, 3 or 4 bytes
(var length system) used in 95% of websites
compatible with ASCII

20
Q

utf-16 description

A

characters are stored in 2 or 4 bytes
(variable length system)
MS API and java

21
Q

utf-32 description

A

characters are stored in 4 bytes (fixed length system)
used by internal APIs in some progslangs and few UNIX programs

22
what are the ord and chr functions
built-in functions to convert characters to their encoding values ord takes single unicode characters and returns an integer value chr takes an integer and returns corresponding unicode character English two digit number int Arabic four digit number int
23
Python allows the use of Unicode characters as numbers using what prefix
'\u' por ejemplo x = '\u0061' x = '\u0100'
24
what does x.hex() knowing x is a bytes object
This method acts like the hexlify function except that the return value is a string.
25
what function returns the Unicode number representation corresponding to each character, stored in a bytes object.
hexlify (b'a') -> b'61' hexlify(b'ab') -> b'6162'
26
The reverse process where you get the encoding number and you want the character stored in a bytes object
unhexlify(b'61') -> b'a'
27
in which library do we have hexlify and unhexlify
binascii
28
Convert a 32-bit positive integer from host to network byte orde
htonl(x)
29
Convert 32-bit positive integers from network to host byte order
ntohl(x)
30
Convert 16-bit positive integers from host to network byte order
htons(x)
31
Convert 16-bit positive integers from network to host byte order
ntohs(x)
32
Convert an IPv4 address2 from a four dot-string IP format into a binary format
inet_aton(ip_string)
33
Convert an IPv4 address from a binary format into a four dot-string IP format
inet_ntoa(ip_string)