© 2017 Morgan C. Benton code4your.life
The ability to create and manipulate symbols distinguishes humans from nearly every other organism on the planet, and may be at the root of our intelligence.
Computer programmers explicitly harness the power of symbols to build software.
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
// examples of constants
const KPM = 1.609344; // kilometers per mile
const GPL = 0.264172; // gallons per liter
// examples of variables
let mpg = 30; // miles per gallon
let kpl; // kilometers per liter
// calculate kpl
kpl = mpg * KPM * GPL;
Other than the basic differences, there are basically two things to worry about:
© 2017 Morgan C. Benton code4your.life
That which we call a rose
By any other name would smell as sweet.--Shakespeare, Romeo & Juliet (II, ii, 1-2)
TAKE HOME MESSAGE:
It's important how
you name stuff.
© 2017 Morgan C. Benton code4your.life
Wikipedia article: https://en.wikipedia.org/wiki/Naming_convention_(programming)
© 2017 Morgan C. Benton code4your.life
// GOOD: short, meaningful symbol names
const KPM = 1.609344; // km/mile
const GPL = 0.264172; // gal/liter
let mpg = 30; // miles/gal
let kpl; // km/liter
// calculate kpl
kpl = mpg * KPM * GPL;
// BAD: short but cryptic
const x = 1.609344;
const y = 0.264172;
let a = 30;
let b;
// calculate kpl
b = a * x * y;
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
© 2017 Morgan C. Benton code4your.life
There are a lot of differences in how data types are implemented across various languages. These examples are not exhaustive, but should give you an idea and also motivate you to study further...
// JavaScript is dynamically typed
// variables need not be declared
// before use
x = undefined; // undefined
x = true; // boolean
x = 3; // number
x = NaN; // number
x = Infinity; // number
x = "3"; // string
x = () => {}; // function
x = null; // object
x = []; // object
x = {}; // object
x = new Date(); // object
© 2017 Morgan C. Benton code4your.life
Primitive Types
Object types
See this survey by Douglas Crockford for a good overview
# Python is dynamically typed; variables
# do not need to be declared before use
x = True # bool
x = "hi" # str
x = 3 # int
x = 3.0 # float
x = 3j # complex
x = None # NoneType (null value)
x = [1, 2, 3] # list (an array)
x = (1, 2, 3) # tuple (immutable list)
x = {1, 2, 3} # set
x = {"one": 1} # dict (a hash table)
x = lambda x: x # function
x = b"hello" # bytes (binary data)
© 2017 Morgan C. Benton code4your.life
See description of native types in DiveIntoPython3 for a good overview
> # R is dynamically typed; variables do not
> # need to be declared before using them
> x <- 5.25 # numeric
> x <- 5 # numeric
> is.integer(x) # FALSE
> x <- as.integer(5) # integer
> is.integer(x) # TRUE
> x <- 1 + 2i # complex
> x <- TRUE # logical
> x <- "hello" # character
> x <- c(1, 2, 3) # numeric vector (array)
> x <- c("1", "2", "3") # character vector
> x <- matrix(c(1,2,3,4), nrow=2,
ncol=2, byrow=TRUE)
> x # matrix (2D array)
[,1] [,2]
[1,] 1 2
[2,] 3 4
> x <- list(c(1, 2, 3), # list (array of vectors)
c("1","2","3"))
> # data frames are special and unique to R
> # and worthy of study on their own
© 2017 Morgan C. Benton code4your.life
See this R Tutorial for a good overview
# Ruby is dynamically typed; variables
# do not need to be declared before use
x = nil # Nil (null)
x = true # Boolean
x = 5 # Integer
x = 5.0 # Float
x = "hello" # String
x = [1, 2, 3] # Array
x = {"one": 1, "two": 2} # Hash
x = :x # Symbol
# all looks pretty standard except Symbol...
# what the hell is Symbol?
© 2017 Morgan C. Benton code4your.life
See this wikibook for a good overview (including an explanation of the mysterious Symbol type)
# PHP is dynamically typed; variables
# do not need to be declared before use
$x = null; # NULL
$x = true; # boolean
$x = 5; # integer
$x = 5.0; # double
$x = 'hello'; # string
$x = [1, 2, 3]; # array
$x = (object) ["one" => 1]; # object
$x = fopen('x.txt', 'w'); # resource
# a resource is something like a file
# reference or a network or database
# connection
© 2017 Morgan C. Benton code4your.life
W3Schools has good tutorials on most things, and their PHP data type overview is no exception
class Main {
public static void main(String[] args) {
byte y = 0; // 8-bit int: +/- 2^7
short s = 1; // 16-bit int: +/- 2^15
int i = 2; // 32-bit int: +/- 2^31
long l = 3; // 64-bit int: +/- 2^63
float f = 4.0f; // 32-bit precision
double d = 5.1; // 64-bit precision
boolean b = true; // 1-bit
char c = 'a'; // 16-bit unicode char
String hi = "hi"; // equivalent to below
char[] h2 = {'h', 'i'}; // array of char
}
}
© 2017 Morgan C. Benton code4your.life
The Java™ Tutorial is the official place to read up on this
package main
import "fmt"
func main() {
var // indicates we're starting our
// variable declarations
bool b = true // 1-bit boolean
string hi = "hi" // string
// unsigned integers
ui uint = 0 // 32 or 64 bit
ui8 uint8 = 0 // 8-bit: 0 ~ 255
ui16 uint16 = 0 // 16-bit: 0 ~ 2^16
ui32 uint32 = 0 // 32-bit: 0 ~ 2^32
ui64 uint64 = 0 // 64-bit: 0 ~ 2^64
y byte = 0 // alias for uint8
// signed integers
ui int = 0 // same as uint
ui8 int8 = 0 // 8-bit: +/- 2^7
ui16 int16 = 0 // 16-bit: +/- 2^15
ui32 int32 = 0 // 32-bit: +/- 2^31
ui64 int64 = 0 // 64-bit: +/- 2^63
rn rune = 0 // alias for int32
// floats
f32 float32 = 4.0 // 32-bit precision
f64 float64 = 4.0 // 64-bit precision
// complex numbers: 64- and 128-bit
c64 complex64 = (1.0 + 2i)
c128 complex128 = (1.0 + 2i)
}
© 2017 Morgan C. Benton code4your.life
In addition to the types shown to the right, Go has:
Try this tutorial if you're interested
Symbols, i.e. variables and constants, are a fundamental component of every programming language
You should have an explicit strategy for naming things
Whether dynamic or strong, every language implements some form of data type system which determines how memory is allocated and how symbols are stored
While most of the time it won't be necessary to spend a lot of time focusing on the data type of your variables, it's important to understand that it exists, and have a sense of the possible ramifications
© 2017 Morgan C. Benton code4your.life