variables

Variables are like boxes.
You can put anything into a box.

Naming a variable
stick to lowercase letters
No spaces
No numbers at the front of the word (Bad 1num Good num1)
you_can_use_underscores

Numbers in variables

You can put numbers into a variable.
In Python you do this by using an = sign
The variable always goes on the left and what you put inside it on the right.

Put a number in a variable
hitpoints = 10
print(hitpoints)
This would output as
>>>
10
>>>
You can add to a number in a variable
hitpoints = 10
hitpoints = hitpoints + 2
print(hitpoints)

This would output as
>>>
12
>>>
You can subtract from a number in a variable
hitpoints = 10
hitpoints = hitpoints -1
print(hitpoints)

This would output as
>>>
9
>>>

You can multiply numbers in a variable
mynumber = 10
mynumber = mynumber * 2
print(mynumber)

This would output as
>>>
20
>>>
Or you can just use the variables to multiply
number = 10
number = number * number
print(number)

This would output as
>>>
100
>>>
You can divide using variables
pocketmoney = 14
pocketmoney = pocketmoney /7
print(pocketmoney)

This would output as
>>>
2.0
>>>
Which is the decimal answer the same as 2
In Python this is called a float or floating number

You can calculate just using variables
daysinyear = 365
hoursinday = 24
hoursinyear = daysinyear * hoursinday
print(hoursinyear)

This would output as
>>>
8760
>>>
Note that we use * instead of x and / instead of divide
You can find out what other maths symbols Python uses here

Text in variables
You can put text in variables

name = ("Frank")
print(name)

This would output as
>>>
Frank
>>>
You can also get the user of your program to put text into a variable
name = input("What is your name?")
print("Hello",name)

Whatever name the user types in will now be inside the variable name
If I typed Herbert when asked my name by the program it would print as
>>>
Hello Herbert
>>>
Find out more about input

Variables can change inside a program
var = ("hello")
print(var)
var = ("goodbye")
print(var)
var = 2
print(var)

This would output as
>>>
hello
goodbye
2
>>>

Each time var is printed it has had new information placed inside it

Comments