Variables and Assignment

Python:: Variables and Assignment

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign/ assignment operator (=) is used to assign values to variables.

The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.

##Rules for Variables names in Python

  • must start with a letter or an underscore. eg. speed, _name.
  • remainder of the variable name may consist of letters, numbers and underscores. eg. password1, n00b.
  • Names are case sensitive. eg. class, Class, and CLASS are different variables.
  • No Special characters allowed: eg. @#$%^&*(
  • Python Reserved Words NOT allowed as variable. eg. def, False

All the code snippets above will give the same printout. Depending on which of the snippets makes the most sense to you, you will realise the best way to name your variables, so that it makes sense to others as well.

Can you figure out why there is nothing output to the console screen when you run the above code?
Go ahead, add more variables of your own and try!
You can download the code by clicking the download button in the white window.