Assignment

Variables are used to store values. An assignment statement has 3 parts:

  1. = is the assignment operator (an equals sign in maths)
  2. The value to be assigned is to the right of the assignment operator
  3. The variable that the value will assigned to is to the left of the assignment operator
variableName = valueToAssign

The value might need to be calculated first before being assigned.

Data types

# Assign an integer value
myInteger = 5
# Assign a real value
myReal = 3.14
# Assign a character
myCharacter = "&"
# Assign a string
myString = "Hello"
# Assign a Boolean value
myBoolean = True

Data structures

All values

# Assign an array of integers
myArrayOfIntegers = [56, 34, 2, 85, 51]
# Assign an array with many values - split over lines
myLongArray = ["Monday", "Tuesday", "Wednesday", "Thursday", 
               "Friday", "Saturday", "Sunday"]
# Assign an array of strings - Shorthand
myArrayOfStrings = [""] * 4

Individual value

Elements in an array are numbered, starting at zero. They are accessed using the subscript operator [ ] and the index position of the element.

# Assign a value to an element
myArrayOfIntegers[0] = 65