A variable is an identifier that holds a value.
An Identifier is used to identify the literals used in the program. There a few rules that are used in the naming of an identifier, lets take a look at these.
- The first character of the variable must start with a letter or the underscore character
- All characters except the first character can be lower case(a-z), upper case (A-Z), an underscore, or a numerical digit (0-9).
- An identifier must not contain any white space or special characters such as $, %, !,@, *(!, @, #, %, ^, &, *).
- An identifier is case sensitive; for example, myname, MyName, Myname and MyName are not the same.
That last one is a biggie, lets look at examples of legal and illegal names
#Legal variable names: myname = "python" my_name = "python" _my_name = "python" myName = "python" MYNAME = "python" myname1 = "python" #Illegal variable names: 1myname = "python" my-name = "python" my name = "python
Python does not require us to declare a variable before using it. It allows us to create a variable at the required time. You also do not need to declare explicitly a variable in Python. When we assign a value to the variable, that variable is declared automatically.
The equal (=) operator is used to assign value to a variable.
Let’s look at a basic example
x = 8 y = "python" print(x) print(y)
You should see something like this in the REPL window
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822 Type "help()" for more information. >>> 8 python
Variables can even change type after they have been set. Lets see an example of that where we have a variable called x and it will be an int first and then we will change it to a string
x = 8 print(x) x = "python" print(x)
Now run this example
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822 Type "help()" for more information. >>> 8 python
String variables
A String variable can be declared either by using single or double quotes, like this
mylanguage1 = "Python" print(mylanguage1) mylanguage2 = 'Python' print(mylanguage2)
When you run this you will see the following
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822 Type "help()" for more information. >>> Python Python
Multiple Values and variables
Python allows you to assign values to multiple variables in one line
Python also allows you to assign the same value to multiple variables in one line
x, y, z = "esp8266", "esp32", "microbit" print(x) print(y) print(z) x = y = z = "microbit" print(x) print(y) print(z)
Run this and you will see something like this
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822 Type "help()" for more information. >>> >>> esp8266 esp32 microbit microbit microbit microbit
Using the + character
You can use the + character to do the following
You can combine both text and a variable
You can add a variable to another variable
You can add numbers
x = "microcontroller" print("ESP32 is a " + x) x = "ESP32 is a " y = "microcontroller" z = x + y print(z) x = 7 y = 9 print(x + y)
You will see the following
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822 Type "help()" for more information. >>> >>> ESP32 is a microcontroller ESP32 is a microcontroller 16
Python will give you an error if you combine a string and a number
x = "ESP" y = 32 print(x + y)
If you run this you will see an error like this
TypeError: can't convert 'int' object to str implicitly