# Numeric variables

What is a variable? A variable is just a name we assign to a piece of memory on the computer. The is similar to the memory button you are used to on the calculator, except we can have as many variables (memory locations) as we want, and we can give them names.

Example to create a variable called “x” and give it a value of 5:

``````x = 4
``````

## A common mistake: Case sensitivity

Python is case-sensitive when it comes to variable names. Upper case `X` is treated as a different name to lower case `x`. This is important to remember as one of the most common mistakes students make is changing the spelling of their variable names and wondering why it doesn’t work.

So if I wanted to confuse myself I could do the following (don’t do this!)

``````x = 4           # Lower case x
X = 15          # Upper case X
print(x)        # Lower case x
print(X)        # Upper case X
``````

## Using variable names in my calculations

The same way we did math before by typing in the actual numbers, we can do the same thing by substituting a number with a variable name. Python will automatically look up the variable’s value and use it in the calculation.

``````x = 4
print( 2 * x )
print( x – 5 )
``````

What names can we use for variables?

• Anything alpha-numeric – which is to say any combination of letters and numbers.
• The only punctuation allowed is the underscore _
• Spaces are not allowed

What number will Python print in the last line of this example?

``````this_is_a_variable = 100
eggs_per_cake = 3
number_of_cakes = 10
total_eggs = eggs_per_cake * number_of_cakes
print( total_eggs )
``````

## A common mistake: Correctly writing calculations

When doing a calculation to save the result in a variable the rule is that the value or calculation goes on the right of the equal sign, the name of the variable we want to save the answer to goes on the left of the equal sign. ie:

``````save_to_me = use_my_value + some_other_value
``````

## Exercises

Some practice exercise with doing simple calculations and variables:

1. Write a program that for any set number, will print out what the number before it was and the number after it. For example, if given the number 179, the output would be: `The next number for 179 is 180, and the previous number was 178.`

2. Write a program that for any number of seconds, will let you know how many hours, minutes and seconds this converts to. For example `4030 seconds is 1 hour, 7 minutes and 10 seconds`

3. Write a program that will extract the 10s digit from a number. For example, `The tens digit in 1234 is 3.`