Using numbers in Python

Let’s get started with programming by doing something very familiar to us all… simple mathematical calculations. While I promise I won’t get too mathy on you, I will say that mathematical calculations are an essential part of successful programming so being comfortable with the basics is a good idea.

Let’s use Python as a calculator! Try the following commands. Hopefully they are all quite intuitive as to what they will do.

print( 2 + 2 )
print( 1.5 + 2.25 )
print( 7 – 2 )
print( 3 * 4 )
print( 10 / 2 )

If you want to use powers/exponents, use the double star symbol. Try these:

print( 4 ** 2 )         # What is 4 to the power of 2? (ie: 4 squared?)
print( 4 ** 3 )         # What is 4 to the power of 3? (ie: 4 cubed?)

Finally there are two different types of division you need to know. Compare the different results of these?

print(13 / 5)
print(13 // 5)
print(13 % 5)

What are these doing?

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?

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


Numbers exercises

  1. Given a two-digit integer, print its left digit (a tens digit) and then its right digit (a ones digit). Use the operator of integer division for obtaining the tens digit and the operator of taking remainder for obtaining the ones digit. For example, given the input 79, output 7 on one line, then 9 on the next line.

  2. Given a two-digit integer, swap its digits and print the result. For example, given the input 79, output 97.

  3. Given an integer greater than 9, print its last two digits. For example, given input 1234, output 34.

  4. Given an integer, print its tens digit. For example, given 1234, output 3.

  5. Given a three-digit number. Find the sum of its digits. For example, given 123, output 6.

  6. Given a positive real number, print its first digit to the right of the decimal point. For example, given 1.79, output 7.

  7. A cupcake costs A dollars and B cents. Determine, how many dollars and cents should one pay for N cupcakes. A program gets three numbers: A, B, N. It should print two numbers: total cost in dollars and cents.

    For example, given input of

    python A = 10 B = 15 N = 2



  8. Given the integer N - the number of minutes that is passed since midnight - how many hours and minutes are displayed on the 24h digital clock? The program should print two numbers: the number of hours (between 0 and 23) and the number of minutes (between 0 and 59). For example, if N = 150, then 150 minutes have passed since midnight - i.e. now is 2:30 am. So the program should print 2 30.