Python Variable Types
Python Variable Types
Variables are nothing but reserved memory locations to store certain 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.
While learning about Python variable types it is highly recommended that you try out all statements in the Python shell and see the results. You must also explore and experiment with the shell.
Variables are the most important aspect of any programming language including python. In simple words, variables are names given to memory locations. The values stored in these memory locations can be altered throughout the programming life cycle. Variables are stored in the main memory and are generally required to carry forward various processes and transactions. Since we are talking about memory allocated to variables, it is quite obvious that once the variables are defined they will get associated with a physical address in the main memory. If we start accessing these values by physical addresses, the code will get complicated and that is why the addresses are associated with variable names.
In case of a static programming language, a variable maybe defined as follows:
int variable_name = 10;
It is not important to assign a value, but it is important to provide the data type.
In case of Python it is important to provide value:
variable name = 10
The value ’10’ is stored in memory.
Now if we create one more variable as follows:
variable_name2 = variable_name
Then both the variables will refer to the same object in the memory and new object will not be created. Another interesting thing about variables in Python is that if the type of value is changed, then the type of the variable also changes. So, if the value of the variable is converted to String, then the integer object created for it earlier will disappear and the variable_name will only fetch the string object.
Generally the approach in every static programming language such as Java, C + + etc. is that while declaring a variable you have to describe its type which means for every variable you have to define whether it is going to store an integer, or a string, or a float etc. however, things are different with python. Here, you need not define the variable type. Python will automatically detect the type. It is for this reason that Python is considered to be dynamic in nature.
The values are assigned to the variable using ‘=’ sign. ‘=’ sign here does not mean that the values on the right and left are equal, the expression given on the right is first evaluated and then the value is passed on to the variable. Therefore, unlike mathematics, Python associates the value or the expression on the right with the variable given on the left.
As mentioned above, python is a smart language that has several built in functions that automatically detect different types of data and operate on them to produce results. So, after assigning a value to a variable you can easily detect its type with the help of function ‘type’. The syntax for using this function is: Type (parameters). To print the outcome of this function write: Print type (parameters). In order to assign a value to a variable the name of the variable is placed at the left of the operator (‘ =’) and the value assigned is written on the right hand side. You can also assign values to multiple variables in one go. If you want to assign the same value to multiple values, then it can be done as shown below: There are several types of data types in Python. Python stores the information about the type of an object within the object. So, when an operation is to be performed on the object, Python evaluates the object rather than the operation to see if works on it or not. The most commonly used built-in data types are as follows:
Variables are created on the basis of their values and not name. a value can exist in the memory only if it has a name associated with it. Assigning several names to same value means that you are creating several copies of the address of the same objectNumeric and string objects are immutable, which means once created their values cannot be changed.
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. ‘Tuple’ is an immutable Python object which means that once it has been created it cannot be changed. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed between brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
Tuples are often used to store related data, such as values of (latitude and longitude), (height and width), (age, name and sex) etc. It can be used to hold any number of data, but once created the elements cannot be added or removed.
A Tuple can be created as follows:
A variable can be assigned to a Tuple as follows:
tup1 =(‘ John’,’M’, 27)
A tuple can be deleted as follows:
The following example will give you a better understanding:
You can find the length of the tuple in the following manner:
It is possible to concatenate tuples. Have a look at the example below:
You can also check if a particular value exists in a tuple
The following is an example of how to carry out iterations in tuples
Now lets see how repetitions can be caused in tuples
You also have the option of indexing and slicing in tuples. Consider the tuple given below:
This concludes the tutorial on Python Variable Types.