# Tutorials/The Basics

Jump to: navigation, search

Have you downloaded kdb+? If not, see our installation instructions before you start!

First, let’s try typing some basic arithmetic into the q console. (Press enter to return a result.)

```q)4*5
20
q)2+8+9 / This is a comment and is ignored.
19
q)5*1+2
15
```

Did you expect the last answer to be 7? There is no order of operations in q, it simply executes from right to left (or left of right). This means that the sum 1+2 is evaluated first, and the result of this is then multiplied by 5. We can enforce order of operation by using parentheses, however the more you can take advantage of the right-to-left evaluation, the more efficient your code will be.

```q)(5*1)+2
7
q)2+5*1	/ more efficient code uses less parentheses
7
```

What else can we do? Let’s make some variables.

```q)x:5		/ define x
q)y:1 2 3	/ define y
q)x+y		/ add x and y
6 7 8
q)x,y		/ join x and y using a comma
5 1 2 3
q)z:4 5 6 7
q)y+z		/ add y and z
'length
```

We defined 3 variables - x, y and z - using the assignment operator :. y and z are lists (also know as arrays/vectors), whereas x is an atom (meaning it is a single item). The last line is an error message - you can add atoms to atoms, and atoms to lists, but if you want to add lists to lists they’ve got to be the same length.

```q)count y
3
q)count z
4
```

# Exercises

1. Calculate the following mathematical expression in q using as few parentheses as possible

(((2+5)×(3×100)) / ((38×2)+24)) = 21

Hint: the division operator in q is %

2. Define a list d.

• Multiply each element of d by 2 and add the result to d.
• Add 1 to each element of d. Call this e. Multiply each element of e by itself.
• Join d and e.