Variables are temporary locations in memory.
Just like very other programming language, Python also support several variable types.
Also Python is considered as a Dynamically Typed language, which means we don't want to define the type of the variable before assigning values to it, it does it automatically when we assign value to it.
You can use type() function to check the type of the variable and I used Ipython to get the left side snippet.
Naming Conventions
You can use Simple Letters, Capital Letters, Numbers and Underscore; but you can't start with a number.
Also try not to use mix of simple and capital letters to name a variable as it is generally accepted as a bad practice even though it is possible.
Data Types
Following are the Data Types which can be put into variables which makes the types of variables.
Data Types in red box are mutable and others are immutable. Mutable means can change values without changing the object id (the real memory slot) and immutable cannot change and need to replace and recreate the object to change it.
Strings
Str aka String type is highly used by us to store the text values like "show ip int br" or may be "show ip route". It can just include characters spaces etc. Also the numbers if it is with characters. An example value would be "router10".
You can use double quotes or single quotes to assign vales.
Integers
Int are integers which means regular numbers. It is just like a String without quotation marks and the value is just a number.
Float
float is where the numbers have with a decimal point.
As an example, 10.0 can be mathematically identified as a integer but since there is a decimal dot, Python identifies it as a float.
Lists
Starts with square brackets.
List is set of ordered and mutable objects. Ordered means the order of the objects matter and mutable means you can change values.
If need to get the 1st value / 2nd value of the list,
You can use append() function to add a new value to the list,
Tuples
Starts with round brackets.
Tuple is very much like a List but it is immutable means you cannot change the values.
If you want to add something, you can recreate it again using the same values and added values.
You can use id() function to check the object id of the variable when it was created at the backend, note that this will change completely when a new value is replacing the old value of the variable which means it becomes a whole new object at the backend.
Dictionaries
Starts with curly brackets.
Dict means dictionary. Dictionaries are type of variables which contains unordered key - value pairs.
Above you can see how to retrieve a value assigned to a key also.
Since this is mutable, we can add a new key - value pair like the following.
But what if we try to change a value of a already created key like the following..
It doesn't work like that instead it adds a new key - value pair.
Sets
Starts with curly brackets but no key-value pairs.
Set is a mutable collection of unordered objects. You cannot have duplicate items in a Set.
If you noticed, I added "vlan 5" twice but it only includes one of them as it cannot have duplicate entries.
Can add a value using add() function like the following.
Also you can notice that the order didn't matter and that's why we could use add() function instead of append() function. You can also remove an item using remove() function.
Booleans
Bool is just variable with a logical value, either true or false.
Note:-
Strings, Integers amd Floats can consider as immutable.
Another point is that, even though Python is a dynamic language, incase if you need to specify the data type of a variable it can be done and it's called "Casting".
Ex:- x should be '5', the character only, not the numeric value.