Skip to content

Variable

  • variable is a concept of storing a value or calculation result when programme is running.
  • The value of the variable can be use multiple time and it can change its value at anytime.

Format

variable_name = variable_value
  • variable_name is the name of the variable
  • = , means assignment
  • variable_value is the storing value of the variable name

Usage

variable.py

python
# Assign a value to a variable, to store amount of money
money = 100

# By using print to output the value of money, we can see how much money we have
print("Money left:", money)

# Now, I buy a book, costs 10

# At here we using 
# money (variable) instead of 100 (value) 
# to calculate how much money left
money = money - 10

print("Money left after buying a book:", money)

Output:

Money left: 100
Money left after buying a book: 90