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?
- The first one,
13 / 5, is the classic division we get using a calculator. 13 divided by 5 is 2.6. This is known as “floating point division”.
- The second one,
13 // 5, is the division you learnt in primary school. Think of it as asking “how many times does 5 go into 13?” The answer, of course, is 2. This is known as “integer division”.
- The third one,
13 % 5, is the companion question to the second one: “If 13 goes into 5 twice, how much is left over?” 5 goes into 13 twice which makes 10 with 3 left over. So 13 % 5 will tell us 3. This is known as the “modulus” or the remainder.
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 _
- The name must start with a letter
- 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
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
7on one line, then
9on the next line.
Given a two-digit integer, swap its digits and print the result. For example, given the input
Given an integer greater than 9, print its last two digits. For example, given input
Given an integer, print its tens digit. For example, given
Given a three-digit number. Find the sum of its digits. For example, given
Given a positive real number, print its first digit to the right of the decimal point. For example, given
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
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