Variables are used to store values. 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. For example:
One can also assign a single value to several variables simultaneously. For example:Here, an integer object is created with the value 5, and all three variables are assigned to the same memory location. One can also assign multiple objects to multiple variables. For example: Here two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "test" is assigned to the variable c.
Complex number type
In Python, you will mean integer, float values, string and complex numbers. A complex number is created using "j" or "J" to represent the squire root -1:
Type of variables
If you are not sure what type a variable, use the function type() to inspect it:
You can convert float to integer using "int()" method. Any number can be converted to string using "str()" method. Here we convert float to integer, then back to float and then to a string:
A namespace is the way of mapping variable names to their values. A namespace is a sort of dictionary containing all defined variable names and corresponding reference to their values. The purpose of the dir() function is to find out what names are defined in a given namespace, and return a list of those names. If called without arguments, it returns a list of the names defined in the local namespace.
Let's look at the namespaces and print them
If you have defined some variables, you will see them in the local namespace:
If you imported functions from a module (say, "math"), you add more variables to your local namespace:
Another way to do this is to use the module name an in argument for dir():
As you can see, this function is very handy for finding out what items are in a module or class.