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.
If you want enrich your career and become a professional in python, then visit Mindmajix - a global online training platform : python certification course This course will help you to achieve excellence in this domain |
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:
1. Numbers
2. String
3. List
4. Tuple
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.
Related Page: Python String Functions
A Tuple can be created as follows:
(‘a’,’ b’)
A variable can be assigned to a Tuple as follows:
tup1 =(‘ John’,’M’, 27)
A tuple can be deleted as follows:
Del tuple_name
The following example will give you a better understanding:
You can find the length of the tuple in the following manner:
Len(tuple_value)
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.
if you are interested learn python and build a career in it ? Then checkout our python training near your cities
Python Training Chennai Python Training New York Python Training in Bangalore Python Training in Dallas
These courses are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in Microsoft Azure and help you to achieve your dream job.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Python Training | Nov 23 to Dec 08 | View Details |
Python Training | Nov 26 to Dec 11 | View Details |
Python Training | Nov 30 to Dec 15 | View Details |
Python Training | Dec 03 to Dec 18 | View Details |
Anjaneyulu Naini is working as a Content contributor for Mindmajix. He has a great understanding of today’s technology and statistical analysis environment, which includes key aspects such as analysis of variance and software,. He is well aware of various technologies such as Python, Artificial Intelligence, Oracle, Business Intelligence, Altrex, etc. Connect with him on LinkedIn and Twitter.