# Programming

7

## Variables + Basic Data Types

What is a variable?

In programming, a variable is somewhere you store information that you want to do things with. The information that you store is called a value. Think of your refrigerator. You probably buy a bunch of food at once, save it in your refrigerator, and then you eat some of it each day. In programming it is similar, you store your information in variables, until you are ready to do something with it.

Text (strings)

Text can be anything that you can type. Each letter, symbol, or digit is called a character. In programming, a bunch of characters bundled together is called a string.

Numbers

Numbers are any digit 0-9, such as 15 or 23. Numbers can also be a decimal value like .5 or 23.15.

How to make or change a variable

To set (make) a variable, you type the name of your variable, an equals sign, and then put either text or a number afterwards. To save text, you need to put it in quotes. To save a number, you just put it right after the equals sign.

1    refrigerator = “milk”
2    variable = “value”

3    character = “A”
4    string = “A string”

5    number = 123
6    decimal – 1.2343

## Printing

Printing text, numbers, and variables

To print, use the print function. In it you put what you want printed inside of the parentheses. If you are printing a variable, make sure that you set it before you use the print function. It looks like this:

1    refrigerator = “milk”
2    print(“text”)
3    print(9)
4    print(refrigerator)

The output of the above code would look like:

text
9
milk

Printing several things

To print several strings all at once, you can use a plus sign. If you do this to numbers, they will be added, and then printed. If you do this to text variables, they will be combined, and then printed. Take a look at this example:

1    refrigerator = “milk”
2    string = “text”
3    print(refrigerator + string)
4    print(refrigerator + ” ” + string)
5    print (34 + 12)

It should print out:

milktext
milk text
46

## Combining Variables + Math Operations

Combining text variables

To combine two text variables you can use “+=”. This will add the first variable to the end of the second and store the result in the first variable (concatenation). In the example below we add the mix variable “chocolate” to the drink variable “milk”. This will store “chocolatemilk” in the mix variable. Next we reset the mix variable and add a space using the “+” sign when we add the mix and drink variables together.

1    mix = “chocolate”
2    drink = “milk”
3    mix += drink
4    print(mix)
5
6    mix = “chocolate”
7    mix += ” ” + drink
8    print(mix)

The output of the above code would look like:

chocolatemilk
chocolate milk

Basic math with number variables

If you want to do math with two variables and store them in a third variable use the + , – , * , / , **

1    number1 = 9
2    number2 = 3
3    adding = number1 + number2
5    subtracting = number1 – number2
6    print(subtracting)
7    multiplying = number1 * number
8    print(multiplying)
9    dividing = number1 / number2
10   print(dividing)
11   exponents = number1 ** number2
12   print(exponents)

It should print out:

12
6
27
3
729

Otherwise, you can use += , -= , *= , /=, or ** to put the result in the first variable.

1    number1 = 9
2    number2 = 3
3    number1 += number2         #note: number1 = 11
4    number1 -= number2         #note: number1 = 6
5    number1 *= number2         #note: number1 = 27
6    number1 /= number2         #note: number1 = 3
7    number1 ** number2         #note: number1 = 729

Working with square roots of variables is slightly more complex. We will use a python library (see the module on libraries for details) to get a tool that will do roots for us. To do this, add the line “import math” which adds the math library to our program. Now when we want to do square roots we use “math.sqrt()”.

1    import math
2    number1 = 9
3    result = math.sqrt(number1)