Variables in Python

 Variables in python - Bytesize Python

What is a variable?

A variable is something which stores data. You can even think of it like a box which can store bits of  information and be labelled

Creating a variable

When you put information into a variable such as a string or number, it is called Assigning a value to a variable. To assign a value to a variable in python, you use an '=' sign as seen below. For numbers you an just put the number you want to assign the variable to. However strings (text) should be placed in quotation marks. (You can find out more about strings in 'Strings in Python')

Naming variables

There are some rules you have to follow when naming your variables:
  • All letters and numbers can be used
  • But they can't start with a number
  • Special characters cannot be used
  • Spaces can't be used
  • But an underscore '_' can be used instead of a space
  • Variables are case sensitive meaning, 'Hat' and 'hat' would be two different variables
  • And finally, you can't use python command words as variables.

Using variables

The 'print' command can be used to show the value of a variable on screen. You don't need to put quotation marks round a variable even if it is assigned to a string.





You can also change the contents of a variable later on. Just assign it again.












The value of one variable can also be assigned to another variable, as seen below.







You can also print multiple things at once by separating them with a comma.





Popular Posts