Scalar Types


Scalar Types

int

  • arbitrary precision integer
  • for all practical purposes, have unlimited precision (i.e. can contain as many digits as needed)
  • examples:
# decimal
10

# binary
0b10 #=> 2

# octal
0o10 #=> 8

# hexadecimal
0x10 #=> 16

# using `int()` constructor
### NOTE: rounding of integers is always towards 0
int(3.5) #=> 3
int(-3.5) #=> 3
int("496") #=> 496
# optional argument to change base
int("10000", 3) #=> 81

flat

  • 64-bit floating point numbers
  • IEEE-754 double-precision with 53-bits of binary precision
    • equivalent to between 15 and 16 significant digits in decimal
  • examples:
# float
3.125

# scientific notation can be used
### approximate speed of light in meters per second (3x10^8)
3e8 #=> 300000000.0

### Planck's constant, 1.616x10^-35
1.616e-35 #=> 1.616e-35

# using `float()` constructor
float(7) #=> 7.0
float("1.618") #=> 1.618

# special floating point numbers
float("nan") #=> nan (not a number)
float("inf") #=> inf (positive infinity)
float("-inf") #=> -inf (negative infinity)

NoneType

  • the null object
  • Null value
  • often represents absence of value
  • examples:
None #=>
a = None
a is None #=> True

bool

  • boolean logical values
  • 'falsey' values:
    • 0, 0.0, "", []
  • examples:
True
False

# using `bool()` constructor
bool(0) #=> False
bool(42) #=> True
bool(-1) #=> True
bool(0.0) #=> False
bool(0.207) #=> True
bool(-1.117) #=> True
bool([]) #=> False
bool([1, 5, 9]) #=> True
bool("") #=> False
bool("Spam") #=> True
Made with Gatsby G Logo