Sequence: Lines of code are run in order they appear once.
* symbol: Multiply e.g. 2 * 4 = 8
/ symbol: Divide e.g. 10 / 5 = 2
// Integer Division symbol: this only gives a whole number e.g. 11 // 5 = 2
% Modulus symbol: this gives the whole number remainder e.g. 11//2 = 1
+ symbol: Add numbers or Concatenate strings e.g. 5 + 2 = 7 e.g. "Hello " + "World" = "Hello World"
** symbol: To the power of. e.g. 2 ** 3 = 8 (This is 2 x 2 x 2)
Look at the code for each question.
What do you think will be the output?
What is the actual output?
Look at the code on the right.
Run it and see what it does.
Click on the pencil icon at the top to see the code again.
First Example:
change the numbers 10, and 1 to something else.
Change myLives = myLives - 1 to myLives = myLives -=2
Second Example:
Change the variable - basketCanHold to another number.
What do you think str( ) does?
code:
myLives = 10
print("My HP:")
print(myLives)
print("Oh no! I got hit!")
#Change 1 to another number. What happens if you try -- 1?
myLives = myLives - 1
print("My HP:")
print(myLives)
code 2:
basketCanHold = 5 #Try changing this number
applesCollected = 34
print("An apple basket can hold " + str(basketCanHold) + " apples.")
print("My friends and I have picked "+ str(applesCollected) + " apples")
print("How many baskets do I need?")
print("I will have... " )
print(applesCollected//basketCanHold )
print("full baskets.")
print("With ...")
print(applesCollected%basketCanHold )
print("apples left over.")
print("In total, I will need...")
print(applesCollected//basketCanHold + 1)
print("Baskets")
Help:
print ( ) is used to output text
input( ) is used to get the user to type something
Variables store values that can change.
Name = Value here
e.g. myName = "John"
Make a program to create a variable called myAge, then add 7. Output the message: "In Dog years, you are [age + 7] years old!"
myAge = 12
Outputs: In dog years, you are 19 years old!
Output the result of two numbers multiplied by each other
e.g. 5 x 3
Outputs: 15
Output the result of a number multiplied to the power of 4.
e.g. 2 to the power of 4
Outputs: 32 (2x2x2x2)
CHALLENGE:
Create two variables to store two numbers.
Divide the first number by the second number to give the answer as a whole number, and how much is remaining.
Number 1: 11
Number 2: 4
Outputs: 11 divided by 4 is 2
Outputs: remainder 3
What will be printed by the following code:
print(6 * 3)
What will be printed by the following code:
print("Hi " * 3)
What will be printed by the following code:
print("Password" + "123")
What will be printed by the following code:
print(3 * 3 * 3)
What will be printed by the following code:
print(3** 3)
What will be printed by the following code:
print(13 / 2)
What will be printed by the following code:
print(13 // 2)
What will be printed by the following code:
print(13 % 2)