Processing Data

Working With Numbers

Let's get some integer (aka whole number) variables going and learn how to add, divide, subtract and multiply in Python.

Addition

Run the following code to see how Python adds together numbers


In [ ]:
boys = int(input('How many boys are in the class: '))
girls = int(input('How many girls are in the class:'))
pupils = boys + girls
print('There are', pupils,'in the class altogether')

Subtraction

Run the following code to see how Python subtracts numbers


In [ ]:
bigger_number = 12
smaller_number = 10
difference = bigger_number - smaller_number
print('The difference between', bigger_number, 'and', smaller_number, 'is', difference)

Multiplication

Python uses an asterisk * to show multiplication.

Run the following code to see how Python multiplies numbers


In [ ]:
number1 = 5
number2 = 6
answer = number1 * number2
print(answer)

Division

Python uses a forward slash / to show division.

Run the following code to see how Python divides numbers


In [ ]:
big_number = 100
divisor_number = 25
dividend_answer = 100/25
print(big_number,'divided by', divisor_number, 'is', dividend_answer)

Extra Useful Bit: Modulo Division (Remainder/Left Overs)

Python uses a percentage sign % to calculate remainders

Run the following code to see how Python can compute remainders when dividing


In [ ]:
big_number = 102
divisor_number = 25
remainder = 100%25
print('If you divide', big_number,'by', divisor_number, 'you get', remainder, 'left over')

Working with Strings - aka Text

Text characters are called strings in Python - this is because they are stored as a string of characters.
Here are two example strings to look at:

phrase1 = 'The quick brown fox jumped over'
phrase2 = 'the moon'

I've assigned them to two variables called phrase1 and phrase2.
Run the following code to see how we can add strings.


In [ ]:
phrase1 = 'The quick brown fox jumped over'
phrase2 = 'the moon'
sentence = phrase1+phrase2
print(sentence)

Change line 3 of the above program to put in a space
Hint: +' '+

If you've done it right, your output should look like:

The quick brown fox jumped over the moon

Exercise

Choose some of the word variables below, add them to make an example sentence of your own


In [ ]:
noun1 = 'turnip'
noun2 = 'elephant'
noun3 = 'worm'
noun4 = 'holiday'
noun5 = 'Scalloway'
verb1 = 'went'
verb2 = 'ate'
verb3 = 'sat'
verb4 = 'jumped'
preposition1 = 'on'
preposition2 = 'to'
preposition3 = 'with'
def_article = 'the'
indef_article = 'a'

example1 = def_article+' '+noun1+' '+verb1+' '+preposition1+' '+noun4+' '+preposition2+' '+noun5
example2 = 'change this line'
print(example1)
print(example2)

Getting Rid of all the ' ' Space Bits

It's pretty horrible having to do noun1+' '+verb1, putting in all those ' ' spaces.
Here's the same program again, but this time using print() with commas to do the work.
The disadvantage of this is the sentence isn't being stored in a variable.
In the last line below, use commas to make your own sentence


In [ ]:
noun1 = 'turnip'
noun2 = 'elephant'
noun3 = 'worm'
noun4 = 'holiday'
noun5 = 'Scalloway'
verb1 = 'went'
verb2 = 'ate'
verb3 = 'sat'
verb4 = 'jumped'
preposition1 = 'on'
preposition2 = 'to'
preposition3 = 'with'
def_article = 'the'
indef_article = 'a'

print(def_article,noun1,verb1,preposition1,noun4,preposition2,noun5)
print('replace this with your own choice of variables')

How Long is a Piece of String

As well as all that science, Python can easily tell us how long strings really are! Who knew?

print(len("How long is this string?"))

Exercise

Print the lengths of each of the strings in these variables using the len() function


In [ ]:
noun1 = 'turnip'
noun2 = 'elephant'
noun3 = 'worm'
noun4 = 'holiday'

If you've done it right you should get 6, 8 , 4 and 7.

There are no additional programming questions for this tutorial. Just go on to #4 Making Decisions.