Most of us are familiar with variables in computer languages: strings of characters that represent some piece of information stored in the computer's memory. Likewise, most of us are familiar with the assignment operator, which directs the program to assign a particular value to a particular variable. Consider, for example, the Python statement a = 2
:
a = 2
b = 2.
Under the hood, a space in memory is allocated, associated with a
, and the value 2 is placed into that memory location.
The decimal number 2 is recorded as the binary number 10.
A single binary digit is a bit. Bits are organized into bytes of 8.
What happens under the hood when we execute the following commands in Python?
a = 2
b = 2.
c = 2 + 2j
d = '2'
e = 'two'
We might even try:
f = a == 2
or even:
def two():
pass
g = two()
int, float, double, char
(int variations: signed/unsigned, short/long)
int (integer) in many compiled languages, 2 or 4 bytes. In Python 3, arbitrary precision, depending on size.
float (floating-point number) depends on platform, but commonly 64 bits. Represented using exponent and mantissa.
complex (complex number) includes real and imaginary part; use "j" to indicate.
bool (boolean) True or False
str (string) object containing a string of characters
NoneType only value is None
object more later!
a = 2
type(a)
b = 2.
type(b)
c = '2'
type(c)
d = 'two'
print(type(d))
d[0]
c + d
e = 2 + 2j
type(e)
f = a == 2
type(f)
g = None
type(g)
list tuple dict
shopping = ['pear', 5, 3.14159]
shopping.append('honey')
shopping += ['88']
shopping[0] = 'peach'
shopping
# tuple
mytup = (1, 2., '3')
mytup
#mytup[0] = 5 # Illegal because tuples are immutable
d = {'spoons' : 13, 'frog' : 'green'}
type(d)
d['frog']
d['gravity'] = 9.81
d
print(d.keys())
print(d.values())