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')
In [ ]:
bigger_number = 12
smaller_number = 10
difference = bigger_number - smaller_number
print('The difference between', bigger_number, 'and', smaller_number, 'is', difference)
In [ ]:
number1 = 5
number2 = 6
answer = number1 * number2
print(answer)
In [ ]:
big_number = 100
divisor_number = 25
dividend_answer = 100/25
print(big_number,'divided by', divisor_number, 'is', dividend_answer)
In [ ]:
big_number = 102
divisor_number = 25
remainder = 100%25
print('If you divide', big_number,'by', divisor_number, 'you get', remainder, 'left over')
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
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)
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')
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.